ArkScript
A small, fast, functional and scripting language for video games
MacroScope.hpp
Go to the documentation of this file.
1/**
2 * @file MacroScope.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Defines tools to handle macro definitions
5 * @date 2023-02-18
6 *
7 * @copyright Copyright (c) 2023-2025
8 *
9 */
10
11#ifndef COMPILER_MACROS_SCOPE_HPP
12#define COMPILER_MACROS_SCOPE_HPP
13
15
16#include <unordered_map>
17#include <string>
18
19namespace Ark::internal
20{
22 {
23 public:
24 /**
25 * @brief Construct a new MacroScope object given a depth in the scope hierarchy
26 *
27 * @param depth
28 */
29 explicit MacroScope(unsigned int depth);
30
31 /**
32 * @brief Check if the current scope holds a value for a given symbol, and returns it as a pointer
33 *
34 * @param name
35 * @return Node* pointer to the value if found, nullptr otherwise
36 */
37 [[nodiscard]] const Node* has(const std::string& name) const;
38
39 /**
40 * @brief Add a new entry in the scope
41 *
42 * @param name
43 * @param node
44 */
45 void add(const std::string& name, const Node& node);
46
47 /**
48 * @brief Remove a macro in the scope, if it exists
49 *
50 * @param name
51 * @return true if the value was found and removed
52 * @return false otherwise
53 */
54 bool remove(const std::string& name);
55
56 /**
57 * @brief Return true if the current scope is empty
58 *
59 * @return true
60 * @return false
61 */
62 [[nodiscard]] bool empty() const
63 {
64 return m_macros.empty();
65 }
66
67 [[nodiscard]] unsigned int depth() const
68 {
69 return m_depth;
70 }
71
72 private:
73 std::unordered_map<std::string, Node> m_macros;
74 unsigned int m_depth { 0 };
75 };
76}
77
78#endif
AST node used by the parser, optimizer and compiler.
bool remove(const std::string &name)
Remove a macro in the scope, if it exists.
const Node * has(const std::string &name) const
Check if the current scope holds a value for a given symbol, and returns it as a pointer.
Definition MacroScope.cpp:9
std::unordered_map< std::string, Node > m_macros
MacroScope(unsigned int depth)
Construct a new MacroScope object given a depth in the scope hierarchy.
Definition MacroScope.cpp:5
unsigned int depth() const
void add(const std::string &name, const Node &node)
Add a new entry in the scope.
bool empty() const
Return true if the current scope is empty.
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:30