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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user