Skip to content

Commit 779ee07

Browse files
authored
compiler: calling const functions from const functions (#59)
* compiler: calling const functions from const functions * make clippy happy
1 parent bc00d36 commit 779ee07

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,6 @@ fn handle_inlined_functions_helper(
16391639
}
16401640

16411641
fn handle_const_arguments(program: &mut Program) {
1642-
// TODO this doesnt suupport const functions calling other const functions
16431642
let mut new_functions = BTreeMap::<String, Function>::new();
16441643
let constant_functions = program
16451644
.functions
@@ -1648,9 +1647,45 @@ fn handle_const_arguments(program: &mut Program) {
16481647
.map(|(name, func)| (name.clone(), func.clone()))
16491648
.collect::<BTreeMap<_, _>>();
16501649

1650+
// First pass: process non-const functions that call const functions
16511651
for func in program.functions.values_mut() {
1652-
handle_const_arguments_helper(&mut func.body, &constant_functions, &mut new_functions);
1652+
if !func.has_const_arguments() {
1653+
handle_const_arguments_helper(&mut func.body, &constant_functions, &mut new_functions);
1654+
}
16531655
}
1656+
1657+
// Process newly created const functions recursively until no more changes
1658+
let mut changed = true;
1659+
while changed {
1660+
changed = false;
1661+
let mut additional_functions = BTreeMap::new();
1662+
1663+
// Collect all function names to process
1664+
let function_names: Vec<String> = new_functions.keys().cloned().collect();
1665+
1666+
for name in function_names {
1667+
if let Some(func) = new_functions.get_mut(&name) {
1668+
let initial_count = additional_functions.len();
1669+
handle_const_arguments_helper(
1670+
&mut func.body,
1671+
&constant_functions,
1672+
&mut additional_functions,
1673+
);
1674+
if additional_functions.len() > initial_count {
1675+
changed = true;
1676+
}
1677+
}
1678+
}
1679+
1680+
// Add any newly discovered functions
1681+
for (name, func) in additional_functions {
1682+
if let std::collections::btree_map::Entry::Vacant(e) = new_functions.entry(name) {
1683+
e.insert(func);
1684+
changed = true;
1685+
}
1686+
}
1687+
}
1688+
16541689
for (name, func) in new_functions {
16551690
assert!(!program.functions.contains_key(&name),);
16561691
program.functions.insert(name, func);

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,26 @@ fn test_match() {
441441
// "#;
442442
// compile_and_run(program, &[], &[]);
443443
// }
444+
445+
#[test]
446+
fn test_const_functions_calling_const_functions() {
447+
// Test that const functions can call other const functions
448+
let program = r#"
449+
fn main() {
450+
y = compute_value(3);
451+
print(y);
452+
return;
453+
}
454+
455+
fn compute_value(const n) -> 1 {
456+
result = complex_computation(n, 5);
457+
return result;
458+
}
459+
460+
fn complex_computation(const a, const b) -> 1 {
461+
return a * a + b * b;
462+
}
463+
"#;
464+
465+
compile_and_run(program, &[], &[], false);
466+
}

0 commit comments

Comments
 (0)