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