// Minimal AST definitions for the SysY subset used in this toy compiler. #pragma once #include #include #include namespace ast { enum class BinaryOp { Add, Sub, Mul, Div }; struct Expr { virtual ~Expr() = default; }; struct NumberExpr : Expr { int value{}; explicit NumberExpr(int v) : value(v) {} }; struct VarExpr : Expr { std::string name; explicit VarExpr(std::string n) : name(std::move(n)) {} }; struct BinaryExpr : Expr { BinaryOp op; std::shared_ptr lhs; std::shared_ptr rhs; BinaryExpr(BinaryOp op, std::shared_ptr lhs, std::shared_ptr rhs) : op(op), lhs(std::move(lhs)), rhs(std::move(rhs)) {} }; struct Stmt { virtual ~Stmt() = default; }; struct ReturnStmt : Stmt { std::shared_ptr value; explicit ReturnStmt(std::shared_ptr v) : value(std::move(v)) {} }; struct VarDecl { std::string name; std::shared_ptr init; // nullptr if no initializer VarDecl(std::string n, std::shared_ptr i) : name(std::move(n)), init(std::move(i)) {} }; struct Block { std::vector> varDecls; std::vector> stmts; }; struct FuncDef { std::string name; std::shared_ptr body; FuncDef(std::string n, std::shared_ptr b) : name(std::move(n)), body(std::move(b)) {} }; struct CompUnit { std::shared_ptr func; explicit CompUnit(std::shared_ptr f) : func(std::move(f)) {} }; // 调试打印 void PrintAST(const CompUnit& cu); } // namespace ast