Skip to content

Commit

Permalink
Merge pull request #490 from HigherOrderCO/489-nested-map-setters
Browse files Browse the repository at this point in the history
#489 Map setters using map getters
  • Loading branch information
developedby authored May 24, 2024
2 parents cfd9b03 + c166c35 commit 534e71d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/"]

Expand Down
10 changes: 9 additions & 1 deletion src/imp/gen_map_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions tests/golden_tests/run_file/nested_map_set.bend
Original file line number Diff line number Diff line change
@@ -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]]
9 changes: 9 additions & 0 deletions tests/snapshots/run_file__nested_map_set.bend.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/nested_map_set.bend
---
NumScott:
100

Scott:
100

0 comments on commit 534e71d

Please sign in to comment.