Skip to content

Commit

Permalink
Improve location storage in program
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Mar 4, 2020
1 parent 05175c9 commit 821f089
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
15 changes: 7 additions & 8 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
}()

c := &compiler{
index: make(map[interface{}]uint16),
index: make(map[interface{}]uint16),
locations: make(map[int]file.Location),
}
if config != nil {
c.mapEnv = config.MapEnv
Expand All @@ -47,7 +48,7 @@ func Compile(tree *parser.Tree, config *conf.Config) (program *Program, err erro
}

type compiler struct {
locations []file.Location
locations map[int]file.Location
constants []interface{}
bytecode []byte
index map[interface{}]uint16
Expand All @@ -61,13 +62,11 @@ func (c *compiler) emit(op byte, b ...byte) int {
current := len(c.bytecode)
c.bytecode = append(c.bytecode, b...)

for i := 0; i < 1+len(b); i++ {
var loc file.Location
if len(c.nodes) > 0 {
loc = c.nodes[len(c.nodes)-1].Location()
}
c.locations = append(c.locations, loc)
var loc file.Location
if len(c.nodes) > 0 {
loc = c.nodes[len(c.nodes)-1].Location()
}
c.locations[current-1] = loc

return current
}
Expand Down
2 changes: 1 addition & 1 deletion vm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type Program struct {
Source *file.Source
Locations []file.Location
Locations map[int]file.Location
Constants []interface{}
Bytecode []byte
}
Expand Down
16 changes: 16 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,19 @@ func TestRun_memory_budget(t *testing.T) {
_, err = vm.Run(program, nil)
require.Error(t, err)
}

func TestRun_runtime_error(t *testing.T) {
input := `map(1..3, {1/(#-3)})`

tree, err := parser.Parse(input)
require.NoError(t, err)

program, err := compiler.Compile(tree, nil)
require.NoError(t, err)

_, err = vm.Run(program, nil)
require.Error(t, err)
require.Equal(t, `runtime error: integer divide by zero (1:13)
| map(1..3, {1/(#-3)})
| ............^`, err.Error())
}

0 comments on commit 821f089

Please sign in to comment.