A simple scripting language in C++
Ferenc Szontágh
2025-04-19 bc2e09a3b7a4e414814b56be71ec5c540b8eb4d9
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
#ifndef IDENTIFIER_EXPRESSION_NODE_HPP
#define IDENTIFIER_EXPRESSION_NODE_HPP
 
#include "ExpressionNode.hpp"
#include "Symbols/SymbolContainer.hpp"
#include "Symbols/Value.hpp"
 
namespace Interpreter {
 
class IdentifierExpressionNode : public ExpressionNode {
    std::string name_;
 
  public:
    explicit IdentifierExpressionNode(std::string name) : name_(std::move(name)) {}
 
    Symbols::Value evaluate(Interpreter & /*interpreter*/) const override {
        auto * sc = Symbols::SymbolContainer::instance();
        const std::string base_ns  = sc->currentScopeName();
        const std::string var_ns   = base_ns + ".variables";
        if (sc->exists(name_, var_ns)) {
            return sc->get(var_ns, name_)->getValue();
        }
        const std::string const_ns = base_ns + ".constants";
        if (sc->exists(name_, const_ns)) {
            return sc->get(const_ns, name_)->getValue();
        }
        throw std::runtime_error("Identifier '" + name_ + "' not found in namespace: " + base_ns);
    }
 
    std::string toString() const override { return name_; }
};
 
}  // namespace Interpreter
 
#endif  // IDENTIFIER_EXPRESSION_NODE_HPP