AST goes brrrr

This commit is contained in:
Erin 2023-10-03 02:20:25 +02:00 committed by ondra05
parent b70865071a
commit 1190ac5bce
3 changed files with 123 additions and 0 deletions

2
rustfmt.toml Normal file
View file

@ -0,0 +1,2 @@
enum_discrim_align_threshold = 16
struct_field_align_threshold = 16

View file

@ -0,0 +1,117 @@
use lasso::Spur;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl From<std::ops::Range<usize>> for Span {
fn from(value: std::ops::Range<usize>) -> Self {
Self {
start: value.start,
end: value.end,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Spanned<T> {
pub item: T,
pub span: Span,
}
impl<T> Spanned<T> {
#[inline]
pub fn new(item: T, span: impl Into<Span>) -> Self {
Self {
item,
span: span.into(),
}
}
}
pub type SpanExpr<'a> = Spanned<Expr<'a>>;
pub type ExprRef<'a> = &'a SpanExpr<'a>;
pub type ExprList<'a> = &'a [SpanExpr<'a>];
pub type Ident = Spur;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Expr<'a> {
Ident(Ident),
Path(ExprList<'a>),
Literal(Literal),
Call(ExprRef<'a>, ExprList<'a>),
Binary(Spanned<BinaryOp>, ExprRef<'a>, ExprRef<'a>),
Unary(Spanned<UnaryOp>, ExprRef<'a>),
Def {
kind: DefKind,
ident: Spanned<Ident>,
ty: Option<Spanned<Type>>,
init: Option<ExprRef<'a>>,
},
Set(ExprRef<'a>, ExprRef<'a>),
Loop(ExprList<'a>),
Block(ExprList<'a>),
Switch {
on: ExprRef<'a>,
branches: &'a [(Spanned<Switcher>, ExprRef<'a>)],
else_: ExprRef<'a>,
},
CondSwitch {
branches: &'a [(ExprRef<'a>, ExprRef<'a>)],
else_: ExprRef<'a>,
},
Func {
ident: Spanned<Ident>,
params: &'a [(Spanned<Ident>, Spanned<Type>)],
ret: Spanned<Type>,
body: ExprList<'a>,
},
Break(Option<ExprRef<'a>>),
Return(Option<ExprRef<'a>>),
Continue,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Type {
Ident(Ident),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Switcher {
Literal(Literal),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DefKind {
Const,
Var,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Literal {
String(Spur),
Int(u64),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinaryOp {
Plus,
Minus,
Star,
Slash,
Equ,
Neq,
Lt,
Gt,
LtEq,
GtEq,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnaryOp {
Minus,
Not,
Star,
}

View file

@ -47,6 +47,10 @@ pub enum Token {
#[token("include")] Include,
#[token("switch")] Switch,
#[token("loop")] Loop,
#[token("return")] Return,
#[token("break")] Break,
#[token("continue")] Continue,
#[token("uninit")] Uninit,
#[token("asm")] Asm,
#[regex(