package vm import ( "jinx/pkg/lang/vm/code" "jinx/pkg/lang/vm/value" ) func (vm *VM) execPushInt(x int64) { vm.stack.Top().Push(value.NewInt(x)) } func (vm *VM) execPushFloat(x float64) { vm.stack.Top().Push(value.NewFloat(x)) } func (vm *VM) execPushString(str string) { vm.stack.Top().Push(value.NewString(str)) } func (vm *VM) execPushBool(b bool) { vm.stack.Top().Push(value.NewBool(b)) } func (vm *VM) execPushNull() { vm.stack.Top().Push(value.NewNull()) } func (vm *VM) execPushArray() { vm.stack.Top().Push(value.NewArray([]value.Value{})) } func (vm *VM) execGetLocal(offset int) error { top := vm.stack.Top() local, err := top.At(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) return nil } func (vm *VM) execAdd() error { top := vm.stack.Top() x, err := top.Pop() if err != nil { return err } y, err := top.Pop() if err != nil { return err } var res value.Value switch x.Type().Kind { case value.IntType: xv := int64(x.Data().(value.IntData)) switch y.Type().Kind { case value.IntType: yv := int64(y.Data().(value.IntData)) res = value.NewInt(xv + yv) case value.FloatType: yv := float64(y.Data().(value.FloatData)) res = value.NewFloat(float64(xv) + yv) default: return ErrInvalidOperandTypes{ Op: code.OpAdd, X: x.Type(), Y: y.Type(), } } case value.FloatType: xv := float64(x.Data().(value.FloatData)) switch y.Type().Kind { case value.IntType: yv := float64(y.Data().(value.IntData)) res = value.NewFloat(xv + yv) case value.FloatType: yv := float64(y.Data().(value.FloatData)) res = value.NewFloat(xv + yv) default: return ErrInvalidOperandTypes{ Op: code.OpAdd, X: x.Type(), Y: y.Type(), } } case value.StringType: switch y.Type().Kind { case value.StringType: res = value.NewString(string(x.Data().(value.StringData)) + string(y.Data().(value.StringData))) default: return ErrInvalidOperandTypes{ Op: code.OpAdd, X: x.Type(), Y: y.Type(), } } default: return ErrInvalidOperandTypes{ Op: code.OpAdd, X: x.Type(), Y: y.Type(), } } top.Push(res) return nil } func (vm *VM) execSub() error { top := vm.stack.Top() x, err := top.Pop() if err != nil { return err } y, err := top.Pop() if err != nil { return err } var res value.Value switch x.Type().Kind { case value.IntType: xv := int64(x.Data().(value.IntData)) switch y.Type().Kind { case value.IntType: yv := int64(y.Data().(value.IntData)) res = value.NewInt(xv - yv) case value.FloatType: yv := float64(y.Data().(value.FloatData)) res = value.NewFloat(float64(xv) - yv) default: return ErrInvalidOperandTypes{ Op: code.OpSub, X: x.Type(), Y: y.Type(), } } case value.FloatType: xv := float64(x.Data().(value.FloatData)) switch y.Type().Kind { case value.IntType: yv := float64(y.Data().(value.IntData)) res = value.NewFloat(xv - yv) case value.FloatType: yv := float64(y.Data().(value.FloatData)) res = value.NewFloat(xv - yv) default: return ErrInvalidOperandTypes{ Op: code.OpSub, X: x.Type(), Y: y.Type(), } } default: return ErrInvalidOperandTypes{ Op: code.OpSub, X: x.Type(), Y: y.Type(), } } top.Push(res) return nil } func (vm *VM) execIndex() error { top := vm.stack.Top() v, err := top.Pop() if err != nil { return err } i, err := top.Pop() if err != nil { return err } 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), } } top.Push(arr[idx]) default: return ErrInvalidOperandTypes{ Op: code.OpIndex, X: v.Type(), Y: i.Type(), } } return nil }