Skip to content

Commit e4dc07f

Browse files
committed
Use int instead of int64 as default integer type
1 parent 8cb3965 commit e4dc07f

File tree

10 files changed

+138
-140
lines changed

10 files changed

+138
-140
lines changed

ast/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type IntegerNode struct {
3030
l helper.Location
3131
t reflect.Type
3232

33-
Value int64
33+
Value int
3434
Certain bool
3535
}
3636

bench_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package expr_test
2+
3+
import (
4+
"github.com/antonmedv/expr"
5+
"github.com/antonmedv/expr/vm"
6+
"testing"
7+
)
8+
9+
func Benchmark_expr(b *testing.B) {
10+
params := make(map[string]interface{})
11+
params["Origin"] = "MOW"
12+
params["Country"] = "RU"
13+
params["Adults"] = int64(1)
14+
params["Value"] = int64(100)
15+
16+
program, err := expr.Compile(`(Origin == "MOW" || Country == "RU") && (Value >= 100 || Adults == 1)`, expr.Env(params))
17+
if err != nil {
18+
b.Fatal(err)
19+
}
20+
21+
var out interface{}
22+
23+
for n := 0; n < b.N; n++ {
24+
out, err = vm.Run(program, params)
25+
}
26+
27+
if err != nil {
28+
b.Fatal(err)
29+
}
30+
if !out.(bool) {
31+
b.Fail()
32+
}
33+
}
34+
35+
func Benchmark_filter(b *testing.B) {
36+
params := make(map[string]interface{})
37+
params["max"] = 50
38+
39+
program, err := expr.Compile(`filter(1..100, {# > max})`, expr.Env(params))
40+
if err != nil {
41+
b.Fatal(err)
42+
}
43+
44+
for n := 0; n < b.N; n++ {
45+
_, err = vm.Run(program, params)
46+
}
47+
48+
if err != nil {
49+
b.Fatal(err)
50+
}
51+
}
52+
53+
func Benchmark_access(b *testing.B) {
54+
type Price struct {
55+
Value int64
56+
}
57+
type Env struct {
58+
Price Price
59+
}
60+
61+
program, err := expr.Compile(`Price.Value > 0`, expr.Env(Env{}))
62+
if err != nil {
63+
b.Fatal(err)
64+
}
65+
66+
env := Env{Price: Price{Value: 1}}
67+
68+
for n := 0; n < b.N; n++ {
69+
_, err = vm.Run(program, env)
70+
}
71+
72+
if err != nil {
73+
b.Fatal(err)
74+
}
75+
}

checker/checker_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ func TestCheck_error(t *testing.T) {
326326
},
327327
{
328328
"1 and false",
329-
"invalid operation: and (mismatched types int64 and bool)",
329+
"invalid operation: and (mismatched types int and bool)",
330330
},
331331
{
332332
"true or 0",
333-
"invalid operation: or (mismatched types bool and int64)",
333+
"invalid operation: or (mismatched types bool and int)",
334334
},
335335
{
336336
"not IntPtr",
@@ -394,19 +394,19 @@ func TestCheck_error(t *testing.T) {
394394
},
395395
{
396396
"1 in Foo",
397-
"invalid operation: in (mismatched types int64 and *checker_test.foo)",
397+
"invalid operation: in (mismatched types int and *checker_test.foo)",
398398
},
399399
{
400400
"1 + ''",
401-
`invalid operation: + (mismatched types int64 and string)`,
401+
`invalid operation: + (mismatched types int and string)`,
402402
},
403403
{
404404
`all(ArrayOfFoo, {#.Fn() < 0})`,
405-
`invalid operation: < (mismatched types bool and int64)`,
405+
`invalid operation: < (mismatched types bool and int)`,
406406
},
407407
{
408408
`map(Any, {0})[0] + "str"`,
409-
`invalid operation: + (mismatched types int64 and string)`,
409+
`invalid operation: + (mismatched types int and string)`,
410410
},
411411
}
412412

