diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-01-13 11:54:50 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-01-13 11:54:50 +0100 |
| commit | 539735e668f601058c2c71a847335e17fac107e8 (patch) | |
| tree | 32ce04a99fd92a089625e4abdc09d2373687f0ab /src/syntax | |
| parent | d2ba1b705ed7a532266294aa100f19423bb07f4d (diff) | |
Basic let bindings 🎞
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/expr.rs | 26 | ||||
| -rw-r--r-- | src/syntax/token.rs | 3 |
2 files changed, 29 insertions, 0 deletions
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<Expr>), /// A block expression: `{1 + 2}`. Block(Box<Expr>), + /// 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<Ident>, + /// The expression to assign to the pattern. + pub expr: Option<Box<Spanned<Expr>>>, +} + +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", |
