From 84ba547c7c80e45cc8edafcde8714973bb2a3a2f Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 20 Jan 2021 21:33:13 +0100 Subject: =?UTF-8?q?If=20expressions=20=F0=9F=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/expr.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'src/syntax/expr.rs') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 79713cf1..29e143b2 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -46,6 +46,8 @@ pub enum Expr { Block(ExprBlock), /// A let expression: `let x = 1`. Let(ExprLet), + /// An if expression: `if x { y } else { z }`. + If(ExprIf), } impl Pretty for Expr { @@ -82,6 +84,7 @@ impl Pretty for Expr { p.push_str("}"); } Self::Let(v) => v.pretty(p), + Self::If(v) => v.pretty(p), } } } @@ -329,6 +332,30 @@ impl Pretty for ExprLet { } } +/// An if expression: `if x { y } else { z }`. +#[derive(Debug, Clone, PartialEq)] +pub struct ExprIf { + /// The pattern to assign to. + pub condition: Box>, + /// The expression to evaluate if the condition is true. + pub if_body: Box>, + /// The expression to evaluate if the condition is false. + pub else_body: Option>>, +} + +impl Pretty for ExprIf { + fn pretty(&self, p: &mut Printer) { + p.push_str("#if "); + self.condition.v.pretty(p); + p.push_str(" "); + self.if_body.v.pretty(p); + if let Some(expr) = &self.else_body { + p.push_str(" #else "); + expr.v.pretty(p); + } + } +} + #[cfg(test)] mod tests { use super::super::tests::test_pretty; @@ -366,8 +393,9 @@ mod tests { test_pretty("{(1)}", "{(1)}"); test_pretty("{{1}}", "{{1}}"); - // Let binding. - test_pretty("#let x=1+2", "#let x = 1 + 2"); + // Control flow. + test_pretty("#let x = 1+2", "#let x = 1 + 2"); + test_pretty("#if x [y] #else [z]", "#if x [y] #else [z]"); } #[test] -- cgit v1.2.3