feat: Add tail call optimization

Introduce a TCO pass to the compiler and modify the VM to handle tail
calls.
This allows for deep recursion without stack overflow.
This commit is contained in:
Michael Schimmel
2026-02-17 23:10:55 +01:00
parent e6eaf70836
commit 98d3344912
7 changed files with 205 additions and 27 deletions
+7 -2
View File
@@ -6,6 +6,8 @@ use crate::ast::parser::Parser;
use crate::ast::compiler::binder::Binder;
use crate::ast::vm::VM;
use crate::ast::compiler::tco::TCO;
pub struct Environment {
pub global_names: Rc<RefCell<HashMap<String, (u32, StaticType)>>>,
pub global_values: Rc<RefCell<Vec<Value>>>,
@@ -115,9 +117,12 @@ impl Environment {
// 3. Bind & Type Check
let mut binder = Binder::new(self.global_names.clone());
let bound_ast = binder.bind(&untyped_ast)?;
let mut bound_ast = binder.bind(&untyped_ast)?;
// 4. Execute
// 4. Optimize
bound_ast = TCO::optimize(bound_ast);
// 5. Execute
let mut vm = VM::new(self.global_values.clone());
vm.run(&bound_ast)
}