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 1.0
6 * @date 2023-02-18
7 *
8 * @copyright Copyright (c) 2023-2024
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 /**
26 * @brief Construct a new MacroScope object given a depth in the scope hierarchy
27 *
28 * @param depth
29 */
30 explicit MacroScope(unsigned int depth);
31
32 /**
33 * @brief Check if the current scope holds a value for a given symbol, and returns it as a pointer
34 *
35 * @param name
36 * @return Node* pointer to the value if found, nullptr otherwise
37 */
38 [[nodiscard]] const Node* has(const std::string& name) const;
39
40 /**
41 * @brief Add a new entry in the scope
42 *
43 * @param name
44 * @param node
45 */
46 void add(const std::string& name, const Node& node);
47
48 /**
49 * @brief Remove a macro in the scope, if it exists
50 *
51 * @param name
52 * @return true if the value was found and removed
53 * @return false otherwise
54 */
55 bool remove(const std::string& name);
56
57 /**
58 * @brief Return true if the current scope is empty
59 *
60 * @return true
61 * @return false
62 */
63 [[nodiscard]] bool empty() const
64 {
65 return m_macros.empty();
66 }
67
68 [[nodiscard]] unsigned int depth() const
69 {
70 return m_depth;
71 }
72
73 private:
74 std::unordered_map<std::string, Node> m_macros;
75 unsigned int m_depth { 0 };
76 };
77}
78
79#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:31