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,
Arg,
Destructure,
Neg,
Not,
Mul,
Div,
Mod,
Add,
Sub,
Eq,
Ne,
Gt,
Ge,
Lt,
Le,
And,
Or,
}
impl fmt::Debug for Builtin {
@ -26,6 +41,21 @@ impl fmt::Debug for Builtin {
Self::Scope => write!(f, "'scope"),
Self::Arg => write!(f, "'arg"),
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 {
pub fn desugar(self) -> (Self, bool) {
@ -16,68 +17,62 @@ impl Expr {
Self::FuncDef(def) => def.desugar(),
Self::Paren {
s0,
s0: _,
inner,
s1,
span,
} => (
Self::Paren {
s0,
inner,
s1,
span,
},
false,
), // TODO Implement
s1: _,
span: _,
} => (*inner, true),
Self::Neg {
minus,
s0,
s0: _,
expr,
span,
} => (
Self::Neg {
minus,
s0,
expr,
span,
},
false,
), // TODO Implement
} => {
let new = Call::arg(Lit::Builtin(Builtin::Neg, minus).expr().boxed(), expr, span);
(new.expr(), true)
}
Self::Not {
not,
s0,
s0: _,
expr,
span,
} => (
Self::Not {
not,
s0,
expr,
span,
},
false,
), // TODO Implement
} => {
let new = Call::arg(Lit::Builtin(Builtin::Not, not).expr().boxed(), expr, span);
(new.expr(), true)
}
Self::BinOp {
left,
s0,
s0: _,
op,
s1,
s1: _,
right,
span,
} => (
Self::BinOp {
left,
s0,
op,
s1,
right,
span,
},
false,
), // TODO Implement
} => {
let builtin = match op {
BinOp::Mul => Builtin::Mul,
BinOp::Div => Builtin::Div,
BinOp::Mod => Builtin::Mod,
BinOp::Add => Builtin::Add,
BinOp::Sub => Builtin::Sub,
BinOp::Eq => Builtin::Eq,
BinOp::Neq => Builtin::Ne,
BinOp::Gt => Builtin::Gt,
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)
}
}
}
}