14 m_welder(debug, lib_env, features)
33 case NodeType::Symbol:
36 R
"({{"type": "Symbol", "name": "{}"}})",
41 case NodeType::Spread:
44 R
"({{"type": "Spread", "name": "{}"}})",
49 case NodeType::Capture:
52 R
"({{"type": "Capture", "name": "{}"}})",
59 json += R
"({"type": "Field", "children": )";
64 case NodeType::String:
67 R
"({{"type": "String", "value": "{}"}})",
72 case NodeType::Number:
75 R
"({{"type": "Number", "value": {}}})",
92 if (args_node.
nodeType() == NodeType::List)
95 for (std::size_t i = 0, end = args_node.
constList().size(); i < end; ++i)
98 if (end > 1 && i != end - 1)
107 R
"({{"type": "Fun", "args": {}, "body": {}}})",
116 R
"({{"type": "Let", "name": {}, "value": {}}})",
125 R
"({{"type": "Mut", "name": {}, "value": {}}})",
134 R
"({{"type": "Set", "name": {}, "value": {}}})",
144 R
"({{"type": "If", "condition": {}, "then": {}, "else": {}}})",
148 R
"({{"type": "If", "condition": {}, "then": {}}})",
157 R
"({{"type": "While", "condition": {}, "body": {}}})",
165 json += R
"({"type": "Begin", "children": )";
170 case Keyword::Import:
175 std::string
package = node.constList()[1].constList().front().string();
176 for (
const auto& sym : node.
constList()[1].constList() | std::views::drop(1))
177 package +=
"." + sym.string();
179 bool is_glob = node.
constList()[2].nodeType() == NodeType::Symbol && node.
constList()[2].string() ==
"*";
180 std::vector<std::string> syms;
181 if (node.
constList()[2].nodeType() == NodeType::List)
183 for (const auto& sym : node.constList()[2].constList())
184 syms.push_back(
'"' + sym.string() +
'"');
187 R
"({{"type": "Import", "package": "{}", "glob": {}, "symbols": [{}]}})",
190 fmt::join(syms, ", "));
198 R
"({{"type": "Del", "value": {}}})",
204 else if (node.
constList().size() > 1 && node.
constList()[0].nodeType() == NodeType::Symbol)
208 R
"({{"type": "FunctionCall", "name": {}, "args": {}}})",
218 case NodeType::Macro:
220 if (
const auto& first = node.
constList()[0]; first.nodeType() == NodeType::Symbol)
223 R
"({{"type": "Macro", "name": {}, )",
229 R
"("args": {}, "body": {}}})",
233 else if (first.nodeType() == NodeType::Keyword)
235 if (first.keyword() == Keyword::If)
237 R
"({{"type": "MacroCondition", "condition": {}, "then": {}, "else": {}}})",
245 case NodeType::Unused:
250 "Not handled NodeType::{} ({} at {}:{}), please report this error on GitHub",
261 std::vector<std::string> json;
262 for (std::size_t i = start, end = node.
constList().size(); i < end; ++i)
264 if (node.
constList()[i].nodeType() != NodeType::Unused)
267 return fmt::format(
"[{}]", fmt::join(json,
", "));
ArkScript homemade exceptions.
bool computeASTFromFile(const std::string &filename)
const internal::Node & ast() const noexcept
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 std::string &filename)
Feed the different variables with information taken from the given source code file.
JsonCompiler(unsigned debug, const std::vector< std::filesystem::path > &lib_env, uint16_t features=0)
Construct a new JsonCompiler object.
std::string compile()
Start the compilation.
std::string toJsonList(const Ark::internal::Node &node, std::size_t start)
Convert a NodeType::List to a JSON list.
std::string _compile(const Ark::internal::Node &node)
Compile a single node and return its representation.
constexpr std::array< std::string_view, 11 > nodeTypes
Node types as string, in the same order as the enum NodeType.