improve panic traces; tail calls work for simpel calls

This commit is contained in:
Scott Richmond 2025-06-19 21:13:18 -04:00
parent 2f5dab84a7
commit 7e4ddd3dc4
2 changed files with 18 additions and 29 deletions

View File

@ -75,14 +75,12 @@ pub fn run(src: &'static str) {
pub fn main() { pub fn main() {
env::set_var("RUST_BACKTRACE", "1"); env::set_var("RUST_BACKTRACE", "1");
let src = " let src = "
fn nope () -> nil fn recursive {
fn foo (_, _, _) -> { (0) -> :done
nope () (n) -> recursive (0)
:foo
} }
fn bar () -> foo (1, 2, 3)
bar () recursive (1)
"; ";
run(src); run(src);
} }

View File

@ -164,21 +164,28 @@ impl Vm {
self.result.as_ref().unwrap() self.result.as_ref().unwrap()
} }
pub fn print_call_stack(&mut self) { pub fn call_stack(&mut self) -> String {
println!("in {}", self.frame.function.as_fn().name()); let mut stack = format!(" calling {}", self.frame.function.show());
for frame in self.call_stack.iter() { for frame in self.call_stack.iter().rev() {
println!(" in {}", frame.function.as_fn().name()); let mut name = frame.function.show();
name = if name == "fn user script" {
"user script".to_string()
} else {
name
};
stack = format!("{stack}\n from {name}");
} }
stack
} }
pub fn panic(&mut self, msg: &'static str) { pub fn panic(&mut self, msg: &'static str) {
self.result = Some(Err(Panic::Str(msg))); let msg = format!("{msg}\nPanic traceback:\n{}", self.call_stack());
self.print_call_stack(); self.result = Some(Err(Panic::String(msg)));
} }
pub fn panic_with(&mut self, msg: String) { pub fn panic_with(&mut self, msg: String) {
let msg = format!("{msg}\nPanic traceback:\n{}", self.call_stack());
self.result = Some(Err(Panic::String(msg))); self.result = Some(Err(Panic::String(msg)));
self.print_call_stack();
} }
pub fn interpret(&mut self) { pub fn interpret(&mut self) {
@ -539,21 +546,6 @@ impl Vm {
self.push(splatted); self.push(splatted);
self.ip += 2; self.ip += 2;
} }
// PushDict => {
// let dict_len = self.chunk().bytecode[self.ip + 1] as usize * 2;
// let dict_members = self.stack.split_off(self.stack.len() - dict_len);
// let mut dict = HashMap::new();
// let mut dict_iter = dict_members.iter();
// while let Some(kw) = dict_iter.next() {
// let Value::Keyword(key) = kw else {
// unreachable!()
// };
// let value = dict_iter.next().unwrap();
// dict.insert(*key, value.clone());
// }
// self.push(Value::Dict(Box::new(dict)));
// self.ip += 2;
// }
PushDict => { PushDict => {
self.push(Value::Dict(Box::new(HashMap::new()))); self.push(Value::Dict(Box::new(HashMap::new())));
self.ip += 1; self.ip += 1;
@ -657,7 +649,6 @@ impl Vm {
let high = self.chunk().bytecode[self.ip + 1]; let high = self.chunk().bytecode[self.ip + 1];
let low = self.chunk().bytecode[self.ip + 2]; let low = self.chunk().bytecode[self.ip + 2];
let jump_len = combine_bytes(high, low); let jump_len = combine_bytes(high, low);
// let jump_len = self.chunk().bytecode[self.ip + 1] as usize;
if self.matches { if self.matches {
self.ip += jump_len + 3; self.ip += jump_len + 3;
} else { } else {