about summary refs log tree commit diff
path: root/src/interpret
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-10-23 00:38:57 +0200
committerMel <einebeere@gmail.com>2021-10-23 00:38:57 +0200
commit32a04f1e677cfa2b4f62a2c1db358588b78d593d (patch)
tree30d7735b0a05dbe9ae70a260fd303aec45b2e129 /src/interpret
parent94e31fd0c39e5f5dbccd4a3a290e7fe5f06fc4a0 (diff)
downloadrabbithole-32a04f1e677cfa2b4f62a2c1db358588b78d593d.tar.zst
rabbithole-32a04f1e677cfa2b4f62a2c1db358588b78d593d.zip
Remove Plus unary op and implement other unary ops
Diffstat (limited to 'src/interpret')
-rw-r--r--src/interpret/value.rs15
-rw-r--r--src/interpret/walker.rs5
2 files changed, 17 insertions, 3 deletions
diff --git a/src/interpret/value.rs b/src/interpret/value.rs
index c0ac96e..27f2668 100644
--- a/src/interpret/value.rs
+++ b/src/interpret/value.rs
@@ -148,6 +148,21 @@ impl Value {
             _ => Err(anyhow!("Left operand can't be compared.")),
         }
     }
+
+    pub fn neg(self) -> Result<Value> {
+        match self {
+            Value::Float(float) => Ok(Value::Float(-float)),
+            Value::Int(int) => Ok(Value::Int(-int)),
+            _ => Err(anyhow!("Can't negate value.")),
+        }
+    }
+
+    pub fn not(self) -> Result<Value> {
+        match self {
+            Value::Bool(bool) => Ok(Value::Bool(bool)),
+            _ => Err(anyhow!("Can't flip non-bool value.")),
+        }
+    }
 }
 
 impl Display for Value {
diff --git a/src/interpret/walker.rs b/src/interpret/walker.rs
index e04b5a4..7684747 100644
--- a/src/interpret/walker.rs
+++ b/src/interpret/walker.rs
@@ -87,9 +87,8 @@ impl Walker {
                 let value = self.walk_expression(right)?;
 
                 let new_value = match op {
-                    UnOp::Plus => value,
-                    UnOp::Minus => todo!(),
-                    UnOp::Not => todo!("Implement boolean arithmetic."),
+                    UnOp::Minus => value.neg()?,
+                    UnOp::Not => value.not()?,
                 };
 
                 Ok(new_value)