A simple scripting language in C++
Ferenc Szontágh
2025-04-18 9a186053a690f2216b43355549c8aa7e2959583c
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
#ifndef INTERPRETER_HPP
#define INTERPRETER_HPP
#include <iostream>
#include <memory>
#include <stdexcept>
 
#include "Interpreter/Operation.hpp"
#include "Interpreter/OperationContainer.hpp"
#include "Symbols/SymbolContainer.hpp"
#include "BaseException.hpp"
 
// Exception type for runtime errors, includes file, line, and column context
namespace Interpreter {
class Exception : public BaseException {
public:
    Exception(const std::string &msg, const std::string &filename, int line, size_t column) {
        rawMessage_ = msg;
        context_ = std::string(" in file \"") + filename + "\" at line: " + std::to_string(line)
                   + ", column: " + std::to_string(column);
        formattedMessage_ = formatMessage();
    }
    std::string formatMessage() const override {
        return std::string("[Runtime ERROR] >>") + context_ + " << : " + rawMessage_;
    }
};
} // namespace Interpreter
 
namespace Interpreter {
 
class Interpreter {
  private:
    bool debug_ = false;
  public:
    /**
     * @brief Construct interpreter with optional debug output
     * @param debug enable interpreter debug output
     */
    Interpreter(bool debug = false) : debug_(debug) {}
 
    /**
     * @brief Execute all operations in the current namespace (e.g., file-level or function-level).
     */
    void run() {
        // Determine namespace to execute
        const std::string ns = Symbols::SymbolContainer::instance()->currentScopeName();
        for (const auto & operation : Operations::Container::instance()->getAll(ns)) {
            runOperation(*operation);
        }
    }
 
    void runOperation(const Operations::Operation & op) {
        if (debug_) {
            std::cerr << "[Debug][Interpreter] Operation: " << op.toString() << "\n";
        }
 
        switch (op.type) {
            case Operations::Type::Declaration:
                if (op.statement) {
                    op.statement->interpret(*this);
                }
                break;
            case Operations::Type::FuncDeclaration:
                {
                    op.statement->interpret(*this);
                }
                break;
 
            case Operations::Type::Assignment:
                {
                    op.statement->interpret(*this);
                    break;
                }
 
            case Operations::Type::Expression:
                {
                    op.statement->interpret(*this);  // csak side effect miatt
                    break;
                }
 
            case Operations::Type::FunctionCall:
                if (op.statement) {
                    op.statement->interpret(*this);
                }
                break;
            case Operations::Type::Conditional:
                // if-else conditional block
                if (op.statement) {
                    op.statement->interpret(*this);
                }
                break;
            case Operations::Type::Return:
                // return statement
                if (op.statement) {
                    op.statement->interpret(*this);
                }
                break;
            case Operations::Type::Loop:
                // for-in or while loop
                if (op.statement) {
                    op.statement->interpret(*this);
                }
                break;
            case Operations::Type::Break:
            case Operations::Type::Continue:
            case Operations::Type::Block:
            case Operations::Type::Import:
            case Operations::Type::Error:
                // TODO: implement these operations later
                break;
            default:
                throw std::runtime_error("Not implemented operation type");
        }
    }
 
};  // class Interpreter
 
}  // namespace Interpreter
#endif  // INTERPRETER_HPP