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& first = node.list()[0];
8
9 if (first.keyword() == Keyword::If)
10 {
11 Node cond = node.list()[1];
12 Node temp = evaluate(cond, /* is_not_body */ true);
13 Node if_true = node.list()[2];
14 Node if_false = node.constList().size() > 3 ? node.list()[3] : Node::getNilNode();
15
16 // evaluate cond
17 if (isTruthy(temp))
18 node = if_true;
19 else if (node.constList().size() > 3)
20 node = if_false;
21 else
22 {
23 // remove node because nothing matched
24 node.list().clear();
26 }
27
28 if (node.nodeType() == NodeType::Macro)
29 registerMacro(node);
30
31 return true;
32 }
33
34 return false;
35 }
36
38 {
39 return node.nodeType() == NodeType::Macro && node.list()[0].nodeType() == NodeType::Keyword;
40 }
41}
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:37
Node evaluate(Node &node, bool is_not_body)
Evaluate only the macros.
Definition: Executor.cpp:30
bool isTruthy(const Node &node)
Check if a node can be evaluated to true.
Definition: Executor.cpp:25
void registerMacro(Node &node)
Registers macros based on their type.
Definition: Executor.cpp:20
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:29
NodeType nodeType() const noexcept
Return the node type.
Definition: Node.cpp:126
void setNodeType(NodeType type) noexcept
Set the Node Type object.
Definition: Node.cpp:131
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Definition: Node.cpp:119
Keyword keyword() const noexcept
Return the keyword held by the value (if the node type allows it)
Definition: Node.cpp:102
static const Node & getNilNode()
Provide a statically initialized / correct and guaranteed to be initialized Node representing "Nil".
Definition: Node.cpp:22
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
Definition: Node.cpp:114