ArkScript
A small, fast, functional and scripting language for video games
Executor.hpp
Go to the documentation of this file.
1/**
2 * @file Executor.hpp
3 * @author Ray John Alovera ([email protected])
4 * @brief The base class for all MacroExecutors
5 * @version 0.5
6 * @date 2021-05-04
7 *
8 * @copyright Copyright (c) 2021
9 *
10 */
11
12#ifndef ARK_COMPILER_EXECUTOR_HPP
13#define ARK_COMPILER_EXECUTOR_HPP
14
15#include <unordered_map>
16
18
19namespace Ark::internal
20{
21 class MacroProcessor;
22
23 /**
24 * @brief A class that applies macros in a Node
25 *
26 */
28 {
29 public:
30 /**
31 * @brief Construct a new Macro Executor object
32 *
33 * @param macroprocessor
34 * @param debug
35 */
36 MacroExecutor(MacroProcessor* macroprocessor, unsigned debug = 0);
37
38 /**
39 * @brief Need a virtual destructor to correctly destory object.
40 *
41 */
42 virtual ~MacroExecutor();
43
44 /**
45 * @brief Executes macros in the Node if the Executor can handle it
46 *
47 * @param node the node that contains a macro
48 * @return true if a macro was successfully applied
49 * @return false
50 */
51 virtual bool applyMacro(Node& node) = 0;
52
53 /**
54 * @brief Checks if the executor can apply a macro on the passed Node
55 *
56 * @param node the node that contains a macro
57 */
58 virtual bool canHandle(Node& node) = 0;
59
60 protected:
61 unsigned int m_debug;
62 MacroProcessor* m_macroprocessor; // Note: Non owned pointer.
63
64 /**
65 * @brief Find the nearest macro matching a giving name
66 *
67 * @details Proxy function for MacroProcessor::findNearestMacro
68 *
69 * @param name
70 * @return const Node* nullptr if no macro was found
71 */
72 const Node* findNearestMacro(const std::string& name) const;
73
74 /**
75 * @brief Registers macros based on their type
76 * @details Validate macros and register them by their name
77 * Proxy function for MacroProcessor::registerMacro
78 *
79 * @param node A node of type Macro
80 */
81 void registerMacro(Node& node);
82
83 /**
84 * @brief Check if a node can be evaluated to true
85 * @details Proxy function for MacroProcessor::isTruthy
86 *
87 * @param node
88 * @return true
89 * @return false
90 */
91 bool isTruthy(const Node& node);
92
93 /**
94 * @brief Evaluate only the macros
95 * @details Proxy function for MacroProcessor::evaluate
96 *
97 * @param node
98 * @param is_not_body true if the method is run on a non-body code (eg a condition of an if-macro)
99 * @return Node
100 */
101 Node evaluate(Node& node, bool is_not_body);
102
103 /**
104 * @brief Applies the spread operator
105 * @details Proxy function for MacroProcessor::unify
106 */
107 void unify(const std::unordered_map<std::string, Node>&, Node&, Node*);
108
109 /**
110 * @brief Throw a macro processing error
111 * @details Proxy function for MacroProcessor::throwMacroProcessingError
112 *
113 * @param message the error
114 * @param node the node in which there is an error
115 */
116 [[noreturn]] void throwMacroProcessingError(const std::string& message, const Node& node);
117
118 /**
119 * @brief Execute a node, trying to emplace macros calls
120 * @details Proxy function for MacroProcessor::applyMacro
121 *
122 * @param node
123 * @return true
124 * @return false
125 */
126 bool applyMacroProxy(Node& node);
127
128 /**
129 * @brief Check if a given symbol is a predefined macro
130 * @details Proxy function for MacroProcessor::isPredefined
131 *
132 * @param symbol
133 * @return true
134 * @return false
135 */
136 bool isPredefined(const std::string& symbol);
137 };
138
139}
140
141#endif
AST node used by the parser, optimizer and compiler.
A class that applies macros in a Node.
Definition: Executor.hpp:28
Node evaluate(Node &node, bool is_not_body)
Evaluate only the macros.
Definition: Executor.cpp:30
MacroProcessor * m_macroprocessor
Definition: Executor.hpp:62
virtual bool canHandle(Node &node)=0
Checks if the executor can apply a macro on the passed Node.
bool isTruthy(const Node &node)
Check if a node can be evaluated to true.
Definition: Executor.cpp:25
void throwMacroProcessingError(const std::string &message, const Node &node)
Throw a macro processing error.
Definition: Executor.cpp:40
virtual bool applyMacro(Node &node)=0
Executes macros in the Node if the Executor can handle it.
virtual ~MacroExecutor()
Need a virtual destructor to correctly destory object.
Definition: Executor.cpp:12
void registerMacro(Node &node)
Registers macros based on their type.
Definition: Executor.cpp:20
void unify(const std::unordered_map< std::string, Node > &, Node &, Node *)
Applies the spread operator.
Definition: Executor.cpp:35
bool isPredefined(const std::string &symbol)
Check if a given symbol is a predefined macro.
Definition: Executor.cpp:50
const Node * findNearestMacro(const std::string &name) const
Find the nearest macro matching a giving name.
Definition: Executor.cpp:15
bool applyMacroProxy(Node &node)
Execute a node, trying to emplace macros calls.
Definition: Executor.cpp:45
The class handling the macros definitions and calls, given an AST.
Definition: Processor.hpp:31
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:29