Skip to content

Commit

Permalink
👷 Evaluate boolean integer infix expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChmielewskiKamil committed Oct 4, 2024
1 parent 794b2e2 commit 2bfa855
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func evalIntegerInfixExpression(
case token.DIV:
result := new(big.Int).Div(&leftVal, &rightVal)
return &object.Integer{Value: *result}
case token.GREATER_THAN:
isGreater := leftVal.Cmp(&rightVal) == 1
return nativeBoolToBooleanObject(isGreater)
case token.LESS_THAN:
isLess := leftVal.Cmp(&rightVal) == -1
return nativeBoolToBooleanObject(isLess)
case token.EQUAL:
isEqual := leftVal.Cmp(&rightVal) == 0
return nativeBoolToBooleanObject(isEqual)
case token.NOT_EQUAL:
isNotEqual := leftVal.Cmp(&rightVal) != 0
return nativeBoolToBooleanObject(isNotEqual)
}
}

Expand Down
8 changes: 8 additions & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func Test_EvalBooleanExpression(t *testing.T) {
}{
{"true", true},
{"false", false},
{"1 > 2", false},
{"2 > 1", true},
{"2 < 3", true},
{"2 < 1", false},
{"3 == 3", true},
{"3 == 5", false},
{"3 != 4", true},
{"5 != 5", false},
}

for _, tt := range tests {
Expand Down

0 comments on commit 2bfa855

Please sign in to comment.