From f8fa04425913b1523b5c6ce4ef18235f260ef324 Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 22 Nov 2022 17:39:02 +0100 Subject: [PATCH] Desugar all expressions --- src/builtin.rs | 30 ++++++++++++++++ src/desugar/expr.rs | 87 +++++++++++++++++++++------------------------ 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/src/builtin.rs b/src/builtin.rs index f0e504d..6ef18c8 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -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"), } } } diff --git a/src/desugar/expr.rs b/src/desugar/expr.rs index 7b3cc26..b7f4b08 100644 --- a/src/desugar/expr.rs +++ b/src/desugar/expr.rs @@ -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) + } } } }