ArkScript
A small, fast, functional and scripting language for video games
Conditional.cpp
Go to the documentation of this file.
2
3namespace Ark::internal
4{
5 bool ConditionalExecutor::applyMacro(Node& node, const unsigned depth)
6 {
7 Node cond = node.list()[1];
8 const Node temp = evaluate(cond, depth + 1, /* is_not_body */ true);
9 const Node if_true = node.list()[2];
10 const Node if_false = node.constList().size() > 3 ? node.list()[3] : getNilNode();
11
12 // evaluate cond
13 if (isTruthy(temp))
14 node.updateValueAndType(if_true);
15 else if (node.constList().size() > 3)
16 node.updateValueAndType(if_false);
17 else
18 {
19 // remove node because nothing matched
20 node.list().clear();
22 }
23
24 if (node.nodeType() == NodeType::Macro)
25 {
26 handleMacroNode(node);
27 applyMacroProxy(node, depth + 1);
28 }
29
30 return true;
31 }
32
34 {
35 return node.nodeType() == NodeType::Macro && node.list()[0].nodeType() == NodeType::Keyword && node.list()[0].keyword() == Keyword::If;
36 }
37}
Executor for Conditional Macros.
bool applyMacro(Node &node, unsigned depth) override
bool canHandle(Node &node) override
void applyMacroProxy(Node &node, unsigned depth)
Apply a macro on a given node.
Definition Executor.cpp:17
Node evaluate(Node &node, unsigned depth, bool is_not_body) const
Evaluate only the macros.
Definition Executor.cpp:32
bool isTruthy(const Node &node) const
Check if a node can be evaluated to true.
Definition Executor.cpp:27
void handleMacroNode(Node &node) const
Registers macros based on their type, expand conditional macros.
Definition Executor.cpp:22
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:31
NodeType nodeType() const noexcept
Return the node type.
Definition Node.cpp:77
void setNodeType(NodeType type) noexcept
Set the Node Type object.
Definition Node.cpp:98
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Definition Node.cpp:72
void updateValueAndType(const Node &source) noexcept
Copy a node to the current one, while keeping the filename and position in the file.
Definition Node.cpp:92
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
Definition Node.cpp:67
const Node & getNilNode()
Definition Node.cpp:320