checker/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
var (
99
nilType = reflect.TypeOf(nil)
1010
boolType = reflect.TypeOf(true)
11-
integerType = reflect.TypeOf(int64(0))
11+
integerType = reflect.TypeOf(int(0))
1212
floatType = reflect.TypeOf(float64(0))
1313
stringType = reflect.TypeOf("")
1414
arrayType = reflect.TypeOf([]interface{}{})

compiler/compiler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,19 @@ func (c *compiler) IntegerNode(node *ast.IntegerNode) {
174174
c.emit(OpConst, c.makeConstant(float64(node.Value))...)
175175

176176
case reflect.Int:
177-
c.emit(OpConst, c.makeConstant(int(node.Value))...)
177+
if node.Value <= math.MaxUint16 {
178+
c.emit(OpPush, encode(uint16(node.Value))...)
179+
} else {
180+
c.emit(OpConst, c.makeConstant(node.Value)...)
181+
}
178182
case reflect.Int8:
179183
c.emit(OpConst, c.makeConstant(int8(node.Value))...)
180184
case reflect.Int16:
181185
c.emit(OpConst, c.makeConstant(int16(node.Value))...)
182186
case reflect.Int32:
183187
c.emit(OpConst, c.makeConstant(int32(node.Value))...)
184188
case reflect.Int64:
185-
if node.Value <= math.MaxUint16 {
186-
c.emit(OpPush, encode(uint16(node.Value))...)
187-
} else {
188-
c.emit(OpConst, c.makeConstant(node.Value)...)
189-
}
189+
c.emit(OpConst, c.makeConstant(int64(node.Value))...)
190190

191191
case reflect.Uint:
192192
c.emit(OpConst, c.makeConstant(uint(node.Value))...)

expr_test.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,8 @@ import (
66
"github.com/antonmedv/expr"
77
"github.com/antonmedv/expr/vm"
88
"strings"
9-
"testing"
109
)
1110

12-
func Benchmark_expr(b *testing.B) {
13-
params := make(map[string]interface{})
14-
params["Origin"] = "MOW"
15-
params["Country"] = "RU"
16-
params["Adults"] = int64(1)
17-
params["Value"] = int64(100)
18-
19-
program, err := expr.Compile(`(Origin == "MOW" || Country == "RU") && (Value >= 100 || Adults == 1)`, expr.Env(params))
20-
if err != nil {
21-
b.Fatal(err)
22-
}
23-
24-
var out interface{}
25-
26-
for n := 0; n < b.N; n++ {
27-
out, err = vm.Run(program, params)
28-
}
29-
30-
if err != nil {
31-
b.Fatal(err)
32-
}
33-
if !out.(bool) {
34-
b.Fail()
35-
}
36-
}
37-
38-
func Benchmark_filter(b *testing.B) {
39-
params := make(map[string]interface{})
40-
params["max"] = 50
41-
42-
program, err := expr.Compile(`filter(1..100, {# > max})`, expr.Env(params))
43-
if err != nil {
44-
b.Fatal(err)
45-
}
46-
47-
for n := 0; n < b.N; n++ {
48-
_, err = vm.Run(program, params)
49-
}
50-
51-
if err != nil {
52-
b.Fatal(err)
53-
}
54-
}
55-
56-
func Benchmark_access(b *testing.B) {
57-
type Price struct {
58-
Value int64
59-
}
60-
type Env struct {
61-
Price Price
62-
}
63-
64-
program, err := expr.Compile(`Price.Value > 0`, expr.Env(Env{}))
65-
if err != nil {
66-
b.Fatal(err)
67-
}
68-
69-
env := Env{Price: Price{Value: 1}}
70-
71-
for n := 0; n < b.N; n++ {
72-
_, err = vm.Run(program, env)
73-
}
74-
75-
if err != nil {
76-
b.Fatal(err)
77-
}
78-
}
79-
8011
func ExampleEval() {
8112
output, err := expr.Eval("'hello world'", nil)
8213
if err != nil {

parser/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ func (p *parser) EnterIntegerLiteral(ctx *gen.IntegerLiteralContext) {
103103
p.reportError(ctx, "parse error: invalid int literal")
104104
return
105105
}
106-
p.push(&ast.IntegerNode{Value: i}).SetLocation(location(ctx))
106+
p.push(&ast.IntegerNode{Value: int(i)}).SetLocation(location(ctx))
107107
} else if node := ctx.HexIntegerLiteral(); node != nil {
108108
text := node.GetText()
109109
i, err := strconv.ParseInt(text, 0, 64)
110110
if err != nil {
111111
p.reportError(ctx, "parse error: invalid hex literal")
112112
return
113113
}
114-
p.push(&ast.IntegerNode{Value: i}).SetLocation(location(ctx))
114+
p.push(&ast.IntegerNode{Value: int(i)}).SetLocation(location(ctx))
115115
} else {
116116
p.reportError(ctx, "parse error: invalid octal literal")
117117
}

vm/runtime.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func fetch(from interface{}, i interface{}) interface{} {
1818
switch v.Kind() {
1919

2020
case reflect.Array, reflect.Slice, reflect.String:
21-
index := toInt64(i)
21+
index := toInt(i)
2222
value := v.Index(int(index))
2323
if value.IsValid() && value.CanInterface() {
2424
return value.Interface()
@@ -603,48 +603,48 @@ func exponent(a, b interface{}) float64 {
603603
return math.Pow(toFloat64(a), toFloat64(b))
604604
}
605605

606-
func makeRange(a, b interface{}) []int64 {
607-
min := toInt64(a)
608-
max := toInt64(b)
606+
func makeRange(a, b interface{}) []int {
607+
min := toInt(a)
608+
max := toInt(b)
609609
size := max - min + 1
610-
rng := make([]int64, size)
610+
rng := make([]int, size)
611611
for i := range rng {
612-
rng[i] = min + int64(i)
612+
rng[i] = min + i
613613
}
614614
return rng
615615
}
616616

617-
func toInt64(a interface{}) int64 {
617+
func toInt(a interface{}) int {
618618
switch x := a.(type) {
619619
case float32:
620-
return int64(x)
620+
return int(x)
621621
case float64:
622-
return int64(x)
622+
return int(x)
623623

624624
case int:
625-
return int64(x)
625+
return int(x)
626626
case int8:
627-
return int64(x)
627+
return int(x)
628628
case int16:
629-
return int64(x)
629+
return int(x)
630630
case int32:
631-
return int64(x)
631+
return int(x)
632632
case int64:
633-
return int64(x)
633+
return int(x)
634634

635635
case uint:
636-
return int64(x)
636+
return int(x)
637637
case uint8:
638-
return int64(x)
638+
return int(x)
639639
case uint16:
640-
return int64(x)
640+
return int(x)
641641
case uint32:
642-
return int64(x)
642+
return int(x)
643643
case uint64:
644-
return int64(x)
644+
return int(x)
645645

646646
default:
647-
panic(fmt.Sprintf("invalid operation: int64(%T)", x))
647+
panic(fmt.Sprintf("invalid operation: int(%T)", x))
648648
}
649649
}
650650

vm/vm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (vm *VM) Run() interface{} {
7676
switch op {
7777

7878
case OpPush:
79-
vm.push(int64(vm.arg()))
79+
vm.push(int(vm.arg()))
8080

8181
case OpPop:
8282
vm.pop()
@@ -266,15 +266,15 @@ func (vm *VM) Run() interface{} {
266266
vm.push(out[0].Interface())
267267

268268
case OpArray:
269-
size := vm.pop().(int64)
269+
size := vm.pop().(int)
270270
array := make([]interface{}, size)
271271
for i := size - 1; i >= 0; i-- {
272272
array[i] = vm.pop()
273273
}
274274
vm.push(array)
275275

276276
case OpMap:
277-
size := vm.pop().(int64)
277+
size := vm.pop().(int)
278278
m := make(map[string]interface{})
279279
for i := size - 1; i >= 0; i-- {
280280
value := vm.pop()
@@ -284,7 +284,7 @@ func (vm *VM) Run() interface{} {
284284
vm.push(m)
285285

286286
case OpLen:
287-
vm.push(int64(length(vm.current())))
287+
vm.push(length(vm.current()))
288288

289289
case OpBegin:
290290
sc := make(Scope)

0 commit comments

Comments
 (0)