use std::fmt; #[derive(Clone, Copy, PartialEq, Eq, Default)] pub struct Span { pub line: u32, pub col: u32, } impl fmt::Debug for Span { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}:{}", self.line, self.col) } } #[derive(Debug, Clone, PartialEq)] pub struct Program { pub items: Vec, } #[derive(Debug, Clone, PartialEq)] pub enum Item { Import(Import), Func(FuncDecl), Struct(StructDecl), Enum(EnumDecl), Interface(InterfaceDecl), Const(VarDecl), } /// `import os`, `import a.b`, or `import c "SDL2/SDL.h"`. #[derive(Debug, Clone, PartialEq)] pub struct Import { pub alias: Option, pub path: String, pub span: Span, } #[derive(Debug, Clone, PartialEq)] pub struct TypeParam { pub name: String, pub constraint: Option, } #[derive(Debug, Clone, PartialEq)] pub struct Param { pub name: String, pub ty: Type, } #[derive(Debug, Clone, PartialEq)] pub struct FuncDecl { pub is_pub: bool, pub name: String, pub type_params: Vec, pub params: Vec, pub ret: Option, /// `None` for interface method signatures. pub body: Option, pub span: Span, } #[derive(Debug, Clone, PartialEq)] pub struct Field { pub is_pub: bool, pub name: String, pub ty: Type, } #[derive(Debug, Clone, PartialEq)] pub struct StructDecl { pub is_pub: bool, pub name: String, pub type_params: Vec, pub fields: Vec, pub methods: Vec, pub span: Span, } #[derive(Debug, Clone, PartialEq)] pub struct Variant { pub name: String, pub fields: Vec, } #[derive(Debug, Clone, PartialEq)] pub struct EnumDecl { pub is_pub: bool, pub name: String, pub type_params: Vec, pub variants: Vec, pub methods: Vec, pub span: Span, } #[derive(Debug, Clone, PartialEq)] pub struct InterfaceDecl { pub is_pub: bool, pub name: String, pub type_params: Vec, pub methods: Vec, pub span: Span, } #[derive(Debug, Clone, PartialEq)] pub enum Type { /// `int`, `ui.Key`, `Stack[int]` Named { path: Vec, args: Vec }, /// `[]T` Slice(Box), /// `T?` Optional(Box), /// `shared T` Shared(Box), /// `weak T` Weak(Box), /// `(A, B)`, only for multiple return values Tuple(Vec), /// `func(A, B) R` Func { params: Vec, ret: Option> }, } #[derive(Debug, Clone, PartialEq)] pub struct Block { pub stmts: Vec, pub span: Span, } /// `var x = 1`, `var a, b = f()`, `const N int = 5` #[derive(Debug, Clone, PartialEq)] pub struct VarDecl { pub is_pub: bool, pub is_const: bool, pub names: Vec, pub ty: Option, pub value: Option, pub span: Span, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AssignOp { Set, Add, Sub, Mul, Div, Rem, } #[derive(Debug, Clone, PartialEq)] pub enum ForKind { /// `for { }` Infinite, /// `for cond { }` While(Expr), /// `for x in items { }`, `for i, x in items { }`, `for i in 0..10 step 2 { }` In { vars: Vec, iter: Expr, step: Option }, } #[derive(Debug, Clone, PartialEq)] pub enum Stmt { Var(VarDecl), Assign { target: Expr, op: AssignOp, value: Expr, span: Span }, Expr(Expr), If { cond: Expr, then: Block, else_: Option>, span: Span }, For { kind: ForKind, body: Block, span: Span }, Return { values: Vec, span: Span }, Break(Span), Continue(Span), Block(Block), } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum UnOp { Neg, Not, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum BinOp { Or, And, Eq, Ne, Lt, Gt, Le, Ge, Add, Sub, Mul, Div, Rem, } #[derive(Debug, Clone, PartialEq)] pub enum StrPart { Text(String), Expr(Expr), } #[derive(Debug, Clone, PartialEq)] pub struct FieldInit { /// `None` for positional initializers like `Vec2{1, 2}`. pub name: Option, pub value: Expr, } #[derive(Debug, Clone, PartialEq)] pub enum Pattern { Wildcard, Int(i64), Char(char), Str(String), Bool(bool), Nil, /// A bare name: binds a value, or names a variant without data. Binding(String), /// `ok(s)`, `Circle(r)` Variant { name: String, args: Vec }, } #[derive(Debug, Clone, PartialEq)] pub enum ArmBody { Expr(Expr), Block(Block), } #[derive(Debug, Clone, PartialEq)] pub struct MatchArm { pub span: Span, pub pattern: Pattern, pub body: ArmBody, } #[derive(Debug, Clone, PartialEq)] pub struct Expr { pub kind: ExprKind, pub span: Span, } #[derive(Debug, Clone, PartialEq)] pub enum ExprKind { Int(i64), Float(f64), Char(char), Str(Vec), Bool(bool), Nil, Ident(String), Array(Vec), /// `{"a": 1}` or `map[string, int]{}`: key/value pairs, with the types written out or left to inference MapLit { types: Option<(Type, Type)>, entries: Vec<(Expr, Expr)> }, StructLit { ty: Box, fields: Vec }, Unary { op: UnOp, expr: Box }, Binary { op: BinOp, lhs: Box, rhs: Box }, /// `a..b`, `a..`, `..b` Range { start: Option>, end: Option> }, Call { callee: Box, args: Vec }, Index { expr: Box, index: Box }, /// A generic type with its types written out, before `{` or `.`: `Stack[int]{}`, `Option[int].None` Generic { expr: Box, args: Vec }, Field { expr: Box, name: String }, /// `expr?`: return the error to the caller Try(Box), /// `shared expr` Shared(Box), /// `weak x`: a non-owning pointer to a shared value Weak(Box), Closure { params: Vec, ret: Option, body: Block }, /// `x => x * 2`, `(a, b) => a + b`: the types come from where it is used Lambda { params: Vec, body: Box }, Match { scrutinee: Box, arms: Vec }, }