A simple scripting language in C++
Ferenc Szontágh
2025-04-18 4abeb5f8a6ad77b32496f3e8b20e1fd1b6f428fb
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
#ifndef SYMBOL_TABLE_HPP
#define SYMBOL_TABLE_HPP
 
#include <vector>
#include <string>
#include "SymbolTypes.hpp"
 
namespace Symbols {
 
class SymbolTable {
    NamespaceMap symbols_;
 
  public:
    void define(const std::string & ns, const SymbolPtr & symbol) { symbols_[ns][symbol->name()] = symbol; }
 
    bool exists(const std::string & ns, const std::string & name) const { return get(ns, name) != nullptr; }
 
    SymbolPtr get(const std::string & ns, const std::string & name) const {
        auto itNs = symbols_.find(ns);
        if (itNs != symbols_.end()) {
            const auto & map = itNs->second;
            auto         it  = map.find(name);
            if (it != map.end()) {
                return it->second;
            }
        }
        return nullptr;
    }
 
    void remove(const std::string & ns, const std::string & name) {
        auto itNs = symbols_.find(ns);
        if (itNs != symbols_.end()) {
            itNs->second.erase(name);
        }
    }
 
    std::vector<std::string> listNSs() {
        std::vector<std::string> result;
        for (const auto & [ns, _] : symbols_) {
            result.push_back(ns);
        }
        return result;
    }
 
    std::vector<SymbolPtr> listAll(const std::string & prefix = "") const {
        std::vector<SymbolPtr> result;
        for (const auto & [ns, map] : symbols_) {
            if (prefix.empty() || ns.substr(0,prefix.length()) == prefix) {
                for (const auto & [_, sym] : map) {
                    result.push_back(sym);
                }
            }
        }
        return result;
    }
 
    void clear(const std::string & ns) { symbols_.erase(ns); }
 
    void clearAll() { symbols_.clear(); }
};
 
}  // namespace Symbols
 
#endif