Skip to content

Commit

Permalink
New subtract immediate instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
mliezun committed Jan 20, 2024
1 parent 2b3610a commit 360be4c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum OpCode {
GetObj,
SetObj,
Addi,
Subi,
GetIter,
GetIterk,
GetIteri,
Expand Down
33 changes: 33 additions & 0 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,39 @@ impl VM {
}
pc += 1;
}
OpCode::Subi => {
let val_b = self
.activation_records
.get_mut(sp + inst.b as usize)
.unwrap()
.clone();
if let Record::Val(Value::Number(n)) = val_b {
self.activation_records[sp + inst.a as usize] = Record::Val(Value::Number(NumberValue {
n: n.n - inst.c as f64
}));
} else {
match val_b
.as_val()
.sub(&mut Value::Number(NumberValue { n: inst.c as f64 }))
{
Ok(v) => {
self.activation_records[sp + inst.a as usize] = Record::Val(v);
}
Err(e) => {
throw_exception!(
self,
this,
original_instructions,
original_instructions_data,
pc,
sp,
e
);
}
}
}
pc += 1;
}
OpCode::GetIter => {
let val_b = self
.activation_records
Expand Down

0 comments on commit 360be4c

Please sign in to comment.