diff options
| author | Bobby <[email protected]> | 2024-03-01 20:13:21 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-03-01 20:15:56 +0000 |
| commit | 4a930511b3aad05682aae69cbfbf7e7273e20264 (patch) | |
| tree | 9c9d8462de4d4f36a2f7cef74b901dd1d3d1ca86 | |
| parent | 6022da9a279b9a95bb839de765cf5e27a4c9138b (diff) | |
| download | mana-4a930511b3aad05682aae69cbfbf7e7273e20264.tar.xz mana-4a930511b3aad05682aae69cbfbf7e7273e20264.zip | |
Infix Operator Evaluation for Boolean Comparisons
| -rw-r--r-- | evaluator/evaluator.go | 19 | ||||
| -rw-r--r-- | evaluator/evaluator_test.go | 23 |
2 files changed, 37 insertions, 5 deletions
diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 5d69e2b..479e244 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -7,7 +7,7 @@ import ( var ( NULL = &object.Null{} - TRUE = &object.Boolean{Value: true} + TRUE = &object.Boolean{Value: true} FALSE = &object.Boolean{Value: false} ) @@ -31,7 +31,7 @@ func Eval(node ast.Node) object.Object { case *ast.PrefixExpression: right := Eval(node.Right) return evalPrefixExpression(node.Operator, right) - + case *ast.InfixExpression: left := Eval(node.Left) right := Eval(node.Right) @@ -73,6 +73,10 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje switch { case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ: return evalIntegerInfixExpression(operator, left, right) + case operator == "==": + return nativeBoolToBooleanObject(left == right) + case operator == "!=": + return nativeBoolToBooleanObject(left != right) default: return NULL } @@ -112,7 +116,18 @@ func evalIntegerInfixExpression(operator string, left, right object.Object) obje case "*": return &object.Integer{Value: leftValue * rightValue} case "/": + if rightValue == 0 { + return NULL + } return &object.Integer{Value: leftValue / rightValue} + case "<": + return nativeBoolToBooleanObject(leftValue < rightValue) + case ">": + return nativeBoolToBooleanObject(leftValue > rightValue) + case "==": + return nativeBoolToBooleanObject(leftValue == rightValue) + case "!=": + return nativeBoolToBooleanObject(leftValue != rightValue) default: return NULL } diff --git a/evaluator/evaluator_test.go b/evaluator/evaluator_test.go index f0a5180..e5bc66c 100644 --- a/evaluator/evaluator_test.go +++ b/evaluator/evaluator_test.go @@ -10,7 +10,7 @@ import ( func TestEvalIntegerExpression(t *testing.T) { tests := []struct { - input string + input string expected int64 }{ {"5", 5}, @@ -38,11 +38,28 @@ func TestEvalIntegerExpression(t *testing.T) { func TestEvalBooleanExpression(t *testing.T) { tests := []struct { - input string + input string expected bool }{ {"true", true}, {"false", false}, + {"1 < 2", true}, + {"1 > 2", false}, + {"1 < 1", false}, + {"1 > 1", false}, + {"1 == 1", true}, + {"1 != 1", false}, + {"1 == 2", false}, + {"1 != 2", true}, + {"true == true", true}, + {"false == false", true}, + {"true == false", false}, + {"true != false", true}, + {"false != true", true}, + {"(1 < 2) == true", true}, + {"(1 < 2) == false", false}, + {"(1 > 2) == true", false}, + {"(1 > 2) == false", true}, } for _, tt := range tests { @@ -53,7 +70,7 @@ func TestEvalBooleanExpression(t *testing.T) { func TestBangOperator(t *testing.T) { tests := []struct { - input string + input string expected bool }{ {"!true", false}, |
