From 4573880fcc32bd76a72a4ae713190cd997194583 Mon Sep 17 00:00:00 2001 From: ov-4 Date: Thu, 29 Jan 2026 13:02:49 +0000 Subject: Added loop statements, FOR, WHILE and REPEAT --- include/cps/AST.h | 60 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 11 deletions(-) (limited to 'include/cps/AST.h') diff --git a/include/cps/AST.h b/include/cps/AST.h index 3c58642..1473c45 100644 --- a/include/cps/AST.h +++ b/include/cps/AST.h @@ -24,6 +24,17 @@ public: const std::string &getName() const { return Name; } }; +class BinaryExprAST : public ExprAST { + int Op; + std::unique_ptr LHS, RHS; +public: + BinaryExprAST(int Op, std::unique_ptr LHS, std::unique_ptr RHS) + : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} + int getOp() const { return Op; } + ExprAST *getLHS() const { return LHS.get(); } + ExprAST *getRHS() const { return RHS.get(); } +}; + class StmtAST { public: virtual ~StmtAST() = default; @@ -60,17 +71,6 @@ public: ExprAST *getExpr() const { return Expr.get(); } }; -class BinaryExprAST : public ExprAST { - int Op; - std::unique_ptr LHS, RHS; -public: - BinaryExprAST(int Op, std::unique_ptr LHS, std::unique_ptr RHS) - : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} - int getOp() const { return Op; } - ExprAST *getLHS() const { return LHS.get(); } - ExprAST *getRHS() const { return RHS.get(); } -}; - class IfStmtAST : public StmtAST { std::unique_ptr Cond; std::vector> ThenStmts; @@ -86,5 +86,43 @@ public: const std::vector> &getElseStmts() const { return ElseStmts; } }; +class WhileStmtAST : public StmtAST { + std::unique_ptr Cond; + std::vector> Body; +public: + WhileStmtAST(std::unique_ptr Cond, std::vector> Body) + : Cond(std::move(Cond)), Body(std::move(Body)) {} + ExprAST *getCond() const { return Cond.get(); } + const std::vector> &getBody() const { return Body; } +}; + +class RepeatStmtAST : public StmtAST { + std::vector> Body; + std::unique_ptr Cond; +public: + RepeatStmtAST(std::vector> Body, std::unique_ptr Cond) + : Body(std::move(Body)), Cond(std::move(Cond)) {} + ExprAST *getCond() const { return Cond.get(); } + const std::vector> &getBody() const { return Body; } +}; + +class ForStmtAST : public StmtAST { + std::string VarName; + std::unique_ptr Start; + std::unique_ptr End; + std::unique_ptr Step; + std::vector> Body; +public: + ForStmtAST(const std::string &VarName, std::unique_ptr Start, + std::unique_ptr End, std::unique_ptr Step, + std::vector> Body) + : VarName(VarName), Start(std::move(Start)), End(std::move(End)), + Step(std::move(Step)), Body(std::move(Body)) {} + const std::string &getVarName() const { return VarName; } + ExprAST *getStart() const { return Start.get(); } + ExprAST *getEnd() const { return End.get(); } + ExprAST *getStep() const { return Step.get(); } + const std::vector> &getBody() const { return Body; } +}; } // namespace cps -- cgit v1.3.1-4-g156e