From e8470824352064fefbb5d0d30a728211d11cea53 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 26 Jan 2021 21:13:52 +0100 Subject: =?UTF-8?q?Multi-expression=20blocks=20=F0=9F=9B=8D=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/expr.rs | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'src/syntax/expr.rs') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 6be9d632..afeac988 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -75,11 +75,7 @@ impl Pretty for Expr { v.v.pretty(p); p.push_str(")"); } - Self::Block(v) => { - p.push_str("{"); - v.v.pretty(p); - p.push_str("}"); - } + Self::Block(v) => v.pretty(p), Self::Unary(v) => v.pretty(p), Self::Binary(v) => v.pretty(p), Self::Call(v) => v.pretty(p), @@ -139,10 +135,28 @@ impl Pretty for Named { pub type ExprTemplate = Tree; /// A grouped expression: `(1 + 2)`. -pub type ExprGroup = Box>; +pub type ExprGroup = SpanBox; /// A block expression: `{1 + 2}`. -pub type ExprBlock = Box>; +#[derive(Debug, Clone, PartialEq)] +pub struct ExprBlock { + /// The list of expressions contained in the block. + pub exprs: SpanVec, +} + +impl Pretty for ExprBlock { + fn pretty(&self, p: &mut Printer) { + p.push_str("{"); + if self.exprs.len() > 1 { + p.push_str(" "); + } + p.join(&self.exprs, "; ", |expr, p| expr.v.pretty(p)); + if self.exprs.len() > 1 { + p.push_str(" "); + } + p.push_str("}"); + } +} /// A unary operation: `-x`. #[derive(Debug, Clone, PartialEq)] @@ -150,7 +164,7 @@ pub struct ExprUnary { /// The operator: `-`. pub op: Spanned, /// The expression to operator on: `x`. - pub expr: Box>, + pub expr: SpanBox, } impl Pretty for ExprUnary { @@ -213,11 +227,11 @@ impl Pretty for UnOp { #[derive(Debug, Clone, PartialEq)] pub struct ExprBinary { /// The left-hand side of the operation: `a`. - pub lhs: Box>, + pub lhs: SpanBox, /// The operator: `+`. pub op: Spanned, /// The right-hand side of the operation: `b`. - pub rhs: Box>, + pub rhs: SpanBox, } impl Pretty for ExprBinary { @@ -376,7 +390,7 @@ pub enum Associativity { #[derive(Debug, Clone, PartialEq)] pub struct ExprCall { /// The callee of the function. - pub callee: Box>, + pub callee: SpanBox, /// The arguments to the function. pub args: Spanned, } @@ -466,17 +480,17 @@ impl Pretty for Argument { pub struct ExprLet { /// The pattern to assign to. pub pat: Spanned, - /// The expression to assign to the pattern. - pub expr: Option>>, + /// The expression the pattern is initialized with. + pub init: Option>, } impl Pretty for ExprLet { fn pretty(&self, p: &mut Printer) { p.push_str("#let "); p.push_str(&self.pat.v); - if let Some(expr) = &self.expr { + if let Some(init) = &self.init { p.push_str(" = "); - expr.v.pretty(p); + init.v.pretty(p); } } } @@ -485,11 +499,11 @@ impl Pretty for ExprLet { #[derive(Debug, Clone, PartialEq)] pub struct ExprIf { /// The condition which selects the body to evaluate. - pub condition: Box>, + pub condition: SpanBox, /// The expression to evaluate if the condition is true. - pub if_body: Box>, + pub if_body: SpanBox, /// The expression to evaluate if the condition is false. - pub else_body: Option>>, + pub else_body: Option>, } impl Pretty for ExprIf { @@ -520,6 +534,7 @@ mod tests { #[test] fn test_pretty_print_expressions() { // Unary and binary operations. + test_pretty("{}", "{}"); test_pretty("{1 +}", "{1}"); test_pretty("{1++1}", "{1 + +1}"); test_pretty("{+-1}", "{+-1}"); -- cgit v1.2.3