diff options
Diffstat (limited to 'pkg/lang/vm/exec.go')
| -rw-r--r-- | pkg/lang/vm/exec.go | 81 |
1 files changed, 25 insertions, 56 deletions
diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go index 1b94572..4f37ad3 100644 --- a/pkg/lang/vm/exec.go +++ b/pkg/lang/vm/exec.go @@ -6,64 +6,45 @@ import ( ) func (vm *VM) execPushInt(x int64) { - vm.stack.Top().Push(value.NewInt(x)) + vm.stack.Push(value.NewInt(x)) } func (vm *VM) execPushFloat(x float64) { - vm.stack.Top().Push(value.NewFloat(x)) + vm.stack.Push(value.NewFloat(x)) } func (vm *VM) execPushString(str string) { - vm.stack.Top().Push(value.NewString(str)) + vm.stack.Push(value.NewString(str)) } func (vm *VM) execPushBool(b bool) { - vm.stack.Top().Push(value.NewBool(b)) + vm.stack.Push(value.NewBool(b)) } func (vm *VM) execPushNull() { - vm.stack.Top().Push(value.NewNull()) + vm.stack.Push(value.NewNull()) } func (vm *VM) execPushArray() { - vm.stack.Top().Push(value.NewArray([]value.Value{})) + vm.stack.Push(value.NewArray([]value.Value{})) } func (vm *VM) execGetLocal(offset int) error { - top := vm.stack.Top() - - local, err := top.At(int(offset)) + local, err := vm.stack.Local(int(offset)) if err != nil { return err } - top.Push(local) - return nil -} - -func (vm *VM) execGetArg() error { - prev, err := vm.stack.Prev() - if err != nil { - return err - } - - arg, err := prev.Pop() - if err != nil { - return err - } - - vm.stack.Top().Push(arg) + vm.stack.Push(local) return nil } func (vm *VM) execAdd() error { - top := vm.stack.Top() - - x, err := top.Pop() + x, err := vm.stack.Pop() if err != nil { return err } - y, err := top.Pop() + y, err := vm.stack.Pop() if err != nil { return err } @@ -122,18 +103,16 @@ func (vm *VM) execAdd() error { } } - top.Push(res) + vm.stack.Push(res) return nil } func (vm *VM) execSub() error { - top := vm.stack.Top() - - x, err := top.Pop() + x, err := vm.stack.Pop() if err != nil { return err } - y, err := top.Pop() + y, err := vm.stack.Pop() if err != nil { return err } @@ -181,18 +160,16 @@ func (vm *VM) execSub() error { } } - top.Push(res) + vm.stack.Push(res) return nil } func (vm *VM) execIndex() error { - top := vm.stack.Top() - - v, err := top.Pop() + v, err := vm.stack.Pop() if err != nil { return err } - i, err := top.Pop() + i, err := vm.stack.Pop() if err != nil { return err } @@ -209,7 +186,7 @@ func (vm *VM) execIndex() error { Len: arr.Len(), } } - top.Push(arr.At(int(idx))) + vm.stack.Push(arr.At(int(idx))) default: return ErrInvalidOperandTypes{ Op: code.OpIndex, @@ -229,13 +206,11 @@ func (vm *VM) execIndex() error { } func (vm *VM) execLte() error { - top := vm.stack.Top() - - x, err := top.Pop() + x, err := vm.stack.Pop() if err != nil { return err } - y, err := top.Pop() + y, err := vm.stack.Pop() if err != nil { return err } @@ -283,14 +258,12 @@ func (vm *VM) execLte() error { } } - top.Push(res) + vm.stack.Push(res) return nil } func (vm *VM) execJumpIf(pc int, cond bool) error { - top := vm.stack.Top() - - b, err := top.Pop() + b, err := vm.stack.Pop() if err != nil { return err } @@ -319,9 +292,7 @@ func (vm *VM) execJumpIf(pc int, cond bool) error { } func (vm *VM) execTempArrLen() error { - top := vm.stack.Top() - - a, err := top.Pop() + a, err := vm.stack.Pop() if err != nil { return err } @@ -330,7 +301,7 @@ func (vm *VM) execTempArrLen() error { case value.ArrayType: arr := a.Data().(value.ArrayData) res := value.NewInt(int64(arr.Len())) - top.Push(res) + vm.stack.Push(res) default: return ErrInvalidOperandTypes{ Op: code.OpTempArrLen, @@ -342,13 +313,11 @@ func (vm *VM) execTempArrLen() error { } func (vm *VM) execTempArrPush() error { - top := vm.stack.Top() - - a, err := top.Pop() + a, err := vm.stack.Pop() if err != nil { return err } - e, err := top.Pop() + e, err := vm.stack.Pop() if err != nil { return err } |
