Allow bitwise operations on floats, according to Spec (and Javascript behavior)

This commit is contained in:
Dave Cunningham 2017-10-24 22:20:25 -04:00
parent e8f6d25f61
commit 3eaf189897
3 changed files with 4 additions and 15 deletions

View File

@ -478,15 +478,15 @@ var builtinExponent = liftNumeric(func(f float64) float64 {
func liftBitwise(f func(int64, int64) int64) func(*evaluator, potentialValue, potentialValue) (value, error) { func liftBitwise(f func(int64, int64) int64) func(*evaluator, potentialValue, potentialValue) (value, error) {
return func(e *evaluator, xp, yp potentialValue) (value, error) { return func(e *evaluator, xp, yp potentialValue) (value, error) {
x, err := e.evaluateInt64(xp) x, err := e.evaluateNumber(xp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
y, err := e.evaluateInt64(yp) y, err := e.evaluateNumber(yp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return makeDoubleCheck(e, float64(f(x, y))) return makeDoubleCheck(e, float64(f(int64(x.value), int64(y.value))))
} }
} }

View File

@ -110,8 +110,6 @@ func (e *evaluator) getInt64(val value) (int64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
// We conservatively convert ot int32, so that it can be machine-sized int
// on any machine. And it's used only for indexing anyway.
intNum := int64(num.value) intNum := int64(num.value)
if float64(intNum) != num.value { if float64(intNum) != num.value {
return 0, e.Error(fmt.Sprintf("Expected an integer, but got %v", num.value)) return 0, e.Error(fmt.Sprintf("Expected an integer, but got %v", num.value))

View File

@ -1,10 +1 @@
RUNTIME ERROR: Expected an integer, but got 1e+30 0
-------------------------------------------------
testdata/bitwise_and3:1:1-10 $
1e30 & 42
-------------------------------------------------
During evaluation