umx_compiler

UMX virtual machine "Monkey" interpreter / bytecode compiler
git clone git://bsandro.tech/umx_compiler
Log | Files | Refs

commit d068fe3b1d66c3d0500299e2c3ae1388b4d0cf0a
parent ca13c7ec05c2e8aee27f5545d05abe993851ba57
Author: bsandro <email@bsandro.tech>
Date:   Fri, 24 Jun 2022 00:11:27 +0300

eval conditional operators support

Diffstat:
Meval/eval.go | 18++++++++++++++++--
Meval/eval_test.go | 8++++++++
2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/eval/eval.go b/eval/eval.go @@ -81,10 +81,16 @@ func evalMinusPrefixOperatorExpression(right object.Object) object.Object { } func evalInfixExpression(operator string, left, right object.Object) object.Object { - if left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ { + switch { + case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ: return evalIntegerInfixExpression(operator, left, right) + case operator == "==": + return boolToObject(left == right) + case operator == "!=": + return boolToObject(left != right) + default: + return NULL } - return NULL } func evalIntegerInfixExpression(operator string, left, right object.Object) object.Object { @@ -99,6 +105,14 @@ func evalIntegerInfixExpression(operator string, left, right object.Object) obje return &object.Integer{Value: leftValue * rightValue} case "/": return &object.Integer{Value: leftValue / rightValue} + case "<": + return boolToObject(leftValue < rightValue) + case ">": + return boolToObject(leftValue > rightValue) + case "==": + return boolToObject(leftValue == rightValue) + case "!=": + return boolToObject(leftValue != rightValue) default: return NULL } diff --git a/eval/eval_test.go b/eval/eval_test.go @@ -54,6 +54,14 @@ func TestEvalBooleanExpression(t *testing.T) { }{ {"true", true}, {"false", false}, + {"1<2", true}, + {"2>3", false}, + {"1==1", true}, + {"10!=10", false}, + {"10==20", false}, + {"5!=6", true}, + {"(1<2)==true", true}, + {"(1<2)==false", false}, } for _, tt := range tests { evaluated := testEval(tt.input)