Skip to content

Commit b5c2a7c

Browse files
committed
Split arguments
1 parent 6de2e69 commit b5c2a7c

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

compiler/compiler.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
2121
}()
2222

2323
c := &compiler{
24-
index: make(map[interface{}]Opcode),
24+
index: make(map[interface{}]int),
2525
locations: make([]file.Location, 0),
2626
}
2727

@@ -44,6 +44,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
4444
Locations: c.locations,
4545
Constants: c.constants,
4646
Bytecode: c.bytecode,
47+
Arguments: c.arguments,
4748
}
4849
return
4950
}
@@ -54,23 +55,24 @@ type compiler struct {
5455
locations []file.Location
5556
constants []interface{}
5657
bytecode []Opcode
57-
index map[interface{}]Opcode
58+
index map[interface{}]int
5859
mapEnv bool
5960
cast reflect.Kind
6061
nodes []ast.Node
6162
chains [][]int
63+
arguments []int
6264
}
6365

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 {
6567
c.bytecode = append(c.bytecode, op)
6668
current := len(c.bytecode)
67-
c.bytecode = append(c.bytecode, arg)
69+
c.arguments = append(c.arguments, arg)
6870
c.locations = append(c.locations, loc)
6971
return current
7072
}
7173

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
7476
if len(args) > 1 {
7577
panic("too many arguments")
7678
}
@@ -88,7 +90,7 @@ func (c *compiler) emitPush(value interface{}) int {
8890
return c.emit(OpPush, c.addConstant(value))
8991
}
9092

91-
func (c *compiler) addConstant(constant interface{}) Opcode {
93+
func (c *compiler) addConstant(constant interface{}) int {
9294
indexable := true
9395
hash := constant
9496
switch reflect.TypeOf(constant).Kind() {
@@ -111,7 +113,7 @@ func (c *compiler) addConstant(constant interface{}) Opcode {
111113
panic("exceeded constants max space limit")
112114
}
113115

114-
p := Opcode(len(c.constants) - 1)
116+
p := len(c.constants) - 1
115117
if indexable {
116118
c.index[hash] = p
117119
}
@@ -120,11 +122,11 @@ func (c *compiler) addConstant(constant interface{}) Opcode {
120122

121123
func (c *compiler) patchJump(placeholder int) {
122124
offset := len(c.bytecode) - 1 - placeholder
123-
c.bytecode[placeholder] = Opcode(offset)
125+
c.arguments[placeholder-1] = offset
124126
}
125127

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
128130
}
129131

130132
func (c *compiler) compile(node ast.Node) {
@@ -502,7 +504,7 @@ func (c *compiler) CallNode(node *ast.CallNode) {
502504
op = OpCallFast
503505
}
504506
c.compile(node.Callee)
505-
c.emit(op, Opcode(len(node.Arguments)))
507+
c.emit(op, len(node.Arguments))
506508
}
507509

508510
func (c *compiler) BuiltinNode(node *ast.BuiltinNode) {
@@ -632,7 +634,7 @@ func (c *compiler) emitCond(body func()) {
632634
c.patchJump(jmp)
633635
}
634636

635-
func (c *compiler) emitLoop(body func()) Opcode {
637+
func (c *compiler) emitLoop(body func()) int {
636638
i := c.addConstant("i")
637639
size := c.addConstant("size")
638640
array := c.addConstant("array")

vm/program.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ type Program struct {
1313
Locations []file.Location
1414
Constants []interface{}
1515
Bytecode []Opcode
16+
Arguments []int
1617
}
1718

1819
func (program *Program) Disassemble() string {
1920
out := ""
2021
ip := 0
2122
for ip < len(program.Bytecode) {
2223
pp := ip
23-
op, arg := program.Bytecode[ip], program.Bytecode[ip+1]
24-
ip += 2
24+
op := program.Bytecode[ip]
25+
arg := program.Arguments[ip]
26+
ip += 1
2527

2628
code := func(label string) {
2729
out += fmt.Sprintf("%v\t%v\n", pp, label)

vm/vm.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (vm *VM) Run(program *Program, env interface{}) (out interface{}, err error
4949
defer func() {
5050
if r := recover(); r != nil {
5151
f := &file.Error{
52-
Location: program.Locations[(vm.ip-2)/2],
52+
Location: program.Locations[vm.ip-1],
5353
Message: fmt.Sprintf("%v", r),
5454
}
5555
err = f.Bind(program.Source)
@@ -75,8 +75,9 @@ func (vm *VM) Run(program *Program, env interface{}) (out interface{}, err error
7575
<-vm.step
7676
}
7777

78-
op, arg := program.Bytecode[vm.ip], program.Bytecode[vm.ip+1]
79-
vm.ip += 2
78+
op := program.Bytecode[vm.ip]
79+
arg := program.Arguments[vm.ip]
80+
vm.ip += 1
8081

8182
switch op {
8283

0 commit comments

Comments
 (0)