Skip to content

Commit

Permalink
Add renames that were needed but missed downstream of assign
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Oct 24, 2023
1 parent dbfc6ff commit 619d6d4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
28 changes: 28 additions & 0 deletions resources/tests/cse-complex-21.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(mod (X)
(include *standard-cl-21*)

(defun mess (X) ;; 11 41
(assign
Y (+ X 1) ;; 12 42
Z (+ Y 2) ;; 14 44

(if (= X 11)
(assign
Y (* X 2) ;; 22 82
Z (+ Y 1) ;; 23 83

(* Y Z) ;; 22 * 23 = 506
)

(assign
Y (* X 3) ;; 33 123
Z (+ Y 2) ;; 35 125

(* Y Z) ;; 123 * 125 = 15375
)
)
)
)

(mess X)
)
9 changes: 6 additions & 3 deletions src/compiler/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn rename_assign_bindings(
) -> Result<(BodyForm, Vec<Rc<Binding>>), CompileErr> {
// Order the bindings.
let sorted_bindings = toposort_assign_bindings(l, bindings)?;
let mut renames = HashMap::new();
let mut renames: HashMap<Vec<u8>, Vec<u8>> = HashMap::new();
// Process in reverse order so we rename from inner to outer.
let bindings_to_rename: Vec<TopoSortItem<_>> = sorted_bindings.to_vec();
let renamed_bindings = map_m_reverse(
Expand All @@ -175,9 +175,11 @@ pub fn rename_assign_bindings(
for (name, renamed) in new_names.iter() {
renames.insert(name.clone(), renamed.clone());
}

let renamed_in_body = Rc::new(rename_args_bodyform(b.body.borrow())?);
Ok(Rc::new(Binding {
pattern: BindingPattern::Complex(rename_in_cons(&renames, p.clone(), false)),
body: Rc::new(rename_in_bodyform(&renames, b.body.clone())?),
body: Rc::new(rename_in_bodyform(&renames, renamed_in_body)?),
..b.clone()
}))
} else {
Expand All @@ -186,7 +188,8 @@ pub fn rename_assign_bindings(
},
&bindings_to_rename,
)?;
Ok((rename_in_bodyform(&renames, body)?, renamed_bindings))
let new_body = Rc::new(rename_args_bodyform(body.borrow())?);
Ok((rename_in_bodyform(&renames, new_body)?, renamed_bindings))
}

fn rename_in_bodyform(
Expand Down
22 changes: 22 additions & 0 deletions src/tests/classic/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,3 +1343,25 @@ fn test_classic_obeys_operator_choice_at_compile_time_version_0() {
.to_string();
assert_eq!(compiled, "FAIL: unimplemented operator 48");
}

#[test]
fn test_assign_rename_tricky() {
let filename = "resources/tests/cse-complex-21.clsp";
let program = do_basic_run(&vec!["run".to_string(), filename.to_string()])
.trim()
.to_string();

let run_result_11 = do_basic_brun(&vec![
"brun".to_string(),
program.clone(),
"(11)".to_string(),
])
.trim()
.to_string();
assert_eq!(run_result_11, "506");

let run_result_41 = do_basic_brun(&vec!["brun".to_string(), program, "(41)".to_string()])
.trim()
.to_string();
assert_eq!(run_result_41, "15375");
}

0 comments on commit 619d6d4

Please sign in to comment.