Exemplo n.º 1
0
        public static bool CompOp(OpCode opcode, LuaValue a, LuaValue b)
        {
            switch (opcode)
            {
            case OpCode.EQ:
                return(a == b);

            case OpCode.LT:
                if (a.ValueType == ValueType.Integer && b.ValueType == ValueType.Integer)
                {
                    return(a.ConvertToInt() < b.ConvertToInt());
                }
                else
                {
                    return(a.ConvertToFloat() < b.ConvertToFloat());
                }

            case OpCode.LE:
                if (a.ValueType == ValueType.Integer && b.ValueType == ValueType.Integer)
                {
                    return(a.ConvertToInt() <= b.ConvertToInt());
                }
                else
                {
                    return(a.ConvertToFloat() <= b.ConvertToFloat());
                }

            default:
                throw new Exception("invalid opcode " + opcode);
            }
        }
Exemplo n.º 2
0
        public static LuaValue UnaryOp(OpCode opcode, LuaValue a)
        {
            switch (opcode)
            {
            case OpCode.UNM:
                if (a.ValueType == ValueType.Integer)
                {
                    return(new LuaValue(-a.ConvertToInt()));
                }
                else
                {
                    return(new LuaValue(-a.ConvertToFloat()));
                }

            case OpCode.BNOT:
                return(new LuaValue(~a.ConvertToInt()));

            case OpCode.NOT:
                return(new LuaValue(!a.ConvertToBool()));

            default:
                throw new Exception("invalid opcode " + opcode);
            }
        }
Exemplo n.º 3
0
        //====================================================
        // Lua operators
        //====================================================
                #if false
        public static LuaValue BinOp(OpCode opcode, LuaValue a, LuaValue b)
        {
            bool intOperator;             // 0=数値演算, 1=Int演算, 2=Bool演算

            switch (opcode)
            {
            case OpCode.ADD:
            case OpCode.SUB:
            case OpCode.MUL:
            case OpCode.MOD:
            case OpCode.POW:
            case OpCode.DIV:
                intOperator = false;
                break;

            case OpCode.IDIV:
            case OpCode.BAND:
            case OpCode.BOR:
            case OpCode.BXOR:
            case OpCode.SHL:
            case OpCode.SHR:
                intOperator = true;
                break;

            default:
                throw new Exception("invalid opcode " + opcode);
            }

            if (!intOperator)
            {
                var    fa = a.ConvertToFloat();
                var    fb = b.ConvertToFloat();
                double r  = 0;
                switch (opcode)
                {
                case OpCode.ADD:
                    r = fa + fb;
                    break;

                case OpCode.SUB:
                    r = fa - fb;
                    break;

                case OpCode.MUL:
                    r = fa * fb;
                    break;

                case OpCode.MOD:
                    r = fa % fb;
                    break;

                case OpCode.POW:
                    r = Math.Pow(fa, fb);
                    break;

                case OpCode.DIV:
                    r = fa / fb;
                    break;
                }
                return(new LuaValue(r));
            }
            else
            {
                var ia = a.ConvertToInt();
                var ib = b.ConvertToInt();
                int r  = 0;
                switch (opcode)
                {
                case OpCode.IDIV:
                    r = ia % ib;
                    break;

                case OpCode.BAND:
                    r = ia & ib;
                    break;

                case OpCode.BOR:
                    r = ia | ib;
                    break;

                case OpCode.BXOR:
                    r = ia ^ ib;
                    break;

                case OpCode.SHL:
                    r = ia << ib;
                    break;

                case OpCode.SHR:
                    r = ia >> ib;
                    break;
                }
                return(new LuaValue(r));
            }
        }