Desugar all function definitions

This commit is contained in:
Joscha 2022-11-22 17:27:06 +01:00
parent 40c28d9496
commit 26dc3c3469
3 changed files with 96 additions and 19 deletions

View file

@ -101,6 +101,38 @@ impl HasSpan for FuncDef {
} }
impl FuncDef { impl FuncDef {
pub fn anon_no_arg(body: Box<Expr>, span: Span) -> Self {
Self::AnonNoArg {
s0: Space::empty(span),
s1: Space::empty(span),
s2: Space::empty(span),
body,
span,
}
}
pub fn anon_arg(arg: Ident, body: Box<Expr>, span: Span) -> Self {
Self::AnonArg {
s0: Space::empty(span),
s1: Space::empty(span),
arg,
s2: Space::empty(span),
s3: Space::empty(span),
body,
span,
}
}
pub fn anon_destr(pattern: TablePattern, body: Box<Expr>, span: Span) -> Self {
Self::AnonDestr {
s0: Space::empty(span),
pattern,
s1: Space::empty(span),
body,
span,
}
}
pub fn expr(self) -> Expr { pub fn expr(self) -> Expr {
Expr::FuncDef(self) Expr::FuncDef(self)
} }

View file

@ -63,6 +63,23 @@ impl HasSpan for TableDestr {
} }
impl TableDestr { impl TableDestr {
pub fn new(local: bool, pattern: TablePattern, value: Box<Expr>, span: Span) -> Self {
let local = if local {
Some(Space::empty(span))
} else {
None
};
Self {
local,
pattern,
s0: Space::empty(span),
s1: Space::empty(span),
value,
span,
}
}
pub fn expr(self) -> Expr { pub fn expr(self) -> Expr {
Expr::TableDestr(self) Expr::TableDestr(self)
} }

View file

@ -1,5 +1,6 @@
use crate::ast::{ use crate::ast::{
BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstrElem, TableLitElem, Var, BoundedSeparated, Call, Expr, FuncDef, Ident, Lit, Space, TableConstrElem, TableDestr,
TableLitElem, Var,
}; };
use crate::builtin::Builtin; use crate::builtin::Builtin;
@ -54,47 +55,74 @@ impl FuncDef {
} }
Self::AnonDestr { Self::AnonDestr {
s0, s0: _,
pattern, pattern,
s1, s1: _,
body, body,
span, span,
} => todo!(), } => {
let arg_call = Call::no_arg(Lit::Builtin(Builtin::Arg, span).expr().boxed(), span);
let arg_destr = TableDestr::new(true, pattern, arg_call.expr().boxed(), span);
let body = BoundedSeparated::new(span)
.then(TableLitElem::Positional(arg_destr.expr().boxed()))
.then(TableLitElem::Positional(body))
.table_lit();
let new = Self::AnonNoArg {
s0: Space::empty(span),
s1: Space::empty(span),
s2: Space::empty(span),
body: body.lit().expr().boxed(),
span,
};
(new.expr(), true)
}
Self::NamedNoArg { Self::NamedNoArg {
local, local,
s0, s0: _,
name, name,
s1, s1: _,
s2, s2: _,
s3, s3: _,
body, body,
span, span,
} => todo!(), } => {
let anon = Self::anon_no_arg(body, span);
let new = Var::assign_ident(local.is_some(), name, anon.expr().boxed(), span);
(new.expr(), true)
}
Self::NamedArg { Self::NamedArg {
local, local,
s0, s0: _,
name, name,
s1, s1: _,
s2, s2: _,
arg, arg,
s3, s3: _,
s4, s4: _,
body, body,
span, span,
} => todo!(), } => {
let anon = Self::anon_arg(arg, body, span);
let new = Var::assign_ident(local.is_some(), name, anon.expr().boxed(), span);
(new.expr(), true)
}
Self::NamedDestr { Self::NamedDestr {
local, local,
s0, s0: _,
name, name,
s1, s1: _,
pattern, pattern,
s2, s2: _,
body, body,
span, span,
} => todo!(), } => {
let anon = Self::anon_destr(pattern, body, span);
let new = Var::assign_ident(local.is_some(), name, anon.expr().boxed(), span);
(new.expr(), true)
}
} }
} }
} }