Skip to content

Commit

Permalink
Introduce a universal proto language
Browse files Browse the repository at this point in the history
Which will act mediator language between the different languages.

Add new files and refactor code to implement AST-based parsing and translation of GCode

New source and header files added for Abstract Syntax Tree (AST) based system to handle GCode translation. This change helps improve code maintainability by unifying parsing and translation functionality. The changes also include modification of existing files, classes, and function signatures to accommodate and support the new system.

Notably, new classes for AST nodes, such as Translate, ast_t and node_t have been designed. For parsing GCode, a factory class is created that maps GCode instructions to corresponding AST nodes. For translating GCode to Proto Path, a translate function is added.

Also, to make it more readable and aligned, the code has been refactored, primarily renaming and relocating components for better structure. Furthermore, file and class headers were renamed to match current usage.

Contribute to CURA-10561
  • Loading branch information
jellespijker committed Oct 17, 2023
1 parent 01c2c33 commit 1f7a92a
Show file tree
Hide file tree
Showing 28 changed files with 293 additions and 144 deletions.
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ find_package(ctre REQUIRED)

# --- Setup the shared C++ mgjtp library ---
set(DULCIFICUM_SRC
src/command_types.cpp
src/utils/io.cpp
src/gcode/ast/ast.cpp
src/gcode/ast/operators.cpp
src/gcode/parse.cpp

src/miracle_jtp/mgjtp_command_to_json.cpp
src/miracle_jtp/mgjtp_json_to_command.cpp

src/proto_path/translate.cpp

src/utils/io.cpp
src/command_types.cpp
)
add_library(dulcificum ${DULCIFICUM_SRC})
target_link_libraries(dulcificum
Expand Down
15 changes: 3 additions & 12 deletions apps/translator_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <docopt/docopt.h>
#include <dulcificum/gcode/parse.h>
#include <dulcificum/proto_path/translate.h>
#include <dulcificum/utils/io.h>
#include <map>

Expand Down Expand Up @@ -36,18 +37,8 @@ int main(int argc, const char** argv)
spdlog::info("Tasting the menu");

auto input{ dulcificum::utils::readFile(args.at("INPUT").asString()).value() };
auto ast = dulcificum::gcode::parse(input);

auto TypeOfNode = Overload{ [](auto& node)
{
node();
}
};

for (auto& node : ast)
{
std::visit(TypeOfNode, node);
}
auto gcode_ast = dulcificum::gcode::parse(input);
auto proto_path_ast = dulcificum::proto_path::translate(gcode_ast);

auto x = 1;
}
6 changes: 3 additions & 3 deletions include/dulcificum/command_types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_COMMAND_TYPES_H
#define DULCIFICUM_COMMAND_TYPES_H
#ifndef INCLUDE_DULCIFICUM_COMMAND_TYPES_H
#define INCLUDE_DULCIFICUM_COMMAND_TYPES_H

#include <cmath>
#include <memory>
Expand Down Expand Up @@ -153,4 +153,4 @@ CommandPtr spawnCommandPtr(const CommandType& type) noexcept;
} // namespace botcmd
} // namespace dulcificum

#endif // DULCIFICUM_COMMAND_TYPES_H
#endif // INCLUDE_DULCIFICUM_COMMAND_TYPES_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/acceleration.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ACCELERATION_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ACCELERATION_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_ACCELERATION_H
#define INCLUDE_DULCIFICUM_GCODE_AST_ACCELERATION_H

#include "dulcificum/gcode/ast/entry.h"

Expand Down Expand Up @@ -35,4 +35,4 @@ class M205 : public Entry<R"(M205((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.

} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ACCELERATION_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_ACCELERATION_H
50 changes: 4 additions & 46 deletions include/dulcificum/gcode/ast/ast.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_AST_H
#define INCLUDE_DULCIFICUM_GCODE_AST_AST_H

#include "dulcificum/gcode/ast/acceleration.h"
#include "dulcificum/gcode/ast/bed_temperature.h"
Expand Down Expand Up @@ -50,50 +50,8 @@ using node_t = std::variant<
Unknown>;
using ast_t = std::vector<node_t>;

