Merge pull request #484 from HigherOrderCO/483-nested-map-gets

483 nested map gets
This commit is contained in:
Nicolas Abril 2024-05-24 12:09:13 +00:00 committed by GitHub
commit cdbaf66da6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 8 deletions

2
Cargo.lock generated
View File

@ -68,7 +68,7 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "bend-lang"
version = "0.2.17"
version = "0.2.18"
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.17"
version = "0.2.18"
edition = "2021"
exclude = ["tests/snapshots/"]

View File

@ -1,5 +1,3 @@
use std::collections::HashMap;
use crate::fun::Name;
use super::{AssignPattern, Definition, Expr, Stmt};
@ -116,15 +114,16 @@ impl Stmt {
}
}
type Substitutions = HashMap<Name, (Name, Box<Expr>)>;
type Substitutions = Vec<(Name, Name, Box<Expr>)>;
impl Expr {
fn substitute_map_gets(&mut self, id: &mut usize) -> Substitutions {
fn go(e: &mut Expr, substitutions: &mut Substitutions, id: &mut usize) {
match e {
Expr::MapGet { nam, key } => {
go(key, substitutions, id);
let new_var = gen_map_var(id);
substitutions.insert(new_var.clone(), (nam.clone(), key.clone()));
substitutions.push((new_var.clone(), nam.clone(), key.clone()));
*e = Expr::Var { nam: new_var };
}
Expr::Call { fun, args, kwargs } => {
@ -175,8 +174,8 @@ impl Expr {
}
fn gen_get(current: &mut Stmt, substitutions: Substitutions) -> Stmt {
substitutions.into_iter().fold(std::mem::take(current), |acc, next| {
let (var, (map_var, key)) = next;
substitutions.into_iter().rfold(std::mem::take(current), |acc, next| {
let (var, map_var, key) = next;
let map_get_call = Expr::Var { nam: Name::new("Map/get") };
let map_get_call = Expr::Call {
fun: Box::new(map_get_call),

View File

@ -0,0 +1,5 @@
def x:
return { 0: 1, 1: 42, 2: 0, 3: 2 }
def main():
return x[x[0]] + x[x[x[3]]]

View File

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