From 539735e668f601058c2c71a847335e17fac107e8 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 13 Jan 2021 11:54:50 +0100 Subject: =?UTF-8?q?Basic=20let=20bindings=20=F0=9F=8E=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/expr.rs | 26 ++++++++++++++++++++++++++ src/syntax/token.rs | 3 +++ 2 files changed, 29 insertions(+) (limited to 'src/syntax') diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 09472df4..b758a849 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -44,6 +44,8 @@ pub enum Expr { Group(Box), /// A block expression: `{1 + 2}`. Block(Box), + /// A let expression: `let x = 1`. + Let(ExprLet), } impl Pretty for Expr { @@ -79,6 +81,7 @@ impl Pretty for Expr { v.pretty(p); p.push_str("}"); } + Self::Let(v) => v.pretty(p), } } } @@ -300,6 +303,26 @@ impl Pretty for ExprDict { /// A template expression: `[*Hi* there!]`. pub type ExprTemplate = Tree; +/// A let expression: `let x = 1`. +#[derive(Debug, Clone, PartialEq)] +pub struct ExprLet { + /// The pattern to assign to. + pub pat: Spanned, + /// The expression to assign to the pattern. + pub expr: 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 { + p.push_str(" = "); + expr.v.pretty(p); + } + } +} + #[cfg(test)] mod tests { use super::super::tests::test_pretty; @@ -336,6 +359,9 @@ mod tests { // Parens and blocks. test_pretty("{(1)}", "{(1)}"); test_pretty("{{1}}", "{{1}}"); + + // Let binding. + test_pretty("#let x=1+2", "#let x = 1 + 2"); } #[test] diff --git a/src/syntax/token.rs b/src/syntax/token.rs index 7055d61a..43415198 100644 --- a/src/syntax/token.rs +++ b/src/syntax/token.rs @@ -27,6 +27,8 @@ pub enum Token<'s> { Backslash, /// A comma: `,`. Comma, + /// A semicolon: `;`. + Semicolon, /// A colon: `:`. Colon, /// A pipe: `|`. @@ -201,6 +203,7 @@ impl<'s> Token<'s> { Self::Tilde => "tilde", Self::Backslash => "backslash", Self::Comma => "comma", + Self::Semicolon => "semicolon", Self::Colon => "colon", Self::Pipe => "pipe", Self::Plus => "plus", -- cgit v1.2.3