From b498160c411e597e970b16c2dc52ee72d98f6336 Mon Sep 17 00:00:00 2001 From: jan Date: Fri, 3 Jan 2025 10:49:28 +0100 Subject: [PATCH] Improved output for division by zero --- .intentionally-empty-file.o | 0 Cargo.lock | 2 +- Cargo.toml | 2 +- src/engine.rs | 7 ++++--- 4 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 .intentionally-empty-file.o diff --git a/.intentionally-empty-file.o b/.intentionally-empty-file.o new file mode 100644 index 0000000..e69de29 diff --git a/Cargo.lock b/Cargo.lock index 907e175..8eeba79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1813,7 +1813,7 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "svc16" -version = "0.7.1" +version = "0.7.2" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index f3fa109..80c57fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "svc16" -version = "0.7.1" +version = "0.7.2" edition = "2021" authors = ["Jan Neuendorf"] description = "An emulator for a simple virtual computer" diff --git a/src/engine.rs b/src/engine.rs index 4b60e1b..b743314 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -33,8 +33,8 @@ pub struct Engine { #[derive(Debug, Error)] pub enum EngineError { - #[error("Division by zero")] - ZeroDivision, + #[error("Division by zero {0}/0")] + ZeroDivision(u16), #[error("Invalid instruction {0}")] InvalidInstruction(u16), @@ -152,7 +152,8 @@ impl Engine { } DIV => { if self.get(arg2) == 0 { - return Err(EngineError::ZeroDivision); + let numerator = self.get(arg1); + return Err(EngineError::ZeroDivision(numerator)); } else { self.set(arg3, self.get(arg1).wrapping_div(self.get(arg2))); self.advance_inst_ptr();