Skip to content

Commit 5ac99ca

Browse files
committed
Cleanup
1 parent 3eb3fca commit 5ac99ca

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

checker/checker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ func TestCheck_TypeWeights(t *testing.T) {
680680
"Uint16": uint16(3),
681681
"Uint32": uint32(4),
682682
"Uint64": uint64(5),
683-
"Int": int(6),
683+
"Int": 6,
684684
"Int8": int8(7),
685685
"Int16": int16(8),
686686
"Int32": int32(9),

compiler/compiler.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
"github.com/antonmedv/expr/vm/runtime"
1414
)
1515

16+
const (
17+
placeholder = 12345
18+
)
19+
1620
func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err error) {
1721
defer func() {
1822
if r := recover(); r != nil {
@@ -49,8 +53,6 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
4953
return
5054
}
5155

52-
const placeholder = MaxOpcode
53-
5456
type compiler struct {
5557
locations []file.Location
5658
constants []interface{}
@@ -72,7 +74,7 @@ func (c *compiler) emitLocation(loc file.Location, op Opcode, arg int) int {
7274
}
7375

7476
func (c *compiler) emit(op Opcode, args ...int) int {
75-
var arg int = 0
77+
arg := 0
7678
if len(args) > 1 {
7779
panic("too many arguments")
7880
}
@@ -215,7 +217,7 @@ func (c *compiler) IntegerNode(node *ast.IntegerNode) {
215217
case reflect.Float64:
216218
c.emitPush(float64(node.Value))
217219
case reflect.Int:
218-
c.emitPush(int(node.Value))
220+
c.emitPush(node.Value)
219221
case reflect.Int8:
220222
c.emitPush(int8(node.Value))
221223
case reflect.Int16:

expr_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,11 @@ func TestExpr(t *testing.T) {
546546
}{
547547
{
548548
`1`,
549-
int(1),
549+
1,
550550
},
551551
{
552552
`-.5`,
553-
float64(-.5),
553+
-.5,
554554
},
555555
{
556556
`true && false || false`,
@@ -578,15 +578,15 @@ func TestExpr(t *testing.T) {
578578
},
579579
{
580580
`Uint64 + 0`,
581-
int(0),
581+
0,
582582
},
583583
{
584584
`Uint64 + Int64`,
585-
int(0),
585+
0,
586586
},
587587
{
588588
`Int32 + Int64`,
589-
int(0),
589+
0,
590590
},
591591
{
592592
`Float64 + 0`,
@@ -1442,7 +1442,7 @@ func (e *mockEnv) GetInt() int {
14421442
}
14431443

14441444
func (*mockEnv) Add(a, b int) int {
1445-
return int(a + b)
1445+
return a + b
14461446
}
14471447

14481448
func (*mockEnv) Duration(s string) time.Duration {

vm/opcodes.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package vm
22

3-
import "math"
4-
5-
type Opcode uint32
6-
7-
const MaxOpcode = math.MaxUint32
3+
type Opcode byte
84

95
const (
106
OpPush Opcode = iota
7+
OpPushInt
118
OpPop
129
OpRot
1310
OpFetch

vm/program.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ func (program *Program) Disassemble() string {
2929
out += fmt.Sprintf("%v\t%v\n", pp, label)
3030
}
3131
jump := func(label string) {
32-
out += fmt.Sprintf("%v\t%v\t%v\t(%v)\n", pp, label, arg, ip+int(arg))
32+
out += fmt.Sprintf("%v\t%v\t%v\t(%v)\n", pp, label, arg, ip+arg)
3333
}
34-
back := func(label string) {
35-
out += fmt.Sprintf("%v\t%v\t%v\t(%v)\n", pp, label, arg, ip-int(arg))
34+
jumpBack := func(label string) {
35+
out += fmt.Sprintf("%v\t%v\t%v\t(%v)\n", pp, label, arg, ip-arg)
3636
}
3737
argument := func(label string) {
3838
out += fmt.Sprintf("%v\t%v\t%v\n", pp, label, arg)
3939
}
4040
constant := func(label string) {
4141
var c interface{}
42-
if int(arg) < len(program.Constants) {
42+
if arg < len(program.Constants) {
4343
c = program.Constants[arg]
4444
} else {
4545
c = "out of range"
@@ -57,6 +57,9 @@ func (program *Program) Disassemble() string {
5757
case OpPush:
5858
constant("OpPush")
5959

60+
case OpPushInt:
61+
argument("OpPushInt")
62+
6063
case OpPop:
6164
code("OpPop")
6265

@@ -115,7 +118,7 @@ func (program *Program) Disassemble() string {
115118
jump("OpJumpIfNil")
116119

117120
case OpJumpBackward:
118-
back("OpJumpBackward")
121+
jumpBack("OpJumpBackward")
119122

120123
case OpIn:
121124
code("OpIn")

vm/vm.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,25 @@ func (vm *VM) Run(program *Program, env interface{}) (out interface{}, err error
144144
vm.push(a.(string) == b.(string))
145145

146146
case OpJump:
147-
offset := arg
148-
vm.ip += int(offset)
147+
vm.ip += arg
149148

150149
case OpJumpIfTrue:
151-
offset := arg
152150
if vm.current().(bool) {
153-
vm.ip += int(offset)
151+
vm.ip += arg
154152
}
155153

156154
case OpJumpIfFalse:
157155
if !vm.current().(bool) {
158-
vm.ip += int(arg)
156+
vm.ip += arg
159157
}
160158

161159
case OpJumpIfNil:
162160
if runtime.IsNil(vm.current()) {
163-
vm.ip += int(arg)
161+
vm.ip += arg
164162
}
165163

166164
case OpJumpBackward:
167-
vm.ip -= int(arg)
165+
vm.ip -= arg
168166

169167
case OpIn:
170168
b := vm.pop()

vm/vm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestRun_Helpers(t *testing.T) {
8484
uint16(1),
8585
uint32(1),
8686
uint64(1),
87-
int(1),
87+
1,
8888
int8(1),
8989
int16(1),
9090
int32(1),

0 commit comments

Comments
 (0)