A simple scripting language in C++
Ferenc Szontágh
2025-04-19 48d9278f0b75098e83e58c589ea86d006358604d
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
#ifndef INTERPRETER_OPERATION_HPP
#define INTERPRETER_OPERATION_HPP
 
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
 
#include "StatementNode.hpp"
 
namespace Operations {
enum class Type : std::uint8_t {
    Assignment,       // Variable assignment, e.g., $x = 5
    Expression,       // Evaluation of an expression (can be evaluated without side effects)
    FunctionCall,     // Call to a function, e.g., print(x)
    FuncDeclaration,  // declaration of new function
    Return,           // return statement
    Conditional,      // if/else structure
    Loop,             // while/for loop
    Break,            // break out of a loop
    Continue,         // continue with the next iteration of a loop
    Block,            // block of statements, e.g. { ... }
    Declaration,      // declaration of new variable (if different from assignment) int $x = 1
    Import,           // import of another script or module
    Error,            // error or non-interpretable operation (error handling)
};
 
struct Operation {
    Operation() = default;
    Operations::Type type;
 
    // Általános mezők
    std::string                                 targetName;
    std::unique_ptr<Interpreter::StatementNode> statement;
 
    Operation(Operations::Type t, std::string target_variable, std::unique_ptr<Interpreter::StatementNode> stmt) :
        type(t),
        targetName(std::move(target_variable)),
        statement(std::move(stmt)) {}
 
    std::string typeToString() const {
        std::unordered_map<Operations::Type, std::string> types = {
            { Operations::Type::Assignment,      "Assignment"      },
            { Operations::Type::Expression,      "Expression"      },
            { Operations::Type::FunctionCall,    "FunctionCall"    },
            { Operations::Type::FuncDeclaration, "FuncDeclaration" },
            { Operations::Type::Return,          "Return"          },
            { Operations::Type::Conditional,     "Conditional"     },
            { Operations::Type::Loop,            "Loop"            },
            { Operations::Type::Break,           "Break"           },
            { Operations::Type::Continue,        "Continue"        },
            { Operations::Type::Block,           "Block"           },
            { Operations::Type::Declaration,     "Declaration"     },
            { Operations::Type::Import,          "Import"          },
            { Operations::Type::Error,           "Error"           }
        };
        auto it = types.find(type);
        if (it != types.end()) {
            return it->second;
        }
        return "Unknown type";
    }
 
    std::string toString() const {
        return "Target: " + targetName + " Type: " + this->typeToString() +
               " Statement: " + ((statement == nullptr) ? "no statement" : statement->toString());
    }
};
};  // namespace Operations
#endif