A simple scripting language in C++
Ferenc Szontágh
2025-04-19 d729312dde311aa6c5e9a2008461c04e98ab4ea1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef SYMBOL_CONTAINER_HPP
#define SYMBOL_CONTAINER_HPP
 
#include <iostream>
#include <memory>
#include <stdexcept>
#include <unordered_map>
#include <vector>
 
#include "SymbolTable.hpp"
 
#define NSMGR Symbols::SymbolContainer::instance()
 
namespace Symbols {
 
class SymbolContainer {
    std::unordered_map<std::string, std::shared_ptr<SymbolTable>> scopes_;
    // Stack of active scope names (supports nested scope entry)
    std::vector<std::string>                                        scopeStack_;
 
  public:
    static SymbolContainer * instance() {
        static SymbolContainer instance_;
        return &instance_;
    }
 
    explicit SymbolContainer(const std::string & default_scope_name = "global") { create(default_scope_name); }
 
    // --- Scope management ---
 
    /**
     * @brief Create a new scope and enter it.
     * @param name Name of the new scope.
     */
    void create(const std::string & name) {
        scopes_[name] = std::make_shared<SymbolTable>();
        scopeStack_.push_back(name);
    }
 
    /**
     * @brief Enter an existing scope.
     * @param name Name of the scope to enter.
     */
    void enter(const std::string & name) {
        auto it = scopes_.find(name);
        if (it != scopes_.end()) {
            scopeStack_.push_back(name);
        } else {
            throw std::runtime_error("Scope does not exist: " + name);
        }
    }
 
    /**
     * @brief Exit the current scope, returning to the previous one.
     */
    void enterPreviousScope() {
        if (scopeStack_.size() > 1) {
            scopeStack_.pop_back();
        }
    }
 
    /**
     * @brief Get the name of the current scope.
     * @return Current scope name.
     */
    [[nodiscard]] std::string currentScopeName() const { return scopeStack_.empty() ? std::string() : scopeStack_.back(); }
 
    std::vector<std::string> getScopeNames() const {
        std::vector<std::string> result;
        result.reserve(scopes_.size());
        for (const auto & [scopeName, _] : scopes_) {
            result.push_back(scopeName);
        }
        return result;
    }
 
    // --- Symbol operations ---
 
    /**
     * @brief Add a symbol to the current scope.
     * @param symbol Symbol to add.
     * @return Namespace under which the symbol was defined.
     */
    std::string add(const SymbolPtr & symbol) {
        const std::string ns = getNamespaceForSymbol(symbol);
        scopes_[currentScopeName()]->define(ns, symbol);
        return ns;
    }
 
    std::vector<std::string> getNameSpaces(const std::string & scopeName) const {
        std::vector<std::string> result;
        auto                     it = scopes_.find(scopeName);
        if (it != scopes_.end()) {
            return it->second->listNSs();
        }
        return result;
    }
 
    std::vector<SymbolPtr> getAll(const std::string & ns = "") const {
        std::vector<SymbolPtr> result;
        for (const auto & [_, table] : scopes_) {
            auto symbols = ns.empty() ? table->listAll() : table->listAll(ns);
            result.insert(result.end(), symbols.begin(), symbols.end());
        }
        return result;
    }
 
    /**
     * @brief Check if a symbol exists in the given namespace (or current scope if none provided).
     * @param name Symbol name.
     * @param fullNamespace Namespace to search within (defaults to current scope).
     * @return True if the symbol exists, false otherwise.
     */
    bool exists(const std::string & name, std::string fullNamespace = "") const {
        if (fullNamespace.empty()) {
            fullNamespace = currentScopeName();
        }
 
        for (const auto & [_, table] : scopes_) {
            if (table->exists(fullNamespace, name)) {
                return true;
            }
        }
        return false;
    }
 
    SymbolPtr get(const std::string & fullNamespace, const std::string & name) const {
        for (const auto & [_, table] : scopes_) {
            auto sym = table->get(fullNamespace, name);
            if (sym) {
                return sym;
            }
        }
        return nullptr;
    }
 
    static std::string dump() {
        std::string result;
 
        std::cout << "\n--- Defined Scopes ---" << '\n';
        for (const auto & scope_name : instance()->getScopeNames()) {
            result += scope_name + '\n';
            for (const auto & sname : instance()->getNameSpaces(scope_name)) {
                result += "\t -" + sname + '\n';
                for (const auto & symbol : instance()->getAll(sname)) {
                    result += symbol->dump() + '\n';
                }
            }
        }
        return result;
    }
 
  private:
    /**
     * @brief Compute the namespace string for a symbol based on its kind and context.
     */
    std::string getNamespaceForSymbol(const SymbolPtr & symbol) const {
        std::string base = symbol->context().empty() ? currentScopeName() : symbol->context();
 
        switch (symbol->getKind()) {
            case Symbols::Kind::Variable:
                return base + ".variables";
            case Symbols::Kind::Function:
                return base + ".functions";
            case Symbols::Kind::Constant:
                return base + ".constants";
            default:
                return base + ".others";
        }
    }
};
 
}  // namespace Symbols
 
#endif