diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..2c4092d --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +enum_discrim_align_threshold = 16 +struct_field_align_threshold = 16 diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index e69de29..8a80787 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -0,0 +1,117 @@ +use lasso::Spur; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Span { + pub start: usize, + pub end: usize, +} + +impl From> for Span { + fn from(value: std::ops::Range) -> Self { + Self { + start: value.start, + end: value.end, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Spanned { + pub item: T, + pub span: Span, +} + +impl Spanned { + #[inline] + pub fn new(item: T, span: impl Into) -> Self { + Self { + item, + span: span.into(), + } + } +} + +pub type SpanExpr<'a> = Spanned>; +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, ExprRef<'a>, ExprRef<'a>), + Unary(Spanned, ExprRef<'a>), + Def { + kind: DefKind, + ident: Spanned, + ty: Option>, + init: Option>, + }, + Set(ExprRef<'a>, ExprRef<'a>), + Loop(ExprList<'a>), + Block(ExprList<'a>), + Switch { + on: ExprRef<'a>, + branches: &'a [(Spanned, ExprRef<'a>)], + else_: ExprRef<'a>, + }, + CondSwitch { + branches: &'a [(ExprRef<'a>, ExprRef<'a>)], + else_: ExprRef<'a>, + }, + Func { + ident: Spanned, + params: &'a [(Spanned, Spanned)], + ret: Spanned, + body: ExprList<'a>, + }, + Break(Option>), + Return(Option>), + 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, +} diff --git a/src/syntax/token.rs b/src/syntax/token.rs index e165690..5f44839 100644 --- a/src/syntax/token.rs +++ b/src/syntax/token.rs @@ -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(