diff --git a/rust/src/chunk.rs b/rust/src/chunk.rs index 16c41c2..9274b3f 100644 --- a/rust/src/chunk.rs +++ b/rust/src/chunk.rs @@ -5,6 +5,7 @@ use crate::value::Value; #[derive(Debug)] pub enum OpCode { Constant(usize), + Negate, Return, } @@ -62,13 +63,13 @@ impl fmt::Display for DisassembledInstruction<'_> { write!(f, "{:>4} ", line)?; } match self.chunk.code[self.i] { - OpCode::Constant(constant) => { - let value = self.chunk.constants[constant]; - write!(f, "{:<16} {:4} '{}'", "OP_CONSTANT", constant, value)?; - } - OpCode::Return => { - write!(f, "OP_RETURN")?; - } + OpCode::Constant(constant) => write!( + f, + "{:<16} {:4} '{}'", + "OP_CONSTANT", constant, self.chunk.constants[constant] + )?, + OpCode::Negate => write!(f, "OP_NEGATE")?, + OpCode::Return => write!(f, "OP_RETURN")?, } Ok(()) diff --git a/rust/src/main.rs b/rust/src/main.rs index bc6b15a..153f8eb 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -19,6 +19,7 @@ fn main() -> Result<()> { let mut chunk = Chunk::default(); chunk.push_constant(1.2, 123); + chunk.push(OpCode::Negate, 123); chunk.push(OpCode::Return, 123); chunk.disassemble("test chunk"); diff --git a/rust/src/vm.rs b/rust/src/vm.rs index e26bb5f..efc7910 100644 --- a/rust/src/vm.rs +++ b/rust/src/vm.rs @@ -42,6 +42,10 @@ impl VM { let value = self.chunk.constants[*constant]; self.push(value); } + OpCode::Negate => { + let value = self.pop(); + self.push(-value); + } OpCode::Return => { println!("{}", self.pop()); return Ok(());