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 * @version 0.1
6 * @date 2023-02-18
7 *
8 * @copyright Copyright (c) 2023
9 *
10 */
11
12#ifndef COMPILER_MACROS_SCOPE_HPP
13#define COMPILER_MACROS_SCOPE_HPP
14
16
17#include <unordered_map>
18#include <string>
19
20namespace Ark::internal
21{
23 {
24 public:
25 MacroScope();
26
27 /**
28 * @brief Construct a new MacroScope object given a depth in the scope hierarchy
29 *
30 * @param depth
31 */
32 explicit MacroScope(unsigned int depth);
33
34 /**
35 * @brief Check if the current scope holds a value for a given symbol, and returns it as a pointer
36 *
37 * @param name
38 * @return Node* pointer to the value if found, nullptr otherwise
39 */
40 const Node* has(const std::string& name) const;
41
42 /**
43 * @brief Add a new entry in the scope
44 *
45 * @param name
46 * @param node
47 */
48 void add(const std::string& name, const Node& node);
49
50 /**
51 * @brief Remove a macro in the scope, if it exists
52 *
53 * @param name
54 * @return true if the value was found and removed
55 * @return false otherwise
56 */
57 bool remove(const std::string& name);
58
59 /**
60 * @brief Return true if the current scope is empty
61 *
62 * @return true
63 * @return false
64 */
65 inline bool empty() const
66 {
67 return m_macros.empty();
68 }
69
70 inline unsigned int depth() const
71 {
72 return m_depth;
73 }
74
75 private:
76 std::unordered_map<std::string, Node> m_macros;
77 unsigned int m_depth;
78 };
79}
80
81#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.
Definition: MacroScope.cpp:25
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:13
std::unordered_map< std::string, Node > m_macros
Definition: MacroScope.hpp:76
unsigned int depth() const
Definition: MacroScope.hpp:70
void add(const std::string &name, const Node &node)
Add a new entry in the scope.
Definition: MacroScope.cpp:20
bool empty() const
Return true if the current scope is empty.
Definition: MacroScope.hpp:65
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:29