From 75398cbe82ef32c1eb1996808717e4e5d564f94b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 10 Feb 2021 22:35:48 +0100 Subject: add a simple stacktrace --- src/vm.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/vm.rs b/src/vm.rs index e5c7b2e..d238087 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -63,9 +63,10 @@ pub struct VM { pub print_blocks: bool, pub print_ops: bool, + runtime: bool, - extern_functions: Vec, + extern_functions: Vec, } #[derive(Eq, PartialEq)] @@ -92,6 +93,7 @@ impl VM { print_blocks: false, print_ops: false, + runtime: false, extern_functions: Vec::new() } @@ -158,10 +160,25 @@ impl VM { self.frame().block.borrow().ops[ip] } + fn print_stacktrace(&self) { + if !self.runtime { return; } + + println!("\n<{}>", "STACK".red()); + for (i, frame) in self.frames.iter().enumerate() { + println!(" {:>3}. {}:{:<4} in {:10}", + i, + frame.block.borrow().file.display(), + frame.block.borrow().line(self.frame().ip), + frame.block.borrow().name.blue()); + } + println!("") + } + /// Stop the program, violently fn crash_and_burn(&self) -> ! { self.print_stack(); println!("\n"); + self.print_stacktrace(); self.frame().block.borrow().debug_print(); println!(" ip: {}, line: {}\n", self.frame().ip.blue(), @@ -171,6 +188,7 @@ impl VM { fn error(&self, kind: ErrorKind, message: Option) -> Error { let frame = self.frames.last().unwrap(); + self.print_stacktrace(); Error { kind, file: frame.block.borrow().file.clone(), @@ -458,6 +476,7 @@ impl VM { self.extern_functions = prog.functions.clone(); self.stack.clear(); self.frames.clear(); + self.runtime = true; self.push(Value::Function(Vec::new(), Rc::clone(&block))); @@ -738,6 +757,7 @@ impl VM { self.blobs = prog.blobs.clone(); self.constants = prog.constants.clone(); self.strings = prog.strings.clone(); + self.runtime = false; self.extern_functions = prog.functions.clone(); for block in prog.blocks.iter() { -- cgit v1.2.1