A simple scripting language in C++
Ferenc Szontágh
2025-04-19 55abb4f6f81fc370e349385b38dffb05fa9d5dcb
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
 #ifndef INTERPRETER_CSTYLEFORSTATEMENTNODE_HPP
 #define INTERPRETER_CSTYLEFORSTATEMENTNODE_HPP
 
 #include <vector>
 #include <memory>
 #include <string>
 #include "Interpreter/StatementNode.hpp"
 #include "Interpreter/Interpreter.hpp"
 #include "Interpreter/ExpressionNode.hpp"
 #include "Symbols/Value.hpp"
 
 namespace Interpreter {
 
 /**
  * @brief Statement node representing a C-style for loop: for(init; cond; incr) { body }
  */
 class CStyleForStatementNode : public StatementNode {
  private:
    std::unique_ptr<StatementNode> initStmt_;
    std::unique_ptr<ExpressionNode> condExpr_;
    std::unique_ptr<StatementNode> incrStmt_;
    std::vector<std::unique_ptr<StatementNode>> body_;
 
  public:
    CStyleForStatementNode(std::unique_ptr<StatementNode> initStmt,
                           std::unique_ptr<ExpressionNode> condExpr,
                           std::unique_ptr<StatementNode> incrStmt,
                           std::vector<std::unique_ptr<StatementNode>> body,
                           const std::string & file_name,
                           int line,
                           size_t column)
      : StatementNode(file_name, line, column),
        initStmt_(std::move(initStmt)),
        condExpr_(std::move(condExpr)),
        incrStmt_(std::move(incrStmt)),
        body_(std::move(body)) {}
 
    void interpret(Interpreter & interpreter) const override {
        try {
            using namespace Symbols;
            // Initialization
            initStmt_->interpret(interpreter);
            // Loop condition and body
            while (true) {
                Value condVal = condExpr_->evaluate(interpreter);
                if (condVal.getType() != Variables::Type::BOOLEAN) {
                    throw Exception("For loop condition not boolean", filename_, line_, column_);
                }
                if (!condVal.get<bool>()) break;
                for (const auto & stmt : body_) {
                    stmt->interpret(interpreter);
                }
                // Increment step
                incrStmt_->interpret(interpreter);
            }
        } catch (const Exception &) {
            throw;
        } catch (const std::exception & e) {
            throw Exception(e.what(), filename_, line_, column_);
        }
    }
 
    std::string toString() const override {
        return "CStyleForStatementNode at " + filename_ + ":" + std::to_string(line_);
    }
};
 
} // namespace Interpreter
 
#endif // INTERPRETER_CSTYLEFORSTATEMENTNODE_HPP