using Creator = std::function<node_t(size_t, const std::string&)>;

// clang-format off
std::map<std::string, Creator> creators
{
{ "G1", [](size_t index, const std::string& line) { return G1{ index, line }; } },
{ "G0", [](size_t index, const std::string& line) { return G0{ index, line }; } },
{ "T", [](size_t index, const std::string& line) { return T{ index, line }; } },
{ "G90", [](size_t index, const std::string& line) { return G90{ index, line }; } },
{ "G91", [](size_t index, const std::string& line) { return G91{ index, line }; } },
{ "G92", [](size_t index, const std::string& line) { return G92{ index, line }; } },
{ "G280", [](size_t index, const std::string& line) { return G280{ index, line }; } },
{ "M82", [](size_t index, const std::string& line) { return M82{ index, line }; } },
{ "M83", [](size_t index, const std::string& line) { return M83{ index, line }; } },
{ "M104", [](size_t index, const std::string& line) { return M104{ index, line }; } },
{ "M106", [](size_t index, const std::string& line) { return M106{ index, line }; } },
{ "M107", [](size_t index, const std::string& line) { return M107{ index, line }; } },
{ "M109", [](size_t index, const std::string& line) { return M109{ index, line }; } },
{ "M140", [](size_t index, const std::string& line) { return M140{ index, line }; } },
{ "M190", [](size_t index, const std::string& line) { return M190{ index, line }; } },
{ "M204", [](size_t index, const std::string& line) { return M204{ index, line }; } },
{ "M205", [](size_t index, const std::string& line) { return M205{ index, line }; } },
{ ";LAYER:", [](size_t index, const std::string& line) { return Layer{ index, line }; } },
{ ";MESH:", [](size_t index, const std::string& line) { return Mesh{ index, line }; } },
{ ";TYPE:", [](size_t index, const std::string& line) { return FeatureType{ index, line }; } },
{ ";BUILD_PLATE.INITIAL_TEMPERATURE:", [](size_t index, const std::string& line) { return InitialTemperatureBuildPlate{ index, line }; } },
{ ";BUILD_VOLUME.TEMPERATURE:", [](size_t index, const std::string& line) { return BuildVolumeTemperature{ index, line }; } },
{ ";EXTRUDER_TRAIN.0.INITIAL_TEMPERATURE:", [](size_t index, const std::string& line) { return InitialTemperatureExtruder{ index, line }; } },
{ ";EXTRUDER_TRAIN.1.INITIAL_TEMPERATURE:", [](size_t index, const std::string& line) { return InitialTemperatureExtruder{ index, line }; } },
};
// clang-format on

node_t factory(size_t index, const std::string& line)
{
for (const auto& pair : creators)
{
if (line.starts_with(pair.first))
{
return pair.second(index, line);
}
}
return Unknown{ index, line };
}
node_t factory(size_t index, const std::string& line);

} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_AST_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_AST_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/bed_temperature.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_BED_TEMPERATURE_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_BED_TEMPERATURE_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_BED_TEMPERATURE_H
#define INCLUDE_DULCIFICUM_GCODE_AST_BED_TEMPERATURE_H

#include "dulcificum/gcode/ast/entry.h"

Expand Down Expand Up @@ -31,4 +31,4 @@ class M190 : public Entry<R"(M190((?:\sS(?<S>\d+(?:\.\d+)?))|(?:\sR(?<R>\d+(?:\.
};
} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_BED_TEMPERATURE_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_BED_TEMPERATURE_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/comment_commands.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_COMMENT_COMMANDS_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_COMMENT_COMMANDS_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_COMMENT_COMMANDS_H
#define INCLUDE_DULCIFICUM_GCODE_AST_COMMENT_COMMANDS_H

#include "dulcificum/gcode/ast/entry.h"

Expand Down Expand Up @@ -80,4 +80,4 @@ class BuildVolumeTemperature : public Entry<R"(;BUILD_VOLUME.TEMPERATURE:(?<S>([

} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_COMMENT_COMMANDS_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_COMMENT_COMMANDS_H
10 changes: 5 additions & 5 deletions include/dulcificum/gcode/ast/entry.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H
#define INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H

