feat(lexer): Add diagnostic and impl error recovering

This commit is contained in:
2026-05-08 22:54:46 +08:00
parent e8b50ae0d7
commit 63a2990826
12 changed files with 525 additions and 120 deletions
+1
View File
@@ -0,0 +1 @@
pub mod types;
+75
View File
@@ -0,0 +1,75 @@
#[derive(Debug, Clone, Copy)]
pub struct Span {
start: usize,
end: usize,
}
pub struct CompileUnit {
pub global_decls: Vec<GlobalDeclStmt>,
}
pub enum GlobalDeclStmt {
VarDecl(VarDeclStmt),
FuncDecl(FuncDeclStmt),
}
pub struct VarDeclStmt {
pub name: String,
pub var_type: Type,
pub span: Span,
}
pub struct FuncDeclStmt {
pub name: String,
pub return_type: Type,
pub params: Vec<Param>,
pub body: BlockStmt,
pub span: Span,
}
pub struct BlockStmt {
pub statements: Vec<Statement>,
pub span: Span,
}
pub enum Statement {
Return(ReturnStmt),
Block(BlockStmt),
Expr(Expr),
VarDecl(VarDeclStmt),
}
pub struct ReturnStmt {
pub value: Option<Expr>,
pub span: Span,
}
pub struct Expr {
pub value: ExprValue,
pub span: Span,
}
pub enum ExprValue {
IntLit(i64),
Var(String),
BinaryOp {
lhs: Box<Expr>,
op: BinaryOp,
rhs: Box<Expr>
},
FuncCall(String, Vec<Expr>),
Assign {
lvalue: Box<Expr>,
rvalue: Box<Expr>
},
}
pub enum BinaryOp {
Add, Sub, Mul, Div, Mod,
Equal, NotEqual, Less, LessEqual, Greater, GreaterEqual,
}
pub enum Type {
Int,
Void,
}
pub struct Param {
name: String,
param_type: Type,
span: Span,
}