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]), Alexandre Plateau ([email protected])
4 * @brief The base class for all MacroExecutors
5 * @date 2024-03-03
6 *
7 * @copyright Copyright (c) 2021-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_EXECUTOR_HPP
12#define ARK_COMPILER_EXECUTOR_HPP
13
14#include <optional>
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 processor
34 * @param debug
35 */
36 explicit MacroExecutor(MacroProcessor* processor, unsigned debug = 0);
37
38 /**
39 * @brief Need a virtual destructor to correctly destroy object.
40 *
41 */
42 virtual ~MacroExecutor() = default;
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 * @param depth
49 * @return true if a macro was successfully applied
50 * @return false
51 */
52 virtual bool applyMacro(Node& node, unsigned depth) = 0;
53
54 /**
55 * @brief Checks if the executor can apply a macro on the passed Node
56 *
57 * @param node the node that contains a macro
58 */
59 virtual bool canHandle(Node& node) = 0;
60
61 /**
62 * @brief Returns the macro node that will be expanded
63 *
64 * @param node AST node on which the executor will run
65 * @return Node
66 */
67 virtual Node macroNode(Node& node) = 0;
68
69 protected:
70 unsigned int m_debug;
71 MacroProcessor* m_processor; ///< This is a non-owned pointer.
72
73 /**
74 * @brief Find the nearest macro matching a giving name
75 *
76 * @details Proxy function for MacroProcessor::findNearestMacro
77 *
78 * @param name
79 * @return const Node* nullptr if no macro was found
80 */
81 [[nodiscard]] const Node* findNearestMacro(const std::string& name) const;
82
83 /**
84 * @brief Apply a macro on a given node
85 * @details Proxy function for MacroProcessor::applyMacro
86 *
87 * @param node
88 * @param depth
89 */
90 void applyMacroProxy(Node& node, unsigned depth);
91
92 /**
93 * @brief Registers macros based on their type, expand conditional macros
94 * @details Validate macros and register them by their name
95 * Proxy function for MacroProcessor::handleMacroNode
96 *
97 * @param node A node of type Macro
98 */
99 void handleMacroNode(Node& node) const;
100
101 /**
102 * @brief Check if a node can be evaluated to true
103 * @details Proxy function for MacroProcessor::isTruthy
104 *
105 * @param node
106 * @return true
107 * @return false
108 */
109 [[nodiscard]] bool isTruthy(const Node& node) const;
110
111 /**
112 * @brief Evaluate only the macros
113 * @details Proxy function for MacroProcessor::evaluate
114 *
115 * @param node
116 * @param depth
117 * @param is_not_body true if the method is run on a non-body code (eg a condition of an if-macro)
118 * @return Node
119 */
120 Node evaluate(Node& node, unsigned depth, bool is_not_body) const;
121
122 /**
123 * @brief Throw a macro processing error
124 * @details Proxy function for MacroProcessor::throwMacroProcessingError
125 *
126 * @param message the error
127 * @param node the node in which there is an error
128 */
129 [[noreturn]] void throwMacroProcessingError(const std::string& message, const Node& node);
130 };
131
132}
133
134#endif
AST node used by the parser, optimizer and compiler.
A class that applies macros in a Node.
Definition Executor.hpp:28
void applyMacroProxy(Node &node, unsigned depth)
Apply a macro on a given node.
Definition Executor.cpp:17
virtual Node macroNode(Node &node)=0
Returns the macro node that will be expanded.
virtual bool canHandle(Node &node)=0
Checks if the executor can apply a macro on the passed Node.
MacroExecutor(MacroProcessor *processor, unsigned debug=0)
Construct a new Macro Executor object.
Definition Executor.cpp:7
virtual bool applyMacro(Node &node, unsigned depth)=0
Executes macros in the Node if the Executor can handle it.
virtual ~MacroExecutor()=default
Need a virtual destructor to correctly destroy object.
void throwMacroProcessingError(const std::string &message, const Node &node)
Throw a macro processing error.
Definition Executor.cpp:37
MacroProcessor * m_processor
This is a non-owned pointer.
Definition Executor.hpp:71
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
const Node * findNearestMacro(const std::string &name) const
Find the nearest macro matching a giving name.
Definition Executor.cpp:12
The class handling the macros definitions and calls, given an AST.
Definition Processor.hpp:32
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:30