A simple scripting language in C++
Ferenc Szontágh
2025-04-18 fcb28cc03f93a4b00ec181a8492432dfa0e5fa11
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
#ifndef INTERPRETER_DEFINE_FUNCTION_STATEMENT_NODE_HPP
#define INTERPRETER_DEFINE_FUNCTION_STATEMENT_NODE_HPP
 
#include <memory>
#include <string>
#include <utility>
 
#include "ExpressionNode.hpp"
#include "Interpreter.hpp"
#include "Interpreter/StatementNode.hpp"
#include "Symbols/ParameterContainer.hpp"
#include "Symbols/SymbolContainer.hpp"
#include "Symbols/SymbolFactory.hpp"
 
namespace Interpreter {
 
class DeclareFunctionStatementNode : public StatementNode {
    std::string                     functionName_;
    Symbols::Variables::Type        returnType_;
    Symbols::FunctionParameterInfo  params_;
    std::unique_ptr<ExpressionNode> expression_;
    std::string                     ns;
 
 
  public:
    DeclareFunctionStatementNode(const std::string & function_name, const std::string & ns,
                                 const Symbols::FunctionParameterInfo & params, Symbols::Variables::Type return_type,
                                 std::unique_ptr<ExpressionNode> expr, const std::string & file_name, int file_line,
                                 size_t line_column) :
        StatementNode(file_name, file_line, line_column),
        functionName_(function_name),
        returnType_(return_type),
        params_(params),
        expression_(std::move(expr)),
        ns(ns) {}
 
    void interpret(Interpreter & /*interpreter*/) const override {
        //Symbols::Value value = expression_->evaluate(interpreter);
        if (Symbols::SymbolContainer::instance()->exists(functionName_)) {
            throw std::runtime_error("Function already declared: " + functionName_ + " file: " + filename_ +
                                     ", line: " + std::to_string(line_) + ", column: " + std::to_string(column_));
        }
        const auto func = Symbols::SymbolFactory::createFunction(functionName_, ns, params_, "", returnType_);
        Symbols::SymbolContainer::instance()->add(func);
    }
 
    std::string toString() const override {
        return std::string( " FunctioName: " + functionName_ + " return type: " + Symbols::Variables::TypeToString(returnType_) +
               " params size: " + std::to_string(params_.size()));
    }
};
 
}  // namespace Interpreter
 
#endif  // INTERPRETER_DEFINE_FUNCTION_STATEMENT_NODE_HPP