From fe93d5c015e8e2c883d2c1e74f2e5ce071256cb5 Mon Sep 17 00:00:00 2001 From: Mel Date: Fri, 20 May 2022 00:01:11 +0200 Subject: Access methods for VM data and stop Array copying --- pkg/lang/vm/exec.go | 54 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'pkg/lang/vm/exec.go') diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go index c145acf..54f43db 100644 --- a/pkg/lang/vm/exec.go +++ b/pkg/lang/vm/exec.go @@ -72,13 +72,13 @@ func (vm *VM) execAdd() error { switch x.Type().Kind { case value.IntType: - xv := int64(x.Data().(value.IntData)) + xv := x.Data().(value.IntData).Get() switch y.Type().Kind { case value.IntType: - yv := int64(y.Data().(value.IntData)) + yv := y.Data().(value.IntData).Get() res = value.NewInt(xv + yv) case value.FloatType: - yv := float64(y.Data().(value.FloatData)) + yv := y.Data().(value.FloatData).Get() res = value.NewFloat(float64(xv) + yv) default: return ErrInvalidOperandTypes{ @@ -88,13 +88,13 @@ func (vm *VM) execAdd() error { } } case value.FloatType: - xv := float64(x.Data().(value.FloatData)) + xv := x.Data().(value.FloatData).Get() switch y.Type().Kind { case value.IntType: - yv := float64(y.Data().(value.IntData)) - res = value.NewFloat(xv + yv) + yv := y.Data().(value.IntData).Get() + res = value.NewFloat(xv + float64(yv)) case value.FloatType: - yv := float64(y.Data().(value.FloatData)) + yv := y.Data().(value.FloatData).Get() res = value.NewFloat(xv + yv) default: return ErrInvalidOperandTypes{ @@ -106,7 +106,7 @@ func (vm *VM) execAdd() error { case value.StringType: switch y.Type().Kind { case value.StringType: - res = value.NewString(string(x.Data().(value.StringData)) + string(y.Data().(value.StringData))) + res = value.NewString(x.Data().(value.StringData).Get() + y.Data().(value.StringData).Get()) default: return ErrInvalidOperandTypes{ Op: code.OpAdd, @@ -142,13 +142,13 @@ func (vm *VM) execSub() error { switch x.Type().Kind { case value.IntType: - xv := int64(x.Data().(value.IntData)) + xv := x.Data().(value.IntData).Get() switch y.Type().Kind { case value.IntType: - yv := int64(y.Data().(value.IntData)) + yv := y.Data().(value.IntData).Get() res = value.NewInt(xv - yv) case value.FloatType: - yv := float64(y.Data().(value.FloatData)) + yv := y.Data().(value.FloatData).Get() res = value.NewFloat(float64(xv) - yv) default: return ErrInvalidOperandTypes{ @@ -158,13 +158,13 @@ func (vm *VM) execSub() error { } } case value.FloatType: - xv := float64(x.Data().(value.FloatData)) + xv := x.Data().(value.FloatData).Get() switch y.Type().Kind { case value.IntType: - yv := float64(y.Data().(value.IntData)) - res = value.NewFloat(xv - yv) + yv := y.Data().(value.IntData).Get() + res = value.NewFloat(xv - float64(yv)) case value.FloatType: - yv := float64(y.Data().(value.FloatData)) + yv := y.Data().(value.FloatData).Get() res = value.NewFloat(xv - yv) default: return ErrInvalidOperandTypes{ @@ -199,16 +199,24 @@ func (vm *VM) execIndex() error { switch v.Type().Kind { case value.ArrayType: - arr := v.Data().([]value.Value) - idx := i.Data().(int64) - if idx < 0 || idx >= int64(len(arr)) { - return ErrArrayIndexOutOfBounds{ - Index: int(idx), - Len: len(arr), + arr := v.Data().(value.ArrayData) + switch i.Type().Kind { + case value.IntType: + idx := i.Data().(value.IntData).Get() + if idx < 0 || idx >= int64(arr.Len()) { + return ErrArrayIndexOutOfBounds{ + Index: int(idx), + Len: arr.Len(), + } + } + top.Push(arr.At(int(idx))) + default: + return ErrInvalidOperandTypes{ + Op: code.OpIndex, + X: i.Type(), + Y: v.Type(), } } - - top.Push(arr[idx]) default: return ErrInvalidOperandTypes{ Op: code.OpIndex, -- cgit 1.4.1