aboutsummaryrefslogtreecommitdiffstats
path: root/src/vm.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/vm.rs')
-rw-r--r--src/vm.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/vm.rs b/src/vm.rs
index 6b64928..7ab2235 100644
--- a/src/vm.rs
+++ b/src/vm.rs
@@ -10,10 +10,13 @@ pub enum Value {
pub enum Op {
Pop,
Constant(Value),
+
Add,
Sub,
Mul,
Div,
+ Neg,
+
Print,
Return,
}
@@ -91,6 +94,15 @@ impl VM {
self.stack.push(value);
}
+ Op::Neg => {
+ let a = self.stack.pop().unwrap();
+ match a {
+ Value::Float(a) => self.stack.push(Value::Float(-a)),
+ Value::Int(a) => self.stack.push(Value::Int(-a)),
+ _ => unimplemented!("Cannot negate '{:?}'.", a),
+ }
+ }
+
Op::Add => {
let b = self.stack.pop().unwrap();
let a = self.stack.pop().unwrap();