#include "dulcificum/utils/char_range_literal.h"

Expand Down Expand Up @@ -27,14 +27,14 @@ class Entry
constexpr auto get()
{
return ctre::match<Pattern.value>(line);
}
};

virtual constexpr void operator()()
{
spdlog::info("lino: {} -> {}", index, line);
}
};
};

} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_ENTRY_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/extruder_temperature.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_EXTRUDER_TEMPERATURE_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_EXTRUDER_TEMPERATURE_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_EXTRUDER_TEMPERATURE_H
#define INCLUDE_DULCIFICUM_GCODE_AST_EXTRUDER_TEMPERATURE_H

#include "dulcificum/gcode/ast/entry.h"

Expand Down Expand Up @@ -31,4 +31,4 @@ class M109 : public Entry<R"(M109((?:\sS(?<S>\d+(?:\.\d+)?))|(?:\sR(?<R>\d+(?:\.
};
} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_EXTRUDER_TEMPERATURE_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_EXTRUDER_TEMPERATURE_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/fan.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_FAN_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_FAN_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_FAN_H
#define INCLUDE_DULCIFICUM_GCODE_AST_FAN_H

#include "dulcificum/gcode/ast/entry.h"

Expand Down Expand Up @@ -29,4 +29,4 @@ class M107 : public Entry<R"(M107)">
};
} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_FAN_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_FAN_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/position.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_POSITION_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_POSITION_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_POSITION_H
#define INCLUDE_DULCIFICUM_GCODE_AST_POSITION_H

#include "dulcificum/gcode/ast/entry.h"

Expand All @@ -21,4 +21,4 @@ class G92 : public Entry<R"(G92((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d
};
} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_POSITION_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_POSITION_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/positioning_mode.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_POSITIONING_MODE_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_POSITIONING_MODE_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_POSITIONING_MODE_H
#define INCLUDE_DULCIFICUM_GCODE_AST_POSITIONING_MODE_H

#include "dulcificum/gcode/ast/entry.h"

Expand Down Expand Up @@ -52,4 +52,4 @@ class M83 : public Entry<R"(M83)">

} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_POSITIONING_MODE_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_POSITIONING_MODE_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/purge.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_PURGE_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_PURGE_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_PURGE_H
#define INCLUDE_DULCIFICUM_GCODE_AST_PURGE_H

#include "dulcificum/gcode/ast/entry.h"

Expand All @@ -17,4 +17,4 @@ class G280 : public Entry<R"(G280((?:\sS(?<S>\d+(?:\.\d+)?)))*$)">
};
} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_PURGE_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_PURGE_H
6 changes: 3 additions & 3 deletions include/dulcificum/gcode/ast/toolchange.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_TOOLCHANGE_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_TOOLCHANGE_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_TOOLCHANGE_H
#define INCLUDE_DULCIFICUM_GCODE_AST_TOOLCHANGE_H

#include "dulcificum/gcode/ast/entry.h"

Expand All @@ -17,4 +17,4 @@ class T : public Entry<R"(T(?<T>\d))">
};
} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_TOOLCHANGE_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_TOOLCHANGE_H
17 changes: 6 additions & 11 deletions include/dulcificum/gcode/ast/translate.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_TRANSLATE_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_TRANSLATE_H
#ifndef INCLUDE_DULCIFICUM_GCODE_AST_TRANSLATE_H
#define INCLUDE_DULCIFICUM_GCODE_AST_TRANSLATE_H

#include "dulcificum/gcode/ast/entry.h"

Expand All @@ -21,14 +21,7 @@ class G0 : public Entry<R"(G0((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)
G0(size_t index, std::string line)
: Entry{ index, std::move(line) } {};

void operator()() override
{
if (auto x = get().get<"X">())
{
spdlog::debug("x: {}", x);
}
spdlog::info("lino: {} -> {}", index, line);
}
void operator()() override final;
};

