A simple scripting language in C++
Ferenc Szontágh
2025-04-18 3bb117e4fee72c4d4019c575bf0cb21043584954
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
#ifndef INTERPRETER_VARIABLE_REFERENCE_NODE_HPP
#define INTERPRETER_VARIABLE_REFERENCE_NODE_HPP
 
#include <memory>
#include <stdexcept>
#include <string>
 
#include "ExpressionNode.hpp"
#include "Symbols/SymbolContainer.hpp"
#include "Symbols/Value.hpp"
#include "Symbols/VariableSymbol.hpp"
 
namespace Interpreter {
class VariableReferenceNode : public ExpressionNode {
    std::string variableName_;
    std::string namespace_;  // pl. "global.variables"
 
  public:
    VariableReferenceNode(const std::string & variableName, const std::string & ns) :
        variableName_(variableName),
        namespace_(ns) {}
 
    Symbols::Value evaluate(class Interpreter & /*interpreter*/) const override {
        auto symbol = Symbols::SymbolContainer::instance()->get(namespace_, variableName_);
        if (!symbol) {
            throw std::runtime_error("Variable not found: " + variableName_);
        }
        auto varSymbol = std::dynamic_pointer_cast<Symbols::VariableSymbol>(symbol);
        if (!varSymbol) {
            throw std::runtime_error("Symbol is not a variable: " + variableName_);
        }
        return varSymbol->getValue();
    }
};
 
};  // namespace Interpreter
#endif  // INTERPRETER_VARIABLE_REFERENCE_NODE_HPP