Created
June 16, 2023 13:39
-
-
Save srenatus/5361bfa8c4044aad97fa62af27d5bbcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/topdown/arithmetic.go b/topdown/arithmetic.go | |
index c75f816a4..b0061074f 100644 | |
--- a/topdown/arithmetic.go | |
+++ b/topdown/arithmetic.go | |
@@ -6,6 +6,7 @@ package topdown | |
import ( | |
"math/big" | |
+ "strconv" | |
"fmt" | |
@@ -113,6 +114,42 @@ func builtinArithArity2(fn arithArity2) BuiltinFunc { | |
} | |
} | |
+var Ints map[string]int | |
+var Snti map[int]string | |
+ | |
+func init() { | |
+ Ints = make(map[string]int, 256) | |
+ Snti = make(map[int]string, 256) | |
+ for i := 0; i < 256; i++ { | |
+ j := strconv.Itoa(i) | |
+ Ints[j] = i | |
+ Snti[i] = j | |
+ } | |
+} | |
+ | |
+func builtinArithPlus(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error { | |
+ n1, err := builtins.NumberOperand(operands[0].Value, 1) | |
+ if err != nil { | |
+ return err | |
+ } | |
+ n2, err := builtins.NumberOperand(operands[1].Value, 2) | |
+ if err != nil { | |
+ return err | |
+ } | |
+ if f, ok := Ints[string(n1)]; ok { | |
+ if g, ok := Ints[string(n2)]; ok { | |
+ if h, ok := Snti[f+g]; ok { | |
+ return iter(ast.NewTerm(ast.Number(h))) | |
+ } | |
+ } | |
+ } | |
+ f, err := arithPlus(builtins.NumberToFloat(n1), builtins.NumberToFloat(n2)) | |
+ if err != nil { | |
+ return err | |
+ } | |
+ return iter(ast.NewTerm(builtins.FloatToNumber(f))) | |
+} | |
+ | |
func builtinMinus(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error { | |
n1, ok1 := operands[0].Value.(ast.Number) | |
@@ -176,7 +213,7 @@ func init() { | |
RegisterBuiltinFunc(ast.Round.Name, builtinArithArity1(arithRound)) | |
RegisterBuiltinFunc(ast.Ceil.Name, builtinArithArity1(arithCeil)) | |
RegisterBuiltinFunc(ast.Floor.Name, builtinArithArity1(arithFloor)) | |
- RegisterBuiltinFunc(ast.Plus.Name, builtinArithArity2(arithPlus)) | |
+ RegisterBuiltinFunc(ast.Plus.Name, builtinArithPlus) | |
RegisterBuiltinFunc(ast.Minus.Name, builtinMinus) | |
RegisterBuiltinFunc(ast.Multiply.Name, builtinArithArity2(arithMultiply)) | |
RegisterBuiltinFunc(ast.Divide.Name, builtinArithArity2(arithDivide)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment