Infix evaluation operation now supported.

git-svn-id: https://svn.tlawal.org/svn/monkey@47 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
Tijani Lawal 2022-08-08 11:01:32 +00:00
parent 4b67c7d6e1
commit a170eb4450
2 changed files with 13 additions and 0 deletions

View File

@ -56,6 +56,8 @@ func eval_prefix_expression(operator string, right object.Object) object.Object
switch operator { switch operator {
case "!": case "!":
return eval_bang_operator_expression(right) return eval_bang_operator_expression(right)
case "-":
return eval_minus_prefix_operator_expression(right)
default: default:
return NULL return NULL
@ -77,3 +79,11 @@ func eval_bang_operator_expression(right object.Object) object.Object {
return FALSE return FALSE
} }
} }
func eval_minus_prefix_operator_expression(right object.Object) object.Object {
if right.Type() != object.INTEGER_OBJECT{
return NULL
}
value := right.(*object.Integer).Value
return &object.Integer{Value: -value}
}

View File

@ -14,6 +14,9 @@ func TestEvalIntegerExpression(l_test *testing.T) {
}{ }{
{"5", 5}, {"5", 5},
{"10", 10}, {"10", 10},
{"-10", -10},
{"-5", -5},
} }
for _, tt := range tests { for _, tt := range tests {