feat(tazjin/rlox): Implement simple arithmetic operators
Change-Id: I9873bcd281053f4e9820a5119f5992a0b8cb8cfc Reviewed-on: https://cl.tvl.fyi/c/depot/+/2417 Tested-by: BuildkiteCI Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
parent
7fb93fb490
commit
d6d3c12efb
4 changed files with 34 additions and 1 deletions
|
|
@ -23,6 +23,14 @@ impl VM {
|
|||
}
|
||||
}
|
||||
|
||||
macro_rules! binary_op {
|
||||
( $vm:ident, $op:tt ) => {{
|
||||
let a = $vm.pop();
|
||||
let b = $vm.pop();
|
||||
$vm.push(a $op b);
|
||||
}}
|
||||
}
|
||||
|
||||
impl VM {
|
||||
fn run(&mut self) -> LoxResult<()> {
|
||||
loop {
|
||||
|
|
@ -43,6 +51,16 @@ impl VM {
|
|||
let c = *self.chunk.constant(*idx);
|
||||
self.push(c);
|
||||
}
|
||||
|
||||
OpCode::OpNegate => {
|
||||
let v = self.pop();
|
||||
self.push(-v)
|
||||
}
|
||||
|
||||
OpCode::OpAdd => binary_op!(self, +),
|
||||
OpCode::OpSubtract => binary_op!(self, -),
|
||||
OpCode::OpMultiply => binary_op!(self, *),
|
||||
OpCode::OpDivide => binary_op!(self, /),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue