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{
6 {
7 Node cond = node.list()[1];
8 const Node temp = evaluate(cond, /* 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 = if_true;
15 else if (node.constList().size() > 3)
16 node = if_false;
17 else
18 {
19 // remove node because nothing matched
20 node.list().clear();
22 }
23
24 // todo: is this still needed? does registerMacro have to call applyMacro???
25 if (node.nodeType() == NodeType::Macro)
26 registerMacro(node);
27
28 return true;
29 }
30
32 {
33 return node.nodeType() == NodeType::Macro && node.list()[0].nodeType() == NodeType::Keyword && node.list()[0].keyword() == Keyword::If;
34 }
35}
Executor for Conditional Macros.
bool applyMacro(Node &node) override
Executes macros in the Node if the Executor can handle it.
Definition: Conditional.cpp:5
bool canHandle(Node &node) override
Checks if the executor can apply a macro on the passed Node.
Definition: Conditional.cpp:31
Node evaluate(Node &node, bool is_not_body) const
Evaluate only the macros.
Definition: Executor.cpp:27
void registerMacro(Node &node) const
Registers macros based on their type.
Definition: Executor.cpp:17
bool isTruthy(const Node &node) const
Check if a node can be evaluated to true.
Definition: Executor.cpp:22
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:30
NodeType nodeType() const noexcept
Return the node type.
Definition: Node.cpp:71
void setNodeType(NodeType type) noexcept
Set the Node Type object.
Definition: Node.cpp:81
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Definition: Node.cpp:66
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
Definition: Node.cpp:61
const Node & getNilNode()
Definition: Node.cpp:293