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
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
 #ifndef INTERPRETER_CALL_EXPRESSION_NODE_HPP
 #define INTERPRETER_CALL_EXPRESSION_NODE_HPP
 
 #include <string>
 #include <vector>
 #include <memory>
 
 #include "Interpreter/ExpressionNode.hpp"
 #include "Interpreter/Interpreter.hpp"
 #include "Interpreter/ReturnException.hpp"
 #include "Interpreter/OperationContainer.hpp"
 #include "Interpreter/Operation.hpp"
 #include "Symbols/SymbolContainer.hpp"
 #include "Symbols/SymbolFactory.hpp"
 #include "Symbols/FunctionSymbol.hpp"
 #include "Symbols/Value.hpp"
 #include "Modules/ModuleManager.hpp"
 
 namespace Interpreter {
 
 /**
  * @brief Expression node representing a function call returning a value.
  */
 class CallExpressionNode : public ExpressionNode {
     std::string functionName_;
     std::vector<std::unique_ptr<ExpressionNode>> args_;
 
   public:
     CallExpressionNode(std::string functionName,
                        std::vector<std::unique_ptr<ExpressionNode>> args) :
         functionName_(std::move(functionName)), args_(std::move(args)) {}
 
     Symbols::Value evaluate(Interpreter &interpreter) const override {
         using namespace Symbols;
         // Evaluate argument expressions
         std::vector<Value> argValues;
         argValues.reserve(args_.size());
         for (const auto &expr : args_) {
             argValues.push_back(expr->evaluate(interpreter));
         }
 
         // Built-in function
         auto &mgr = Modules::ModuleManager::instance();
         if (mgr.hasFunction(functionName_)) {
             return mgr.callFunction(functionName_, argValues);
         }
 
        // User-defined function: lookup through scope hierarchy
        SymbolContainer *sc = SymbolContainer::instance();
        std::string lookupNs = sc->currentScopeName();
        std::shared_ptr<FunctionSymbol> funcSym;
        // Search for function symbol in current and parent scopes
        while (true) {
            std::string fnSymNs = lookupNs + ".functions";
            auto sym = sc->get(fnSymNs, functionName_);
            if (sym && sym->getKind() == Kind::Function) {
                funcSym = std::static_pointer_cast<FunctionSymbol>(sym);
                break;
            }
            auto pos = lookupNs.find_last_of('.');
            if (pos == std::string::npos) {
                break;
            }
            lookupNs = lookupNs.substr(0, pos);
        }
        if (!funcSym) {
            throw std::runtime_error("Function not found: " + functionName_);
        }
        const auto &params = funcSym->parameters();
        if (params.size() != argValues.size()) {
            throw std::runtime_error(
                "Function '" + functionName_ + "' expects " + std::to_string(params.size()) +
                " args, got " + std::to_string(argValues.size()));
        }
        // Enter function scope and bind parameters
        const std::string fnOpNs = funcSym->context() + "." + functionName_;
        sc->enter(fnOpNs);
        for (size_t i = 0; i < params.size(); ++i) {
            const auto &p = params[i];
            const Value &v = argValues[i];
            auto varSym = SymbolFactory::createVariable(p.name, v, fnOpNs);
            sc->add(varSym);
        }
        // Execute function body operations and capture return
        Symbols::Value returnValue;
        auto ops = Operations::Container::instance()->getAll(fnOpNs);
        for (const auto &op : ops) {
            try {
                interpreter.runOperation(*op);
            } catch (const ReturnException &ret) {
                returnValue = ret.value();
                break;
            }
        }
        sc->enterPreviousScope();
        return returnValue;
     }
 
     std::string toString() const override {
         return "CallExpressionNode{ function='" + functionName_ + "', args=" + std::to_string(args_.size()) + " }";
     }
 };
 
} // namespace Interpreter
 
 #endif // INTERPRETER_CALL_EXPRESSION_NODE_HPP