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
+3
View File
@@ -70,6 +70,7 @@ pub enum Value {
Function(Rc<dyn Fn(Vec<Value>) -> Value>),
Object(Rc<dyn Object>), // For compiled Closures and other opaque types
Cell(Rc<RefCell<Value>>), // Boxed value for captures
TailCallRequest(Rc<dyn Object>, Vec<Value>), // Internal: For TCO
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -113,6 +114,7 @@ impl Value {
Value::Function { .. } => StaticType::Any, // Dynamic function
Value::Object(o) => StaticType::Object(o.type_name()),
Value::Cell(c) => c.borrow().static_type(),
Value::TailCallRequest(_, _) => panic!("Internal VM state leaked: TailCallRequest"),
}
}
}
@@ -147,6 +149,7 @@ impl fmt::Display for Value {
Value::Function(_) => write!(f, "<native fn>"),
Value::Object(o) => write!(f, "<{}>", o.type_name()),
Value::Cell(c) => write!(f, "{}", c.borrow()),
Value::TailCallRequest(_, _) => panic!("Internal VM state leaked: TailCallRequest"),
}
}
}