A simple scripting language in C++
Ferenc Szontágh
2025-04-18 e398a47fc9ef8ded356c56b5739808ce1192e3d2
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
#ifndef INTERPRETER_OPERATION_CONTAINER_HPP
#define INTERPRETER_OPERATION_CONTAINER_HPP
 
#include <map>
#include <string>
#include <vector>
 
#include "Interpreter/Operation.hpp"
 
namespace Operations {
 
class Container {
 
  public:
    /**
     * @brief Get the Operations::Container instance.
     * @return The Operations::Container instance.
     */
    static Operations::Container * instance() {
        static Operations::Container instance_;
        return &instance_;
    }
 
    Container() = default;
 
    void add(const std::string & ns, Operations::Operation operation) {
        this->_operations[ns].emplace_back(std::make_shared<Operations::Operation>(std::move(operation)));
    }
 
    /**
    * @brief Returns the first operation in the namespace.
    * @param ns Namespace from which to get the operation.
    * @return The first operation in the namespace.
    */
    std::shared_ptr<Operations::Operation> getFirst(const std::string & ns) {
        auto it = _operations.find(ns);
        if (it != _operations.end()) {
            return it->second.front();
        }
        return nullptr;
    }
 
    /**
     * @brief Removes the first operation from the namespace.
     * @param ns Namespace from which to remove the operation.
     * @return The removed operation.
     */
    std::shared_ptr<Operations::Operation> pullFirst(const std::string & ns) {
        auto it = _operations.find(ns);
        if (it != _operations.end()) {
            auto operation = it->second.front();
            it->second.erase(it->second.begin());
            return operation;
        }
        return nullptr;
    }
 
    /**
     * @brief Removes the last operation from the namespace.
     * @param ns Namespace from which to remove the operation.
     * @return The removed operation.
     */
    std::shared_ptr<Operations::Operation> pullLast(const std::string & ns) {
        auto it = _operations.find(ns);
        if (it != _operations.end()) {
            auto operation = it->second.back();
            it->second.pop_back();
            return operation;
        }
        return nullptr;
    }
 
    /**
    * @brief Returns the last operation in the namespace.
    * @param ns Namespace from which to get the operation.
    * @return The last operation in the namespace.
    */
    std::shared_ptr<Operations::Operation> getLast(const std::string & ns) {
        auto it = _operations.find(ns);
        if (it != _operations.end()) {
            return it->second.back();
        }
        return nullptr;
    }
 
    /**
     * @brief Returns all operations in the namespace.
     * @param ns Namespace from which to get the operations.
     * @return All operations in the namespace.
     */
    std::vector<std::shared_ptr<Operations::Operation>> getAll(const std::string & ns) {
        auto it = _operations.find(ns);
        if (it != _operations.end()) {
            return it->second;
        }
        return {};
    }
 
    /**
     * @brief Returns all operations from all namespaces
     * @return All operations in the namespace.
     */
    std::vector<std::shared_ptr<Operations::Operation>> getAll() {
        std::vector<std::shared_ptr<Operations::Operation>> result;
        for (const auto & [_, table] : _operations) {
            result.insert(result.end(), table.begin(), table.end());
        }
        return result;
    }
 
    auto begin() { return _operations.begin(); }
 
    auto end() { return _operations.end(); }
 
    auto begin() const { return _operations.begin(); }
 
    auto end() const { return _operations.end(); }
 
    static std::string dump()  {
        std::string result;
        for (const auto & [_, table] : Operations::Container::instance()->_operations) {
            result += "Namespace: " + _ + "\n";
            for (const auto & operation : table) {
                result += "  Operation: " + operation->toString() + "\n";
            }
        }
        return result;
    }
 
  private:
    std::map<std::string, std::vector<std::shared_ptr<Operations::Operation>>> _operations;
};  // class Container
};  // namespace Operations
 
#endif  // INTERPRETER_OPERATION_CONTAINER_HPP