Add function definitions to ast

This commit is contained in:
Joscha 2022-11-19 10:58:34 +01:00
parent 5466f7afbf
commit 36e40d254d
3 changed files with 100 additions and 0 deletions

View file

@ -3,6 +3,7 @@ use crate::span::{HasSpan, Span};
use super::basic::Space;
use super::call::Call;
use super::field::Field;
use super::func_def::FuncDef;
use super::lit::Lit;
use super::table_constr::TableConstr;
use super::table_destr::TableDestr;
@ -38,6 +39,7 @@ pub enum Expr {
Var(Var),
TableConstr(TableConstr),
TableDestr(TableDestr),
FuncDef(FuncDef),
/// `(a)`
///
@ -93,6 +95,7 @@ impl HasSpan for Expr {
Expr::Var(var) => var.span(),
Expr::TableConstr(constr) => constr.span(),
Expr::TableDestr(destr) => destr.span(),
Expr::FuncDef(def) => def.span(),
Expr::Paren { span, .. } => *span,
Expr::Neg { span, .. } => *span,
Expr::Not { span, .. } => *span,

95
src/ast/func_def.rs Normal file
View file

@ -0,0 +1,95 @@
use crate::span::{HasSpan, Span};
use super::{Expr, Ident, Space, TablePattern};
#[derive(Debug, Clone)]
pub enum FuncDef {
/// `function() a
///
/// Structure: `function s0 ( s1 ) s2 body`
AnonNoArg {
s0: Space,
s1: Space,
s2: Space,
body: Box<Expr>,
span: Span,
},
/// `function(foo) a`
///
/// Structure: `function s0 ( s1 arg s2 ) s3 body`
AnonArg {
s0: Space,
s1: Space,
arg: Ident,
s2: Space,
s3: Space,
body: Box<Expr>,
span: Span,
},
/// `function{..} a`
///
/// Structure: `function s0 pattern s1 body`
AnonDestr {
s0: Space,
pattern: TablePattern,
s1: Space,
body: Box<Expr>,
span: Span,
},
/// `function foo() a`
///
/// Structure: `function s0 name s1 ( s2 ) s3 body`
NamedNoArg {
s0: Space,
name: Ident,
s1: Space,
s2: Space,
s3: Space,
body: Box<Expr>,
span: Span,
},
/// `function foo(bar) a`
///
/// Structure: `function s0 name s1 ( s2 arg s3 ) s4 body`
NamedArg {
s0: Space,
name: Ident,
s1: Space,
s2: Space,
arg: Ident,
s3: Space,
s4: Space,
body: Box<Expr>,
span: Span,
},
/// `function foo{..} a`
///
/// Structure: `function s0 name s1 pattern s2 body`
NamedDestr {
s0: Space,
name: Ident,
s1: Space,
pattern: TablePattern,
s2: Space,
body: Box<Expr>,
span: Span,
},
}
impl HasSpan for FuncDef {
fn span(&self) -> Span {
match self {
FuncDef::AnonNoArg { span, .. } => *span,
FuncDef::AnonArg { span, .. } => *span,
FuncDef::AnonDestr { span, .. } => *span,
FuncDef::NamedNoArg { span, .. } => *span,
FuncDef::NamedArg { span, .. } => *span,
FuncDef::NamedDestr { span, .. } => *span,
}
}
}