/*!
Expand All @@ -45,8 +38,10 @@ class G1 : public Entry<R"(G1((?:\sX(?<X>\d+(?:\.\d+)?))|(?:\sY(?<Y>\d+(?:\.\d+)
G1() = delete;
G1(size_t index, std::string line)
: Entry{ index, std::move(line) } {};

void operator()() override final;
};

} // namespace dulcificum::gcode::ast

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_AST_TRANSLATE_H
#endif // INCLUDE_DULCIFICUM_GCODE_AST_TRANSLATE_H
27 changes: 4 additions & 23 deletions include/dulcificum/gcode/parse.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
#ifndef DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_PARSE_H
#define DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_PARSE_H
#ifndef INCLUDE_DULCIFICUM_GCODE_PARSE_H
#define INCLUDE_DULCIFICUM_GCODE_PARSE_H

#include "dulcificum/gcode/ast/ast.h"

#include <spdlog/spdlog.h>

#include <cstddef>
#include <sstream>
#include <string>
#include <string_view>

namespace dulcificum::gcode
{

ast::ast_t parse(std::string_view content)
{
std::istringstream stream(content.data());
std::string line;
ast::ast_t ast;

size_t index{ 0 };
while (std::getline(stream, line))
{
ast.emplace_back(ast::factory(index++, line));
}

return ast;
}

ast::ast_t parse(std::string_view content);

} // namespace dulcificum::gcode

#endif // DULCIFICUM_INCLUDE_DULCIFICUM_GCODE_PARSE_H
#endif // INCLUDE_DULCIFICUM_GCODE_PARSE_H
6 changes: 3 additions & 3 deletions include/dulcificum/miracle_jtp/mgjtp_command_to_json.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_MIRACLEGRUE_JSONTOOLPATH_H
#define DULCIFICUM_MIRACLEGRUE_JSONTOOLPATH_H
#ifndef INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_COMMAND_TO_JSON_H
#define INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_COMMAND_TO_JSON_H

#include "dulcificum/command_types.h"

Expand All @@ -12,4 +12,4 @@ nlohmann::json toJson(const botcmd::Command& cmd);

} // namespace dulcificum::miracle_jtp

#endif // DULCIFICUM_MIRACLEGRUE_JSONTOOLPATH_H
#endif // INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_COMMAND_TO_JSON_H
6 changes: 3 additions & 3 deletions include/dulcificum/miracle_jtp/mgjtp_json_to_command.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_MIRACLE_GRUE_JSONTOOLPATH_IN_H
#define DULCIFICUM_MIRACLE_GRUE_JSONTOOLPATH_IN_H
#ifndef INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_JSON_TO_COMMAND_H
#define INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_JSON_TO_COMMAND_H

#include "dulcificum/miracle_jtp/mgjtp_mappings_json_key_to_str.h"

Expand All @@ -12,4 +12,4 @@ botcmd::CommandPtr toCommand(const nlohmann::json& jcmd);

} // namespace dulcificum::miracle_jtp

#endif // DULCIFICUM_MIRACLE_GRUE_JSONTOOLPATH_IN_H
#endif // INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_JSON_TO_COMMAND_H
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DULCIFICUM_MIRACLE_GRUE_JSONTOOLPATH_JSON_KEYS_H
#define DULCIFICUM_MIRACLE_GRUE_JSONTOOLPATH_JSON_KEYS_H
#ifndef INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_MAPPINGS_JSON_KEY_TO_STR_H
#define INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_MAPPINGS_JSON_KEY_TO_STR_H

#include "dulcificum/miracle_jtp/mgjtp_command_to_json.h"

Expand Down Expand Up @@ -67,4 +67,4 @@ constexpr static std::array<const std::string_view, 5> k_param_point_names{ x, y

} // namespace dulcificum

#endif // DULCIFICUM_MIRACLE_GRUE_JSONTOOLPATH_JSON_KEYS_H
#endif // INCLUDE_DULCIFICUM_MIRACLE_JTP_MGJTP_MAPPINGS_JSON_KEY_TO_STR_H
Loading

0 comments on commit 1f7a92a

Please sign in to comment.