A simple scripting language in C++
Ferenc Szontágh
2025-04-18 9a186053a690f2216b43355549c8aa7e2959583c
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
// ReturnStatementNode.hpp
#ifndef INTERPRETER_RETURN_STATEMENT_NODE_HPP
#define INTERPRETER_RETURN_STATEMENT_NODE_HPP
 
#include <memory>
#include <string>
#include "StatementNode.hpp"
#include "ExpressionNode.hpp"
#include "ReturnException.hpp"
#include "Symbols/Value.hpp"
 
namespace Interpreter {
 
/**
 * @brief Statement node representing a return statement inside a function.
 */
class ReturnStatementNode : public StatementNode {
    std::unique_ptr<ExpressionNode> expr_;
  public:
    explicit ReturnStatementNode(std::unique_ptr<ExpressionNode> expr,
                                 const std::string &file_name,
                                 int line,
                                 size_t column) :
        StatementNode(file_name, line, column), expr_(std::move(expr)) {}
 
    void interpret(Interpreter &interpreter) const override {
        Symbols::Value retVal;
        if (expr_) {
            retVal = expr_->evaluate(interpreter);
        }
        throw ReturnException(retVal);
    }
 
    std::string toString() const override {
        return std::string("return") + (expr_ ? (" " + expr_->toString()) : std::string());
    }
};
 
} // namespace Interpreter
#endif // INTERPRETER_RETURN_STATEMENT_NODE_HPP