[rlox] Add Not op

This commit is contained in:
ctsk
2023-10-08 21:31:07 +02:00
parent 8ace98a215
commit f617738674
3 changed files with 69 additions and 20 deletions

View File

@@ -51,6 +51,13 @@ impl VM {
.ok_or(self.type_err("Number", top_of_stack))
}
fn pop_bool(&mut self) -> Result<bool, VMError> {
let top_of_stack = self.pop()?;
top_of_stack
.as_bool()
.ok_or(self.type_err("Boolean", top_of_stack))
}
pub fn run(&mut self, chunk: &Chunk) -> Result<Option<Value>, VMError> {
while self.pc < chunk.code.len() {
let instr = chunk.code[self.pc];
@@ -83,6 +90,10 @@ impl VM {
let new_val = -self.pop_num()?;
self.push(new_val.into());
}
Op::Not => {
let new_val = !self.pop_bool()?;
self.push(new_val.into());
}
Op::Add | Op::Subtract | Op::Multiply | Op::Divide => {
let b = self.pop_num()?;
let a = self.pop_num()?;
@@ -158,4 +169,17 @@ mod tests {
vm.type_err("Number", Value::Nil)
);
}
#[test]
fn simple_booleans() {
let chunk = Chunk::new_with(
vec![Op::False, Op::Not],
vec![],
vec![],
);
let mut vm = VM::new();
vm.run(&chunk).unwrap();
assert_eq!(vm.stack[0], true.into());
}
}