|
|
|
|
@ -16,20 +16,19 @@ struct Expr {
|
|
|
|
|
|
|
|
|
|
struct NumberExpr : Expr {
|
|
|
|
|
int value{};
|
|
|
|
|
explicit NumberExpr(int v) : value(v) {}
|
|
|
|
|
explicit NumberExpr(int v);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct VarExpr : Expr {
|
|
|
|
|
std::string name;
|
|
|
|
|
explicit VarExpr(std::string n) : name(std::move(n)) {}
|
|
|
|
|
explicit VarExpr(std::string n);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct BinaryExpr : Expr {
|
|
|
|
|
BinaryOp op;
|
|
|
|
|
std::shared_ptr<Expr> lhs;
|
|
|
|
|
std::shared_ptr<Expr> rhs;
|
|
|
|
|
BinaryExpr(BinaryOp op, std::shared_ptr<Expr> lhs, std::shared_ptr<Expr> rhs)
|
|
|
|
|
: op(op), lhs(std::move(lhs)), rhs(std::move(rhs)) {}
|
|
|
|
|
BinaryExpr(BinaryOp op, std::shared_ptr<Expr> lhs, std::shared_ptr<Expr> rhs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Stmt {
|
|
|
|
|
@ -38,14 +37,13 @@ struct Stmt {
|
|
|
|
|
|
|
|
|
|
struct ReturnStmt : Stmt {
|
|
|
|
|
std::shared_ptr<Expr> value;
|
|
|
|
|
explicit ReturnStmt(std::shared_ptr<Expr> v) : value(std::move(v)) {}
|
|
|
|
|
explicit ReturnStmt(std::shared_ptr<Expr> v);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct VarDecl {
|
|
|
|
|
std::string name;
|
|
|
|
|
std::shared_ptr<Expr> init; // nullptr if no initializer
|
|
|
|
|
VarDecl(std::string n, std::shared_ptr<Expr> i)
|
|
|
|
|
: name(std::move(n)), init(std::move(i)) {}
|
|
|
|
|
VarDecl(std::string n, std::shared_ptr<Expr> i);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Block {
|
|
|
|
|
@ -56,13 +54,12 @@ struct Block {
|
|
|
|
|
struct FuncDef {
|
|
|
|
|
std::string name;
|
|
|
|
|
std::shared_ptr<Block> body;
|
|
|
|
|
FuncDef(std::string n, std::shared_ptr<Block> b)
|
|
|
|
|
: name(std::move(n)), body(std::move(b)) {}
|
|
|
|
|
FuncDef(std::string n, std::shared_ptr<Block> b);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CompUnit {
|
|
|
|
|
std::shared_ptr<FuncDef> func;
|
|
|
|
|
explicit CompUnit(std::shared_ptr<FuncDef> f) : func(std::move(f)) {}
|
|
|
|
|
explicit CompUnit(std::shared_ptr<FuncDef> f);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 调试打印
|
|
|
|
|
|