aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-02-18 00:17:54 -0500
committerBobby <[email protected]>2024-02-18 00:17:54 -0500
commit8415754488747d96d40ffc5ff77631ef3cfec44a (patch)
tree5b42d32c15002cf47ac792f7eb4d4790527c7c3e
parent57a79dc622311ec5beeafb8af8bcaeff940b4720 (diff)
downloadmana-8415754488747d96d40ffc5ff77631ef3cfec44a.tar.xz
mana-8415754488747d96d40ffc5ff77631ef3cfec44a.zip
Eval Minus Prefix Operator
-rw-r--r--evaluator/evaluator.go11
-rw-r--r--evaluator/evaluator_test.go2
2 files changed, 13 insertions, 0 deletions
diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go
index 0cc003f..b8940a9 100644
--- a/evaluator/evaluator.go
+++ b/evaluator/evaluator.go
@@ -57,6 +57,8 @@ func evalPrefixExpression(operator string, right object.Object) object.Object {
switch operator {
case "!":
return evalBangOperatorExpression(right)
+ case "-":
+ return evalMinusPrefixOperatorExpression(right)
default:
return NULL
}
@@ -74,3 +76,12 @@ func evalBangOperatorExpression(right object.Object) object.Object {
return FALSE
}
}
+
+func evalMinusPrefixOperatorExpression(right object.Object) object.Object {
+ if right.Type() != object.INTEGER_OBJ {
+ return NULL
+ }
+
+ value := right.(*object.Integer).Value
+ return &object.Integer{Value: -value}
+}
diff --git a/evaluator/evaluator_test.go b/evaluator/evaluator_test.go
index aa1833a..1221de6 100644
--- a/evaluator/evaluator_test.go
+++ b/evaluator/evaluator_test.go
@@ -15,6 +15,8 @@ func TestEvalIntegerExpression(t *testing.T) {
}{
{"5", 5},
{"10", 10},
+ {"-5", -5},
+ {"-10", -10},
}
for _, tt := range tests {