diff --git a/evaluator/digit_test.go b/evaluator/digit_test.go index 6a8e4bd..b537d57 100644 --- a/evaluator/digit_test.go +++ b/evaluator/digit_test.go @@ -73,6 +73,10 @@ func TestEvalDigitExpression(t *testing.T) { `4 / 2`, 2, }, + { + `100 / 8`, + 12.5, + }, { `4 % 2`, 0, diff --git a/evaluator/hash_test.go b/evaluator/hash_test.go index 351451f..d853fbd 100644 --- a/evaluator/hash_test.go +++ b/evaluator/hash_test.go @@ -11,7 +11,7 @@ func TestHashLiterals(t *testing.T) { { "one": 10 - 9, two: 1 + 1, - "thr" + "ee": 6 / 2, + "thr" + "ee": 3, 4: 4, true: 5, false: 6 diff --git a/evaluator/integer.go b/evaluator/integer.go index 7b722f4..61e00df 100644 --- a/evaluator/integer.go +++ b/evaluator/integer.go @@ -1,6 +1,9 @@ package evaluator -import "ninja/object" +import ( + "math" + "ninja/object" +) func evalIntegerInfixExpression( operator string, @@ -23,7 +26,16 @@ func evalIntegerInfixExpression( case "%": return &object.Integer{Value: leftVal % rightVal} case "/": - return &object.Integer{Value: leftVal / rightVal} + left := float64(leftVal) + right := float64(rightVal) + total := left / right + + // @todo check if we need a ELIPSON var or if there any way of doing + if math.Round(total)-total <= 0.00000000000001 { + return &object.Integer{Value: int64(total)} + } + + return &object.Float{Value: left / right} case "<": return nativeBoolToBooleanObject(leftObject.Compare(rightObject) == -1) case ">":