@@ -21,7 +21,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
21
21
}()
22
22
23
23
c := & compiler {
24
- index : make (map [interface {}]Opcode ),
24
+ index : make (map [interface {}]int ),
25
25
locations : make ([]file.Location , 0 ),
26
26
}
27
27
@@ -44,6 +44,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
44
44
Locations : c .locations ,
45
45
Constants : c .constants ,
46
46
Bytecode : c .bytecode ,
47
+ Arguments : c .arguments ,
47
48
}
48
49
return
49
50
}
@@ -54,23 +55,24 @@ type compiler struct {
54
55
locations []file.Location
55
56
constants []interface {}
56
57
bytecode []Opcode
57
- index map [interface {}]Opcode
58
+ index map [interface {}]int
58
59
mapEnv bool
59
60
cast reflect.Kind
60
61
nodes []ast.Node
61
62
chains [][]int
63
+ arguments []int
62
64
}
63
65
64
- func (c * compiler ) emitLocation (loc file.Location , op Opcode , arg Opcode ) int {
66
+ func (c * compiler ) emitLocation (loc file.Location , op Opcode , arg int ) int {
65
67
c .bytecode = append (c .bytecode , op )
66
68
current := len (c .bytecode )
67
- c .bytecode = append (c .bytecode , arg )
69
+ c .arguments = append (c .arguments , arg )
68
70
c .locations = append (c .locations , loc )
69
71
return current
70
72
}
71
73
72
- func (c * compiler ) emit (op Opcode , args ... Opcode ) int {
73
- var arg Opcode = 0
74
+ func (c * compiler ) emit (op Opcode , args ... int ) int {
75
+ var arg int = 0
74
76
if len (args ) > 1 {
75
77
panic ("too many arguments" )
76
78
}
@@ -88,7 +90,7 @@ func (c *compiler) emitPush(value interface{}) int {
88
90
return c .emit (OpPush , c .addConstant (value ))
89
91
}
90
92
91
- func (c * compiler ) addConstant (constant interface {}) Opcode {
93
+ func (c * compiler ) addConstant (constant interface {}) int {
92
94
indexable := true
93
95
hash := constant
94
96
switch reflect .TypeOf (constant ).Kind () {
@@ -111,7 +113,7 @@ func (c *compiler) addConstant(constant interface{}) Opcode {
111
113
panic ("exceeded constants max space limit" )
112
114
}
113
115
114
- p := Opcode ( len (c .constants ) - 1 )
116
+ p := len (c .constants ) - 1
115
117
if indexable {
116
118
c .index [hash ] = p
117
119
}
@@ -120,11 +122,11 @@ func (c *compiler) addConstant(constant interface{}) Opcode {
120
122
121
123
func (c * compiler ) patchJump (placeholder int ) {
122
124
offset := len (c .bytecode ) - 1 - placeholder
123
- c .bytecode [placeholder ] = Opcode ( offset )
125
+ c .arguments [placeholder - 1 ] = offset
124
126
}
125
127
126
- func (c * compiler ) calcBackwardJump (to int ) Opcode {
127
- return Opcode ( len (c .bytecode ) + 2 - to )
128
+ func (c * compiler ) calcBackwardJump (to int ) int {
129
+ return len (c .bytecode ) - 1 - to
128
130
}
129
131
130
132
func (c * compiler ) compile (node ast.Node ) {
@@ -502,7 +504,7 @@ func (c *compiler) CallNode(node *ast.CallNode) {
502
504
op = OpCallFast
503
505
}
504
506
c .compile (node .Callee )
505
- c .emit (op , Opcode ( len (node .Arguments ) ))
507
+ c .emit (op , len (node .Arguments ))
506
508
}
507
509
508
510
func (c * compiler ) BuiltinNode (node * ast.BuiltinNode ) {
@@ -632,7 +634,7 @@ func (c *compiler) emitCond(body func()) {
632
634
c .patchJump (jmp )
633
635
}
634
636
635
- func (c * compiler ) emitLoop (body func ()) Opcode {
637
+ func (c * compiler ) emitLoop (body func ()) int {
636
638
i := c .addConstant ("i" )
637
639
size := c .addConstant ("size" )
638
640
array := c .addConstant ("array" )
0 commit comments