14 m_welder(debug, lib_env, features)
33 case NodeType::Symbol:
36 R
"({{"type": "Symbol", "name": "{}"}})",
41 case NodeType::MutArg:
44 R
"({{"type": "MutArg", "name": "{}"}})",
49 case NodeType::RefArg:
52 R
"({{"type": "RefArg", "name": "{}"}})",
57 case NodeType::Spread:
60 R
"({{"type": "Spread", "name": "{}"}})",
65 case NodeType::Capture:
68 R
"({{"type": "Capture", "name": "{}"}})",
75 json += R
"({"type": "Field", "children": )";
80 case NodeType::String:
83 R
"({{"type": "String", "value": "{}"}})",
88 case NodeType::Number:
91 R
"({{"type": "Number", "value": {}}})",
108 if (args_node.
nodeType() == NodeType::List)
111 for (std::size_t i = 0, end = args_node.
constList().size(); i < end; ++i)
114 if (end > 1 && i != end - 1)
123 R
"({{"type": "Fun", "args": {}, "body": {}}})",
132 R
"({{"type": "Let", "name": {}, "value": {}}})",
141 R
"({{"type": "Mut", "name": {}, "value": {}}})",
150 R
"({{"type": "Set", "name": {}, "value": {}}})",
160 R
"({{"type": "If", "condition": {}, "then": {}, "else": {}}})",
164 R
"({{"type": "If", "condition": {}, "then": {}}})",
173 R
"({{"type": "While", "condition": {}, "body": {}}})",
181 json += R
"({"type": "Begin", "children": )";
186 case Keyword::Import:
191 std::string
package = node.constList()[1].constList().front().string();
192 for (
const auto& sym : node.
constList()[1].constList() | std::views::drop(1))
193 package +=
"." + sym.string();
195 bool is_glob = node.
constList()[2].nodeType() == NodeType::Symbol && node.
constList()[2].string() ==
"*";
196 std::vector<std::string> syms;
197 if (node.
constList()[2].nodeType() == NodeType::List)
198 std::ranges::transform(
200 std::back_inserter(syms),
201 [](
const auto& sym) {
202 return fmt::format(
"{:?}", sym.string());
206 R
"({{"type": "Import", "package": "{}", "glob": {}, "symbols": [{}]}})",
209 fmt::join(syms, ", "));
217 R
"({{"type": "Del", "value": {}}})",
223 else if (node.
constList().size() > 1 && node.
constList()[0].nodeType() == NodeType::Symbol)
227 R
"({{"type": "FunctionCall", "name": {}, "args": {}}})",
237 case NodeType::Macro:
239 if (
const auto& first = node.
constList()[0]; first.nodeType() == NodeType::Symbol)
242 R
"({{"type": "Macro", "name": {}, )",
248 R
"("args": {}, "body": {}}})",
252 else if (first.nodeType() == NodeType::Keyword)
254 if (first.keyword() == Keyword::If)
256 R
"({{"type": "MacroCondition", "condition": {}, "then": {}, "else": {}}})",
264 case NodeType::Unused:
269 "Not handled NodeType::{} ({} at {}:{}), please report this error on GitHub",
280 std::vector<std::string> json;
281 for (std::size_t i = start, end = node.
constList().size(); i < end; ++i)
283 if (node.
constList()[i].nodeType() != NodeType::Unused)
286 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)
FileSpan position() const noexcept
Get the span of the node (start and end)
double number() const noexcept
Return the number held by the value (if the node type allows it)
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, 13 > nodeTypes
Node types as string, in the same order as the enum NodeType.
std::size_t column
0-indexed column number
std::size_t line
0-indexed line number