package vm import ( "jinx/pkg/lang/vm/code" "jinx/pkg/lang/vm/value" ) func (vm *VM) execPushInt(x int64) { vm.stack.Push(value.NewInt(x)) } func (vm *VM) execPushFloat(x float64) { vm.stack.Push(value.NewFloat(x)) } func (vm *VM) execPushString(str string) { vm.stack.Push(value.NewString(str)) } func (vm *VM) execPushBool(b bool) { vm.stack.Push(value.NewBool(b)) } func (vm *VM) execPushNull() { vm.stack.Push(value.NewNull()) } func (vm *VM) execPushArray() { vm.stack.Push(value.NewArray([]value.Value{})) } func (vm *VM) execGetLocal(offset int) error { local, err := vm.stack.Local(int(offset)) if err != nil { return err } vm.stack.Push(local) return nil } func (vm *VM) execAdd() error { x, err := vm.stack.Pop() if err != nil { return err } y, err := vm.stack.Pop() if err != nil { return err } var res value.Value switch x.Type().Kind { case value.IntType: xv := x.Data().(value.IntData).Get() switch y.Type().Kind { case value.IntType: yv := y.Data().(value.IntData).Get() res = value.NewInt(xv + yv) case value.FloatType: yv := y.Data().(value.FloatData).Get() res = value.NewFloat(float64(xv) + yv) default: return ErrInvalidOperandTypes{ Op: code.OpAdd, X: x.Type(), Y: y.Type(), } } case value.FloatType: xv := x.Data().(value.FloatData).Get() switch y.Type().Kind { case value.IntType: yv := y.Data().(value.IntData).Get() res = value.NewFloat(xv + float64(yv)) case value.FloatType: yv := y.Data().(value.FloatData).Get() 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(x.Data().(value.StringData).Get() + y.Data().(value.StringData).Get()) default: return ErrInvalidOperandTypes{ Op: code.OpAdd, X: x.Type(), Y: y.Type(), } } default: return ErrInvalidOperandTypes{ Op: code.OpAdd, X: x.Type(), Y: y.Type(), } } vm.stack.Push(res) return nil } func (vm *VM) execSub() error { x, err := vm.stack.Pop() if err != nil { return err } y, err := vm.stack.Pop() if err != nil { return err } var res value.Value switch x.Type().Kind { case value.IntType: xv := x.Data().(value.IntData).Get() switch y.Type().Kind { case value.IntType: yv := y.Data().(value.IntData).Get() res = value.NewInt(xv - yv) case value.FloatType: yv := y.Data().(value.FloatData).Get() res = value.NewFloat(float64(xv) - yv) default: return ErrInvalidOperandTypes{ Op: code.OpSub, X: x.Type(), Y: y.Type(), } } case value.FloatType: xv := x.Data().(value.FloatData).Get() switch y.Type().Kind { case value.IntType: yv := y.Data().(value.IntData).Get() res = value.NewFloat(xv - float64(yv)) case value.FloatType: yv := y.Data().(value.FloatData).Get() 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(), } } vm.stack.Push(res) return nil } func (vm *VM) execIndex() error { v, err := vm.stack.Pop() if err != nil { return err } i, err := vm.stack.Pop() if err != nil { return err } switch v.Type().Kind { case value.ArrayType: 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(), } } vm.stack.Push(arr.At(int(idx))) default: return ErrInvalidOperandTypes{ Op: code.OpIndex, X: i.Type(), Y: v.Type(), } } default: return ErrInvalidOperandTypes{ Op: code.OpIndex, X: v.Type(), Y: i.Type(), } } return nil } func (vm *VM) execLte() error { x, err := vm.stack.Pop() if err != nil { return err } y, err := vm.stack.Pop() if err != nil { return err } var res value.Value switch x.Type().Kind { case value.IntType: xv := x.Data().(value.IntData).Get() switch y.Type().Kind { case value.IntType: yv := y.Data().(value.IntData).Get() res = value.NewBool(xv <= yv) case value.FloatType: yv := y.Data().(value.FloatData).Get() res = value.NewBool(float64(xv) <= yv) default: return ErrInvalidOperandTypes{ Op: code.OpLte, X: x.Type(), Y: y.Type(), } } case value.FloatType: xv := x.Data().(value.FloatData).Get() switch y.Type().Kind { case value.IntType: yv := y.Data().(value.IntData).Get() res = value.NewBool(xv <= float64(yv)) case value.FloatType: yv := y.Data().(value.FloatData).Get() res = value.NewBool(xv <= yv) default: return ErrInvalidOperandTypes{ Op: code.OpLte, X: x.Type(), Y: y.Type(), } } default: return ErrInvalidOperandTypes{ Op: code.OpLte, X: x.Type(), Y: y.Type(), } } vm.stack.Push(res) return nil } func (vm *VM) execJumpIf(pc int, cond bool) error { b, err := vm.stack.Pop() if err != nil { return err } switch b.Type().Kind { case value.BoolType: bl := b.Data().(value.BoolData) if bl.Get() == cond { vm.pc = pc } default: var op code.Op if cond { op = code.OpJt } else { op = code.OpJf } return ErrInvalidOperandType{ Op: op, X: b.Type(), } } return nil } func (vm *VM) execTempArrLen() error { a, err := vm.stack.Pop() if err != nil { return err } switch a.Type().Kind { case value.ArrayType: arr := a.Data().(value.ArrayData) res := value.NewInt(int64(arr.Len())) vm.stack.Push(res) default: return ErrInvalidOperandTypes{ Op: code.OpTempArrLen, X: a.Type(), } } return nil } func (vm *VM) execTempArrPush() error { a, err := vm.stack.Pop() if err != nil { return err } e, err := vm.stack.Pop() if err != nil { return err } switch a.Type().Kind { case value.ArrayType: arr := a.Data().(value.ArrayData) arr.Push(e) default: return ErrInvalidOperandType{ Op: code.OpTempArrPush, X: a.Type(), } } return nil }