A simple scripting language in C++
Ferenc Szontágh
2025-04-15 3895272a7f238c9aef0b584bd3b10b900445245d
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
#ifndef INTERPRETER_HPP
#define INTERPRETER_HPP
#include <memory>
#include <utility>
#include <vector>
 
#include "Interpreter/Operation.hpp"
#include "Interpreter/OperationContainer.hpp"
#include "Symbols/SymbolContainer.hpp"
 
namespace Interpreter {
 
class Interpreter {
  private:
    std::shared_ptr<Symbols::SymbolContainer> symbol_container_;
    const OperationContainer &                operations_conatiner_;
 
    void setVariable(const std::string & name, int value) {
        //symbol_container_->setVariable(name, value);
    }
  public:
    // Constructor takes the populated symbol container from the parser
    Interpreter(std::shared_ptr<Symbols::SymbolContainer> symbols, const OperationContainer & operations) :
        symbol_container_(std::move(symbols)),
        operations_conatiner_(operations) {}
 
    void run(const std::vector<Operation> & ops) {
        for (const auto & op : ops) {
            switch (op.type) {
                case OperationType::Assignment:
                    {
                        int value = op.expression->evaluate(*this);
                        setVariable(op.targetVariable, value);
                        break;
                    }
 
                case OperationType::Expression:
                    {
                        op.expression->evaluate(*this);  // csak side effect miatt
                        break;
                    }
            }
        }
    }
 
};  // class Interpreter
 
}  // namespace Interpreter
#endif  // INTERPRETER_HPP