A simple scripting language in C++
Szontágh Ferenc
2025-04-19 571a9a1bafe0d8adddf3141f82213a47e4568baa
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
 #ifndef INTERPRETER_CONDITIONAL_STATEMENT_NODE_HPP
 #define INTERPRETER_CONDITIONAL_STATEMENT_NODE_HPP
 
 #include <vector>
 #include <memory>
 #include <string>
#include "Interpreter/StatementNode.hpp"
// Include for unified runtime Exception
#include "Interpreter/Interpreter.hpp"
#include "Interpreter/ExpressionNode.hpp"
 
 namespace Interpreter {
 
 /**
  * @brief Statement node representing an if-else conditional block.
  */
 class ConditionalStatementNode : public StatementNode {
     std::unique_ptr<ExpressionNode> condition_;
     std::vector<std::unique_ptr<StatementNode>> thenBranch_;
     std::vector<std::unique_ptr<StatementNode>> elseBranch_;
 
   public:
     ConditionalStatementNode(
         std::unique_ptr<ExpressionNode> condition,
         std::vector<std::unique_ptr<StatementNode>> thenBranch,
         std::vector<std::unique_ptr<StatementNode>> elseBranch,
         const std::string & file_name,
         int line,
         size_t column
     ) : StatementNode(file_name, line, column),
         condition_(std::move(condition)),
         thenBranch_(std::move(thenBranch)),
         elseBranch_(std::move(elseBranch)) {}
 
     void interpret(class Interpreter & interpreter) const override {
         try {
             auto val = condition_->evaluate(interpreter);
             bool cond = false;
             if (val.getType() == Symbols::Variables::Type::BOOLEAN) {
                 cond = val.get<bool>();
             } else {
                 throw Exception("Condition did not evaluate to boolean", filename_, line_, column_);
             }
             const auto & branch = cond ? thenBranch_ : elseBranch_;
             for (const auto & stmt : branch) {
                 stmt->interpret(interpreter);
             }
         } catch (const Exception &) {
             throw;
         } catch (const std::exception &e) {
             throw Exception(e.what(), filename_, line_, column_);
         }
     }
 
     std::string toString() const override {
         return "ConditionalStatementNode at " + filename_ + ":" + std::to_string(line_);
     }
 };
 
 } // namespace Interpreter
 
 #endif // INTERPRETER_CONDITIONAL_STATEMENT_NODE_HPP