A simple scripting language in C++
Ferenc Szontágh
2025-04-17 ba9a9199d01b0fdd4bf9a54914f8058bf71f30c5
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
#ifndef PARSEREXPRESSION_BUILDER_HPP
#define PARSEREXPRESSION_BUILDER_HPP
 
#include <memory>
#include <stdexcept>
 
#include "Interpreter/BinaryExpressionNode.hpp"
#include "Interpreter/ExpressionNode.hpp"
#include "Interpreter/IdentifierExpressionNode.hpp"
#include "Interpreter/LiteralExpressionNode.hpp"
#include "Interpreter/UnaryExpressionNode.hpp" // <-- új include
#include "Parser/ParsedExpression.hpp"
 
namespace Parser {
static std::unique_ptr<Interpreter::ExpressionNode> buildExpressionFromParsed(
    const Parser::ParsedExpressionPtr & expr) {
    using Kind = Parser::ParsedExpression::Kind;
 
    switch (expr->kind) {
        case Kind::Literal:
            return std::make_unique<Interpreter::LiteralExpressionNode>(expr->value);
 
        case Kind::Variable:
            return std::make_unique<Interpreter::IdentifierExpressionNode>(expr->name);
 
        case Kind::Binary: {
            auto lhs = buildExpressionFromParsed(expr->lhs);
            auto rhs = buildExpressionFromParsed(expr->rhs);
            return std::make_unique<Interpreter::BinaryExpressionNode>(std::move(lhs), expr->op, std::move(rhs));
        }
 
        case Kind::Unary: {
            auto operand = buildExpressionFromParsed(expr->rhs);  // rhs az operandus
            return std::make_unique<Interpreter::UnaryExpressionNode>(expr->op, std::move(operand));
        }
    }
 
    throw std::runtime_error("Unknown ParsedExpression kind");
}
}  // namespace Parser
 
#endif  // PARSEREXPRESSION_BUILDER_HPP