#[derive(Debug, Clone, Copy)]
pub struct Span {
pub line: usize,
pub col: usize,
}
#[derive(Debug, Clone)]
pub struct Program {
pub stmts: Vec<Stmt>,
}
#[derive(Debug, Clone)]
pub struct Stmt {
pub kind: StmtKind,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum StmtKind {
Let {
mutable: bool,
pattern: Pattern,
type_ann: Option<TypeAnn>,
value: Expr,
},
ExprStmt { expr: Expr, has_semi: bool },
FnDecl {
name: String,
params: Vec<Param>,
body: Vec<Stmt>,
},
For {
label: Option<String>,
pattern: Pattern,
iter: Expr,
body: Vec<Stmt>,
},
While {
label: Option<String>,
cond: Expr,
body: Vec<Stmt>,
},
WhileLet {
label: Option<String>,
pattern: Pattern,
expr: Expr,
body: Vec<Stmt>,
},
Loop {
label: Option<String>,
body: Vec<Stmt>,
},
Break {
label: Option<String>,
value: Option<Expr>,
},
Continue { label: Option<String> },
Use {
path: Vec<String>,
imports: UseImports,
},
Return { value: Option<Expr> },
Assign {
target: AssignTarget,
op: AssignOp,
value: Expr,
},
}
#[derive(Debug, Clone)]
pub struct Param {
pub name: String,
pub default: Option<Expr>,
}
#[derive(Debug, Clone)]
pub enum AssignTarget {
Ident(String),
Index(Box<Expr>, Box<Expr>),
Field(Box<Expr>, String),
}
#[derive(Debug, Clone, Copy)]
pub enum AssignOp {
Eq,
PlusEq,
MinusEq,
StarEq,
SlashEq,
}
#[derive(Debug, Clone)]
pub struct Expr {
pub kind: ExprKind,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum ExprKind {
Int(i64),
Float(f64),
Bool(bool),
Str(String),
FStr(Vec<FStrPart>),
Bytes(Vec<u8>),
None,
Unit,
Ident(String),
ModulePath(Vec<String>),
SomeExpr(Box<Expr>),
OkExpr(Box<Expr>),
ErrExpr(Box<Expr>),
List(Vec<ListEntry>),
Dict(Vec<DictEntry>),
Tuple(Vec<Expr>),
ListComp {
expr: Box<Expr>,
pattern: Pattern,
iter: Box<Expr>,
cond: Option<Box<Expr>>,
},
DictComp {
key: Box<Expr>,
value: Box<Expr>,
pattern: Pattern,
iter: Box<Expr>,
cond: Option<Box<Expr>>,
},
BinOp {
left: Box<Expr>,
op: BinOp,
right: Box<Expr>,
},
UnaryOp {
op: UnaryOp,
expr: Box<Expr>,
},
Try(Box<Expr>),
PipeOp {
left: Box<Expr>,
right: Box<Expr>,
},
FieldAccess {
expr: Box<Expr>,
field: String,
},
Index {
expr: Box<Expr>,
index: Box<Expr>,
},
Slice {
expr: Box<Expr>,
start: Option<Box<Expr>>,
end: Option<Box<Expr>>,
inclusive: bool,
},
MethodCall {
expr: Box<Expr>,
method: String,
args: Vec<CallArg>,
},
Call {
func: Box<Expr>,
args: Vec<CallArg>,
},
Lambda {
params: Vec<String>,
body: Box<Expr>,
},
If {
cond: Box<Expr>,
then_body: Vec<Stmt>,
else_body: Option<Vec<Stmt>>,
},
IfLet {
pattern: Pattern,
expr: Box<Expr>,
then_body: Vec<Stmt>,
else_body: Option<Vec<Stmt>>,
},
Match {
expr: Box<Expr>,
arms: Vec<MatchArm>,
},
Block(Vec<Stmt>),
LoopExpr(Vec<Stmt>),
TryCatch {
body: Vec<Stmt>,
var: String,
handler: Vec<Stmt>,
},
StructConstruct {
name: String,
fields: Vec<(String, Expr)>,
spread: Option<Box<Expr>>,
},
EnumVariant {
enum_name: String,
variant: String,
},
EnumVariantCall {
enum_name: String,
variant: String,
args: Vec<Expr>,
},
Range {
start: Box<Expr>,
end: Box<Expr>,
inclusive: bool,
},
AsyncBlock(Vec<Stmt>),
SpawnExpr(Box<Expr>),
AwaitExpr(Box<Expr>),
SelectExpr(Vec<SelectBranch>),
}
#[derive(Debug, Clone)]
pub enum ListEntry {
Elem(Expr),
Spread(Expr),
}
#[derive(Debug, Clone)]
pub enum DictEntry {
KeyValue(Expr, Expr),
Spread(Expr),
}
#[derive(Debug, Clone)]
pub enum FStrPart {
Literal(String),
Expr(Expr),
}
#[derive(Debug, Clone)]
pub struct CallArg {
pub name: Option<String>,
pub value: Expr,
}
#[derive(Debug, Clone)]
pub struct SelectBranch {
pub pattern: Pattern,
pub future_expr: Expr,
pub body: Expr,
}
#[derive(Debug, Clone)]
pub struct MatchArm {
pub pattern: Pattern,
pub guard: Option<Expr>,
pub body: Expr,
}
#[derive(Debug, Clone)]
pub enum Pattern {
Wildcard,
Ident(String),
Int(i64),
Float(f64),
Bool(bool),
Str(String),
Bytes(Vec<u8>),
None,
Some(Box<Pattern>),
Ok(Box<Pattern>),
Err(Box<Pattern>),
Tuple(Vec<Pattern>),
List(Vec<Pattern>, Option<Box<Pattern>>),
EnumVariant {
enum_name: String,
variant: String,
fields: EnumPatternFields,
},
Struct {
name: String,
fields: Vec<(String, Option<Pattern>)>,
},
}
#[derive(Debug, Clone)]
pub enum EnumPatternFields {
None,
Positional(Vec<Pattern>),
Named(Vec<(String, Option<Pattern>)>),
}
#[derive(Debug, Clone, Copy)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Mod,
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
And,
Or,
BitAnd,
BitOr,
BitXor,
Shl,
Shr,
}
#[derive(Debug, Clone)]
pub enum TypeAnn {
Simple(String), Option(Box<TypeAnn>), Result(Box<TypeAnn>, Box<TypeAnn>), List(Box<TypeAnn>), Dict(Box<TypeAnn>, Box<TypeAnn>), }
#[derive(Debug, Clone, Copy)]
pub enum UnaryOp {
Neg,
Not,
}
#[derive(Debug, Clone)]
pub enum UseImports {
Glob,
Names(Vec<String>),
Single(String),
}