From 06638574f712398750475be4e02c870d3aaa0753 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sat, 30 Sep 2023 23:41:12 -0500 Subject: [PATCH] :loud_sound: improve error message for this error with the missing math lib --- src/instruction.rs | 2 +- src/loader.rs | 2 -- src/main.rs | 12 +++++----- src/state.rs | 41 ++++++++++++++++++++++++--------- src/tests.rs | 56 +++++++++++++++++++++++++++++++++++++--------- 5 files changed, 84 insertions(+), 29 deletions(-) diff --git a/src/instruction.rs b/src/instruction.rs index 3cca3c1..fb76c0e 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -1,4 +1,4 @@ -#[derive (Debug, PartialEq)] +#[derive (Clone, Copy, Debug, PartialEq)] pub enum Instruction { Add (u8, u8, u8), AddI (u8, u8, i8), diff --git a/src/loader.rs b/src/loader.rs index 1611244..8b1ead7 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -221,8 +221,6 @@ pub fn parse_block (rdr: &mut R, blocks: &mut Vec ) { // Ignore things I haven't implemented yet - use crate::value::Value; - parse_string (rdr)?; // function name parse_int (rdr).unwrap (); // start line in source code parse_int (rdr).unwrap (); // last line in source code diff --git a/src/main.rs b/src/main.rs index d344c7f..7037b29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ mod value; #[cfg (test)] mod tests; -fn main () { +fn main () -> Result <(), state::StepError> { use state::State; let mut list_bytecode = false; @@ -86,17 +86,17 @@ fn main () { match input.as_str ().trim_end () { "c" => in_break = false, - "q" => return, + "q" => return Ok (()), "registers" => { dbg! (&vm.registers); continue; } "s" => { - match vm.step () { + match vm.step ()? { None => (), Some (state::StepOutput::ChunkReturned (x)) => { dbg! (x); - return; + return Ok (()); }, } continue; @@ -105,11 +105,11 @@ fn main () { } } - match vm.step () { + match vm.step ()? { None => (), Some (state::StepOutput::ChunkReturned (x)) => { dbg! (x); - return; + return Ok (()); }, } } diff --git a/src/state.rs b/src/state.rs index c01390d..ec4efbe 100644 --- a/src/state.rs +++ b/src/state.rs @@ -79,6 +79,13 @@ pub enum StepOutput { ChunkReturned (Vec ), } +#[derive (Debug)] +pub struct StepError { + frame: StackFrame, + inst: Instruction, + msg: &'static str, +} + impl <'a> State <'a> { pub fn new (chunk: &'a Chunk, upvalues: &'a [Value]) -> Self { Self { @@ -140,7 +147,17 @@ impl <'a> State <'a> { self.top - self.stack.last ().unwrap ().register_offset } - pub fn step (&mut self) -> Option { + fn make_step_error (&self, msg: &'static str, inst: &Instruction) -> StepError + { + StepError { + frame: self.stack.last ().unwrap ().clone (), + inst: inst.clone (), + msg, + } + } + + pub fn step (&mut self) -> Result