Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
274 lines
6.1 KiB
Rust
274 lines
6.1 KiB
Rust
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<Item>,
|
|
}
|
|
|
|
#[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<String>,
|
|
pub path: String,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct TypeParam {
|
|
pub name: String,
|
|
pub constraint: Option<Type>,
|
|
}
|
|
|
|
#[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<TypeParam>,
|
|
pub params: Vec<Param>,
|
|
pub ret: Option<Type>,
|
|
/// `None` for interface method signatures.
|
|
pub body: Option<Block>,
|
|
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<TypeParam>,
|
|
pub fields: Vec<Field>,
|
|
pub methods: Vec<FuncDecl>,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Variant {
|
|
pub name: String,
|
|
pub fields: Vec<Param>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct EnumDecl {
|
|
pub is_pub: bool,
|
|
pub name: String,
|
|
pub type_params: Vec<TypeParam>,
|
|
pub variants: Vec<Variant>,
|
|
pub methods: Vec<FuncDecl>,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct InterfaceDecl {
|
|
pub is_pub: bool,
|
|
pub name: String,
|
|
pub type_params: Vec<TypeParam>,
|
|
pub methods: Vec<FuncDecl>,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub enum Type {
|
|
/// `int`, `ui.Key`, `Stack[int]`
|
|
Named { path: Vec<String>, args: Vec<Type> },
|
|
/// `[]T`
|
|
Slice(Box<Type>),
|
|
/// `T?`
|
|
Optional(Box<Type>),
|
|
/// `shared T`
|
|
Shared(Box<Type>),
|
|
/// `weak T`
|
|
Weak(Box<Type>),
|
|
/// `(A, B)`, only for multiple return values
|
|
Tuple(Vec<Type>),
|
|
/// `func(A, B) R`
|
|
Func { params: Vec<Type>, ret: Option<Box<Type>> },
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Block {
|
|
pub stmts: Vec<Stmt>,
|
|
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<String>,
|
|
pub ty: Option<Type>,
|
|
pub value: Option<Expr>,
|
|
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<String>, iter: Expr, step: Option<Expr> },
|
|
}
|
|
|
|
#[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<Box<Stmt>>, span: Span },
|
|
For { kind: ForKind, body: Block, span: Span },
|
|
Return { values: Vec<Expr>, 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<String>,
|
|
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<Pattern> },
|
|
}
|
|
|
|
#[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<StrPart>),
|
|
Bool(bool),
|
|
Nil,
|
|
Ident(String),
|
|
Array(Vec<Expr>),
|
|
/// `{"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<Expr>, fields: Vec<FieldInit> },
|
|
Unary { op: UnOp, expr: Box<Expr> },
|
|
Binary { op: BinOp, lhs: Box<Expr>, rhs: Box<Expr> },
|
|
/// `a..b`, `a..`, `..b`
|
|
Range { start: Option<Box<Expr>>, end: Option<Box<Expr>> },
|
|
Call { callee: Box<Expr>, args: Vec<Expr> },
|
|
Index { expr: Box<Expr>, index: Box<Expr> },
|
|
/// A generic type with its types written out, before `{` or `.`: `Stack[int]{}`, `Option[int].None`
|
|
Generic { expr: Box<Expr>, args: Vec<Type> },
|
|
Field { expr: Box<Expr>, name: String },
|
|
/// `expr?`: return the error to the caller
|
|
Try(Box<Expr>),
|
|
/// `shared expr`
|
|
Shared(Box<Expr>),
|
|
/// `weak x`: a non-owning pointer to a shared value
|
|
Weak(Box<Expr>),
|
|
Closure { params: Vec<Param>, ret: Option<Type>, body: Block },
|
|
/// `x => x * 2`, `(a, b) => a + b`: the types come from where it is used
|
|
Lambda { params: Vec<String>, body: Box<Expr> },
|
|
Match { scrutinee: Box<Expr>, arms: Vec<MatchArm> },
|
|
}
|