Skip to content

Commit

Permalink
Add OpInvalid
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jul 19, 2023
1 parent 38b27f3 commit d2100ec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package vm
type Opcode byte

const (
OpPush Opcode = iota
OpInvalid Opcode = iota
OpPush
OpPushInt
OpPop
OpLoadConst
Expand Down
3 changes: 3 additions & 0 deletions vm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (program *Program) Disassemble() string {
}

switch op {
case OpInvalid:
code("OpInvalid")

case OpPush:
constant("OpPush")

Expand Down
3 changes: 3 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func (vm *VM) Run(program *Program, env interface{}) (_ interface{}, err error)

switch op {

case OpInvalid:
panic("invalid opcode")

case OpPush:
vm.push(program.Constants[arg])

Expand Down
12 changes: 12 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/antonmedv/expr/checker"
"github.com/antonmedv/expr/compiler"
"github.com/antonmedv/expr/conf"
"github.com/antonmedv/expr/file"
"github.com/antonmedv/expr/parser"
"github.com/antonmedv/expr/vm"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -380,3 +381,14 @@ func TestRun_TaggedFieldName(t *testing.T) {

require.Equal(t, "hello world", out)
}

func TestRun_OpInvalid(t *testing.T) {
program := &vm.Program{
Locations: []file.Location{{0, 0}},
Bytecode: []vm.Opcode{vm.OpInvalid},
Arguments: []int{0},
}

_, err := vm.Run(program, nil)
require.EqualError(t, err, "invalid opcode")
}

0 comments on commit d2100ec

Please sign in to comment.