Merge pull request #490 from HigherOrderCO/489-nested-map-setters

#489 Map setters using map getters
This commit is contained in:
Nicolas Abril 2024-05-24 16:32:18 +00:00 committed by GitHub
commit 534e71dc55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 27 additions and 4 deletions

2
Cargo.lock generated
View File

@ -68,7 +68,7 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "bend-lang"
version = "0.2.19"
version = "0.2.20"
dependencies = [
"TSPL",
"arrayvec",

View File

@ -2,7 +2,7 @@
name = "bend-lang"
description = "A high-level, massively parallel programming language"
license = "Apache-2.0"
version = "0.2.19"
version = "0.2.20"
edition = "2021"
exclude = ["tests/snapshots/"]

View File

@ -15,14 +15,22 @@ impl Definition {
impl Stmt {
fn gen_map_get(&mut self, id: &mut usize) {
match self {
Stmt::Assign { pat: _, val, nxt } => {
Stmt::Assign { pat, val, nxt } => {
let key_substitutions =
if let AssignPattern::MapSet(_, key) = pat { key.substitute_map_gets(id) } else { Vec::new() };
if let Some(nxt) = nxt {
nxt.gen_map_get(id);
}
let substitutions = val.substitute_map_gets(id);
if !substitutions.is_empty() {
*self = gen_get(self, substitutions);
}
if !key_substitutions.is_empty() {
*self = gen_get(self, key_substitutions);
}
}
Stmt::Ask { pat: _, val, nxt } => {
nxt.gen_map_get(id);

View File

@ -46,9 +46,10 @@ pub struct MatchArm {
pub rgt: Stmt,
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub enum AssignPattern {
// "*"
#[default]
Eraser,
// [a-zA-Z_]+
Var(Name),

View File

@ -0,0 +1,5 @@
def main():
map = { 0: 1, 1: 10, 2: 0, 3: 1, 4: 3 }
map[map[0]] = 99
map[map[2]] = 1
return map[map[map[4]]] + map[map[2]]

View File

@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/nested_map_set.bend
---
NumScott:
100
Scott:
100