mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-10-26 05:50:18 +03:00
Merge pull request #484 from HigherOrderCO/483-nested-map-gets
483 nested map gets
This commit is contained in:
commit
cdbaf66da6
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -68,7 +68,7 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
|
||||
|
||||
[[package]]
|
||||
name = "bend-lang"
|
||||
version = "0.2.17"
|
||||
version = "0.2.18"
|
||||
dependencies = [
|
||||
"TSPL",
|
||||
"arrayvec",
|
||||
|
@ -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/"]
|
||||
|
||||
|
@ -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),
|
||||
|
5
tests/golden_tests/run_file/nested_map_get.bend
Normal file
5
tests/golden_tests/run_file/nested_map_get.bend
Normal 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]]]
|
9
tests/snapshots/run_file__nested_map_get.bend.snap
Normal file
9
tests/snapshots/run_file__nested_map_get.bend.snap
Normal 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
|
Loading…
Reference in New Issue
Block a user