Skip to content

Commit d23f155

Browse files
authored
compiler: calling inline functions from inline functions (#61)
* compiler: calling inline functions from inline functions * fmt
1 parent 0c5db92 commit d23f155

File tree

2 files changed

+107
-17
lines changed

2 files changed

+107
-17
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,28 +1521,68 @@ fn handle_inlined_functions(program: &mut Program) {
15211521
);
15221522
}
15231523

1524-
for func in program.functions.values_mut() {
1525-
if func.inlined {
1526-
let mut func_called = Vec::new();
1527-
get_function_called(&func.body, &mut func_called);
1528-
for func_called in func_called {
1529-
assert!(
1530-
!inlined_functions.contains_key(&func_called),
1531-
"Inlined functions calling other inlined functions are not supported yet"
1524+
// Process inline functions iteratively to handle dependencies
1525+
// Repeat until all inline function calls are resolved
1526+
let mut max_iterations = 10;
1527+
while max_iterations > 0 {
1528+
let mut any_changes = false;
1529+
1530+
// Process non-inlined functions
1531+
for func in program.functions.values_mut() {
1532+
if !func.inlined {
1533+
let mut counter1 = Counter::new();
1534+
let mut counter2 = Counter::new();
1535+
let old_body = func.body.clone();
1536+
1537+
handle_inlined_functions_helper(
1538+
&mut func.body,
1539+
&inlined_functions,
1540+
&mut counter1,
1541+
&mut counter2,
15321542
);
1543+
1544+
if func.body != old_body {
1545+
any_changes = true;
1546+
}
15331547
}
1534-
} else {
1535-
handle_inlined_functions_helper(
1536-
&mut func.body,
1537-
&inlined_functions,
1538-
&mut Counter::new(),
1539-
&mut Counter::new(),
1540-
);
15411548
}
1549+
1550+
// Process inlined functions that may call other inlined functions
1551+
// We need to update them so that when they get inlined later, they don't have unresolved calls
1552+
for func in program.functions.values_mut() {
1553+
if func.inlined {
1554+
let mut counter1 = Counter::new();
1555+
let mut counter2 = Counter::new();
1556+
let old_body = func.body.clone();
1557+
1558+
handle_inlined_functions_helper(
1559+
&mut func.body,
1560+
&inlined_functions,
1561+
&mut counter1,
1562+
&mut counter2,
1563+
);
1564+
1565+
if func.body != old_body {
1566+
any_changes = true;
1567+
}
1568+
}
1569+
}
1570+
1571+
if !any_changes {
1572+
break;
1573+
}
1574+
1575+
max_iterations -= 1;
15421576
}
15431577

1544-
for func in inlined_functions.keys() {
1545-
program.functions.remove(func);
1578+
assert!(
1579+
max_iterations > 0,
1580+
"Too many iterations processing inline functions"
1581+
);
1582+
1583+
// Remove all inlined functions from the program (they've been inlined)
1584+
for func_name in inlined_functions.keys() {
1585+
program.functions.remove(func_name);
15461586
}
15471587
}
15481588

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,53 @@ fn test_const_functions_calling_const_functions() {
464464

465465
compile_and_run(program, &[], &[], false);
466466
}
467+
468+
#[test]
469+
fn test_inline_functions_calling_inline_functions() {
470+
let program = r#"
471+
fn main() {
472+
x = double(3);
473+
y = quad(x);
474+
print(y);
475+
return;
476+
}
477+
478+
fn double(a) inline -> 1 {
479+
return a + a;
480+
}
481+
482+
fn quad(b) inline -> 1 {
483+
result = double(b);
484+
return result + result;
485+
}
486+
"#;
487+
488+
compile_and_run(program, &[], &[], false);
489+
}
490+
491+
#[test]
492+
fn test_nested_inline_functions() {
493+
let program = r#"
494+
fn main() {
495+
result = level_one(3);
496+
print(result);
497+
return;
498+
}
499+
500+
fn level_one(x) inline -> 1 {
501+
result = level_two(x);
502+
return result;
503+
}
504+
505+
fn level_two(y) inline -> 1 {
506+
result = level_three(y);
507+
return result;
508+
}
509+
510+
fn level_three(z) inline -> 1 {
511+
return z * z * z;
512+
}
513+
"#;
514+
515+
compile_and_run(program, &[], &[], false);
516+
}

0 commit comments

Comments
 (0)