Desugar all expressions

This commit is contained in:
Joscha 2022-11-22 17:39:02 +01:00
parent 26dc3c3469
commit f8fa044259
2 changed files with 71 additions and 46 deletions

View file

@ -12,6 +12,21 @@ pub enum Builtin {
Scope, Scope,
Arg, Arg,
Destructure, Destructure,
Neg,
Not,
Mul,
Div,
Mod,
Add,
Sub,
Eq,
Ne,
Gt,
Ge,
Lt,
Le,
And,
Or,
} }
impl fmt::Debug for Builtin { impl fmt::Debug for Builtin {
@ -26,6 +41,21 @@ impl fmt::Debug for Builtin {
Self::Scope => write!(f, "'scope"), Self::Scope => write!(f, "'scope"),
Self::Arg => write!(f, "'arg"), Self::Arg => write!(f, "'arg"),
Self::Destructure => write!(f, "'destructure"), Self::Destructure => write!(f, "'destructure"),
Self::Neg => write!(f, "'neg"),
Self::Not => write!(f, "'not"),
Self::Mul => write!(f, "'mul"),
Self::Div => write!(f, "'div"),
Self::Mod => write!(f, "'mod"),
Self::Add => write!(f, "'add"),
Self::Sub => write!(f, "'sub"),
Self::Eq => write!(f, "'eq"),
Self::Ne => write!(f, "'ne"),
Self::Gt => write!(f, "'gt"),
Self::Ge => write!(f, "'ge"),
Self::Lt => write!(f, "'lt"),
Self::Le => write!(f, "'le"),
Self::And => write!(f, "'and"),
Self::Or => write!(f, "'or"),
} }
} }
} }

View file

@ -1,4 +1,5 @@
use crate::ast::Expr; use crate::ast::{BinOp, BoundedSeparated, Call, Expr, Lit, TableConstrElem};
use crate::builtin::Builtin;
impl Expr { impl Expr {
pub fn desugar(self) -> (Self, bool) { pub fn desugar(self) -> (Self, bool) {
@ -16,68 +17,62 @@ impl Expr {
Self::FuncDef(def) => def.desugar(), Self::FuncDef(def) => def.desugar(),
Self::Paren { Self::Paren {
s0, s0: _,
inner, inner,
s1, s1: _,
span, span: _,
} => ( } => (*inner, true),
Self::Paren {
s0,
inner,
s1,
span,
},
false,
), // TODO Implement
Self::Neg { Self::Neg {
minus, minus,
s0, s0: _,
expr, expr,
span, span,
} => ( } => {
Self::Neg { let new = Call::arg(Lit::Builtin(Builtin::Neg, minus).expr().boxed(), expr, span);
minus, (new.expr(), true)
s0, }
expr,
span,
},
false,
), // TODO Implement
Self::Not { Self::Not {
not, not,
s0, s0: _,
expr, expr,
span, span,
} => ( } => {
Self::Not { let new = Call::arg(Lit::Builtin(Builtin::Not, not).expr().boxed(), expr, span);
not, (new.expr(), true)
s0, }
expr,
span,
},
false,
), // TODO Implement
Self::BinOp { Self::BinOp {
left, left,
s0, s0: _,
op, op,
s1, s1: _,
right, right,
span, span,
} => ( } => {
Self::BinOp { let builtin = match op {
left, BinOp::Mul => Builtin::Mul,
s0, BinOp::Div => Builtin::Div,
op, BinOp::Mod => Builtin::Mod,
s1, BinOp::Add => Builtin::Add,
right, BinOp::Sub => Builtin::Sub,
span, BinOp::Eq => Builtin::Eq,
}, BinOp::Neq => Builtin::Ne,
false, BinOp::Gt => Builtin::Gt,
), // TODO Implement BinOp::Ge => Builtin::Ge,
BinOp::Lt => Builtin::Lt,
BinOp::Le => Builtin::Le,
BinOp::And => Builtin::And,
BinOp::Or => Builtin::Or,
};
let constr = BoundedSeparated::new(span)
.then(TableConstrElem::positional(left))
.then(TableConstrElem::positional(right))
.table_constr();
let new = Call::constr(Lit::Builtin(builtin, span).expr().boxed(), constr, span);
(new.expr(), true)
}
} }
} }
} }