Implement getter generation for map setter keys

This commit is contained in:
imaqtkatt 2024-05-24 12:22:42 -03:00
parent cfd9b03efd
commit b436232a3f
4 changed files with 25 additions and 2 deletions

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