From e22d7f33d87bdc5b90726e038a63044ade8f3418 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Fri, 17 May 2024 13:07:09 -0300 Subject: [PATCH] Fix empty map parsing and improve syntax doc --- docs/syntax.md | 36 ++++++++++++++++++++++++++++++++++++ src/imp/parser.rs | 8 ++++++++ 2 files changed, 44 insertions(+) diff --git a/docs/syntax.md b/docs/syntax.md index 50debe22..53129d79 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -109,6 +109,19 @@ It's possible to assign to a pattern, like a tuple or superposition, which will (first, second) = (1, 2) ``` +### Use + +```rust +use x = 2 + 3 +return x + x +``` + +Inline copies of the declared bind, it is equivalent to this code: + +```rust +return ((2 + 3) + (2 + 3)) +``` + ### In-Place Operation ```python @@ -538,6 +551,16 @@ A List literal is surrounded by `[` `]`. The elements must be separated by `,`. It is desugared to constructor calls of the built-in type List, `List/cons(head, ~tail)` and `List/nil` . +### Map Literals + +```python +{ 0: 4, `hi`: "bye", 'c': 2 + 3 } +x[0] = 5 # Assigns the key 0 to the value 5 +return x[0] # Gets the value of the key 0 +``` + +Bend has a built-in binary tree map data structure where the key is a `u24`, meaning you can use numbers, characters, and symbols as keys. + ### List Comprehension ```python @@ -749,6 +772,19 @@ The let term will expects a binding value followed by a `next` term. Using `;` is optional. +### Use + +```rust +use x = (+ 2 3) +(+ x x) +``` + +Inline copies of the declared bind, it is equivalent to this code: + +```rust +(+ (+ 2 3) (+ 2 3)) +``` + ### Switch ```rust diff --git a/src/imp/parser.rs b/src/imp/parser.rs index 52e98097..d3991324 100644 --- a/src/imp/parser.rs +++ b/src/imp/parser.rs @@ -116,6 +116,10 @@ impl<'a> PyParser<'a> { } '{' => { self.advance_one(); + self.skip_trivia(); + if self.try_consume_exactly("}") { + return Ok(Expr::MapInit { entries: Vec::new() }); + } let head = self.parse_expr(false)?; self.skip_trivia(); if self.starts_with(":") { self.parse_map_init(head)? } else { self.parse_sup(head)? } @@ -127,6 +131,10 @@ impl<'a> PyParser<'a> { let val = STRINGS.get(str); Expr::Str { val } } + '\'' => { + let chr = self.parse_quoted_char()?; + Expr::Num { val: Num::U24(chr as u32 & 0x00ff_ffff) } + } '$' => { self.advance_one(); let nam = self.parse_bend_name()?;