Files
leaf/parser/src/ast.rs
T

226 lines
3.9 KiB
Rust
Raw Normal View History

2026-02-26 19:40:27 +01:00
use arcstr::Substr;
use derive_more::Deref;
2026-03-07 01:42:06 +01:00
use indexmap::IndexMap;
2026-03-06 17:17:12 +01:00
use std::ops::Range;
use std::sync::Arc;
2026-02-26 19:40:27 +01:00
#[derive(Debug, Deref)]
pub struct Ident(pub Substr);
#[derive(Debug, Deref)]
pub struct Import(pub Expr);
#[derive(Debug)]
pub struct Number {
pub text: Substr,
pub r#type: Option<Ident>,
}
#[derive(derive_more::Debug)]
pub enum Expr {
#[debug("uninit")]
Uninit(Substr),
#[debug("Ident({:?})", _0.0)]
Ident(Ident),
#[debug("{_0:?}")]
Number(Number),
String(Substr),
#[debug("{_0:?}")]
2026-03-07 16:24:32 +01:00
Binary(Box<BinaryExpr>),
Index(Box<IndexingExpr>),
Access(Box<AccessExpr>),
Deref(Box<Expr>),
2026-02-26 19:40:27 +01:00
Tuple(Vec<Expr>),
List(Vec<Expr>),
2026-03-07 16:24:32 +01:00
Struct(Box<StructCtor>),
2026-02-26 19:40:27 +01:00
#[debug("{_0:?}")]
2026-03-07 16:24:32 +01:00
Block(Arc<Block>),
2026-02-26 19:40:27 +01:00
#[debug("{_0:?}")]
2026-03-06 17:17:12 +01:00
Func(Arc<Function>),
2026-02-27 15:48:16 +01:00
#[debug("{_0:?}")]
2026-03-07 16:24:32 +01:00
Type(Box<Type>),
2026-02-26 19:40:27 +01:00
#[debug("{_0:?}")]
2026-03-07 16:24:32 +01:00
ConstDecl(Box<ConstDecl>),
#[debug("{_0:?}")]
VarDecl(Box<VarDecl>),
2026-02-26 19:40:27 +01:00
#[debug("{_0:?}")]
2026-03-07 16:24:32 +01:00
For(Box<For>),
2026-02-26 19:40:27 +01:00
#[debug("{_0:?}")]
2026-03-07 16:24:32 +01:00
While(Box<While>),
2026-02-27 15:48:16 +01:00
#[debug("{_0:?}")]
2026-03-07 16:24:32 +01:00
If(Box<If>),
2026-02-26 19:40:27 +01:00
Call {
2026-03-07 16:24:32 +01:00
func: Box<Expr>,
2026-02-26 19:40:27 +01:00
args: Vec<Expr>,
},
}
2026-02-27 15:48:16 +01:00
impl Expr {
pub fn range(&self) -> Range<usize> {
match self {
2026-03-06 15:21:44 +01:00
Self::Ident(e) => e.range(),
2026-03-07 01:42:06 +01:00
Self::Access(e) => e.range(),
Self::Number(e) => e.text.range(),
2026-03-06 15:21:44 +01:00
_ => todo!("{self:?}"),
2026-02-27 15:48:16 +01:00
}
}
}
2026-02-26 19:40:27 +01:00
#[derive(Debug)]
pub struct BinaryExpr {
pub lhs: Expr,
pub op: BinaryOp,
pub rhs: Expr,
}
#[derive(Debug)]
pub struct IndexingExpr {
pub value: Expr,
pub index: Expr,
}
2026-03-07 01:42:06 +01:00
#[derive(Debug)]
pub struct AccessExpr {
pub value: Expr,
pub field: Ident,
}
impl AccessExpr {
pub fn range(&self) -> Range<usize> {
self.value.range().start..self.field.0.range().end
}
}
2026-02-26 19:40:27 +01:00
#[rustfmt::skip]
#[derive(derive_more::Debug)]
pub enum BinaryOp {
2026-02-27 15:48:16 +01:00
#[debug("{_0}")] Add(Substr),
#[debug("{_0}")] Sub(Substr),
#[debug("{_0}")] Mul(Substr),
#[debug("{_0}")] Div(Substr),
#[debug("{_0}")] Mod(Substr),
2026-03-06 15:21:44 +01:00
#[debug("{_0}")] Eq(Substr),
#[debug("{_0}")] Ne(Substr),
2026-02-27 15:48:16 +01:00
#[debug("{_0}")] Lt(Substr),
#[debug("{_0}")] Gt(Substr),
#[debug("{_0}")] Le(Substr),
#[debug("{_0}")] Ge(Substr),
#[debug("{_0}")] Range(Substr),
#[debug("{_0}")] Assign(Substr),
2026-03-06 15:21:44 +01:00
#[debug("{_0}")] Cast(Substr),
2026-02-27 15:48:16 +01:00
}
#[derive(Debug)]
pub enum Type {
2026-03-07 01:42:06 +01:00
Ptr { base: Expr, mutable: Option<Substr> },
Struct(Struct),
2026-02-26 19:40:27 +01:00
}
#[derive(Debug)]
pub struct ConstDecl {
pub names: NamePattern,
pub r#type: Option<Expr>,
pub value: Expr,
}
#[derive(Debug)]
pub struct VarDecl {
pub names: NamePattern,
pub r#type: Option<Expr>,
pub value: Expr,
}
#[derive(Debug)]
pub enum NamePattern {
Single(Ident),
Tuple(Vec<Ident>),
List(Vec<Ident>),
}
impl NamePattern {
pub fn as_slice(&self) -> &[Ident] {
match self {
NamePattern::Single(ident) => std::slice::from_ref(ident),
NamePattern::Tuple(idents) => idents.as_slice(),
NamePattern::List(idents) => idents.as_slice(),
}
}
}
#[derive(Debug)]
pub struct Function {
pub text: Substr,
pub args: Vec<NameValuePair>,
2026-02-27 15:48:16 +01:00
pub ret: Option<Expr>,
2026-03-06 17:17:12 +01:00
pub block: Option<Arc<Block>>,
2026-02-26 19:40:27 +01:00
}
#[derive(Debug)]
pub struct NameValuePair {
pub name: Ident,
2026-03-07 01:42:06 +01:00
pub value: Expr,
}
#[derive(Debug)]
pub struct Struct {
pub fields: Vec<Field>,
}
#[derive(Debug)]
pub struct Field {
pub name: Ident,
pub ty: Expr,
pub public: Option<Substr>,
pub mutable: Option<Substr>,
2026-02-26 19:40:27 +01:00
}
#[derive(Debug)]
pub struct StructCtor {
2026-03-07 19:16:29 +01:00
pub r#type: Option<Expr>,
2026-03-07 01:42:06 +01:00
pub r#values: IndexMap<Substr, NameValuePair>,
2026-03-07 19:16:29 +01:00
pub(crate) range: Range<usize>,
}
impl StructCtor {
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
2026-02-26 19:40:27 +01:00
}
#[derive(Debug)]
pub struct Block(pub Vec<Expr>);
#[derive(Debug)]
pub struct For {
pub names: NamePattern,
pub value: Expr,
pub block: Block,
}
2026-02-27 15:48:16 +01:00
#[derive(Debug)]
pub struct While {
2026-03-07 16:24:32 +01:00
pub cond: Expr,
2026-02-27 15:48:16 +01:00
pub block: Block,
}
2026-03-07 16:24:32 +01:00
#[derive(Debug)]
pub struct If {
pub cond: Expr,
pub block: Block,
pub else_: Option<Box<Else>>,
}
#[derive(Debug)]
pub enum Else {
If(If),
Block(Block),
}
2026-02-26 19:40:27 +01:00
#[derive(Debug)]
pub struct CompilationUnit {
pub imports: Vec<Import>,
pub decls: Vec<ConstDecl>,
}