12 using namespace internal;
15 m_parser(debug, options, libenv), m_optimizer(options),
16 m_options(options), m_debug(debug)
33 template <
typename... Args>
36 constexpr size_t buffer_size = 8192;
37 static char buf[buffer_size] = { 0 };
38 std::string to_return =
"";
39 while (snprintf(buf, buffer_size - 1, format.c_str(), std::forward<Args>(args)...) == buffer_size - 1)
40 to_return += std::string(buf);
41 to_return += std::string(buf);
47 std::string json =
"";
51 case NodeType::Symbol:
54 R
"({"type": "Symbol", "name": "%s"})",
59 case NodeType::Capture:
62 R
"({"type": "Capture", "name": "%s"})",
67 case NodeType::GetField:
70 R
"({"type": "GetField", "name": "%s"})",
75 case NodeType::String:
78 R
"({"type": "String", "value": "%s"})",
83 case NodeType::Number:
86 R
"({"type": "Number", "value": %f})",
93 if (node.
constList().size() > 1 && node.
constList()[0].nodeType() == NodeType::Keyword)
101 std::string args =
"";
103 for (std::size_t i = 0, end = args_node.
constList().size(); i < end; ++i)
106 if (end > 1 && i != end - 1)
111 R
"({"type": "Fun", "args": [%s], "body": %s})",
120 R
"({"type": "Let", "name": %s, "value": %s})",
129 R
"({"type": "Mut", "name": %s, "value": %s})",
138 R
"({"type": "Set", "name": %s, "value": %s})",
147 R
"({"type": "If", "condition": %s, "then": %s, "else": %s})",
156 R
"({"type": "While", "condition": %s, "body": %s})",
164 json += R
"({"type": "Begin", "children": )";
169 case Keyword::Import:
173 R
"({"type": "Import", "value": %s})",
182 R
"({"type": "Quote", "value": %s})",
191 R
"({"type": "Del", "value": %s})",
197 else if (node.
constList().size() > 1 && node.
constList()[0].nodeType() == NodeType::Symbol)
201 R
"({"type": "FunctionCall", "name": %s, "args": )",
213 "Not handled NodeType::%s (%s at %zu:%zu), please report this error on GitHub",
224 std::string json =
"[";
225 for (std::size_t i = start, end = node.
constList().size(); i < end; ++i)
Handles the macros and their expansion in ArkScript source code.
unsigned m_debug
the debug level of the compiler
void feed(const std::string &code, const std::string &filename=ARK_NO_NAME_FILE)
Feed the differents variables with information taken from the given source code file.
JsonCompiler(unsigned debug, const std::vector< std::string > &libenv, uint16_t options=DefaultFeatures)
Construct a new JsonCompiler object.
std::string compile()
Start the compilation.
internal::Parser m_parser
std::string _compile(const internal::Node &node)
Compile a single node and return its representation.
internal::Optimizer m_optimizer
std::string toJsonList(const internal::Node &node, std::size_t start)
Convert a NodeType::List to a JSON list.
The class handling the macros definitions and calls, given an AST.
const Node & ast() const noexcept
Return the modified AST.
void feed(const Node &ast)
Send the complete AST (after the inclusions and stuff), and work on it.
A node of an Abstract Syntax Tree for ArkScript.
NodeType nodeType() const noexcept
Return the node type.
const std::string & filename() const noexcept
Return the filename in which this node was created.
const std::string & string() const noexcept
Return the string held by the value (if the node type allows it)
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Keyword keyword() const noexcept
Return the keyword held by the value (if the node type allows it)
std::size_t col() const noexcept
Get the column at which this node was created.
double number() const noexcept
Return the number held by the value (if the node type allows it)
std::size_t line() const noexcept
Get the line at which this node was created.
void feed(const Node &ast)
Send the AST to the optimizer, then run the different optimization strategies on it.
const Node & ast() const noexcept
Returns the modified AST.
const Node & ast() const noexcept
Return the generated AST.
void feed(const std::string &code, const std::string &filename=ARK_NO_NAME_FILE)
Give the code to parse.
constexpr std::array< std::string_view, 11 > nodeTypes
std::string string_format(const std::string &format, Args &&... args)