Exemplo n.º 1
0
        public void CompileAdjust(Bytecodes bytecode, int adjust, params byte[] args)
        {
            this.bytes.Add((byte)(bytecode + adjust));

            foreach (var b in args)
            {
                this.bytes.Add(b);
            }
        }
Exemplo n.º 2
0
        public void Compile(Bytecodes bytecode, params byte[] args)
        {
            this.bytes.Add((byte)bytecode);

            foreach (var b in args)
            {
                this.bytes.Add(b);
            }
        }
        private static void CompileBytecode(string text, Bytecodes bc)
        {
            var compiler = new SimpleCompiler(text);
            var result   = compiler.Compile();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Length);
            Assert.AreEqual((byte)bc, result[0]);
        }
        private static void CompileBytecode(string text, Bytecodes bc, params byte[] bytes)
        {
            var compiler = new SimpleCompiler(text);
            var result   = compiler.Compile();

            Assert.IsNotNull(result);
            Assert.AreEqual(1 + bytes.Length, result.Length);
            Assert.AreEqual((byte)bc, result[0]);

            for (int k = 0; k < bytes.Length; k++)
            {
                Assert.AreEqual(bytes[k], result[k + 1]);
            }
        }
Exemplo n.º 5
0
        // Execute a single operation, and return it's cost
        private int opcode(Int16 code)
        {
            if ((code >= (int)Bytecodes.NUM_MIN_CODE && code <= (int)Bytecodes.NUM_MAX_CODE) ||
                code >= (int)Bytecodes.REG_MIN_CODE)
            {
                stack_.Push(code);
                return(1);
            }
            else if (code >= (int)Bytecodes.OP_MIN_CODE && code <= (int)Bytecodes.OP_MAX_CODE)
            {
                Bytecodes op = (Bytecodes)code;
                switch (op)
                {
                case Bytecodes.OP_PLUS: return(doPlus());

                case Bytecodes.OP_MINUS: return(doMinus());

                case Bytecodes.OP_TIMES: return(doTimes());

                case Bytecodes.OP_DIVIDE: return(doDivide());

                case Bytecodes.OP_GREATER: return(doGreater());

                case Bytecodes.OP_LESS: return(doLess());

                case Bytecodes.OP_EQUAL: return(doEqual());

                case Bytecodes.OP_NOTEQUAL: return(doNotEqual());

                case Bytecodes.OP_STORE: return(doStore());

                case Bytecodes.OP_DROP: return(doDrop());

                case Bytecodes.OP_SWAP: return(doSwap());

                case Bytecodes.OP_ROLL: return(doRoll());

                case Bytecodes.OP_JUMP: return(doJump());

                case Bytecodes.OP_CALL: return(doCall());

                case Bytecodes.OP_DUP: return(doDuplicate());

                case Bytecodes.OP_IF: return(doIf());

                case Bytecodes.OP_IFE: return(doIfe());

                case Bytecodes.OP_RECALL: return(doRecall());

                case Bytecodes.OP_END: throw new VMachineException(this, "End of code reached.");

                case Bytecodes.OP_NOP: return(1);

                case Bytecodes.OP_AND: return(doAnd());

                case Bytecodes.OP_OR: return(doOr());

                case Bytecodes.OP_EOR: return(doEor());

                case Bytecodes.OP_MOD: return(doMod());

                case Bytecodes.OP_BEEP: return(doBeep());

                case Bytecodes.OP_CHS: return(doChs());

                case Bytecodes.OP_NOT: return(doNot());

                case Bytecodes.OP_ARCTAN: return(doArcTan());

                case Bytecodes.OP_ABS: return(doAbs());

                case Bytecodes.OP_SIN: return(doSin());

                case Bytecodes.OP_COS: return(doCos());

                case Bytecodes.OP_TAN: return(doTan());

                case Bytecodes.OP_SQRT: return(doSqrt());

                case Bytecodes.OP_SYNC: return(int.MaxValue);

                case Bytecodes.OP_VSTORE: return(doVStore());

                case Bytecodes.OP_VRECALL: return(doVRecall());

                case Bytecodes.OP_DIST: return(doDist());

                case Bytecodes.OP_IFG: return(doIfg());

                case Bytecodes.OP_IFEG: return(doIfeg());

                case Bytecodes.OP_DEBUG: return(doDebug());

                case Bytecodes.OP_INTON: return(doIntOn());

                case Bytecodes.OP_INTOFF: return(doIntOff());

                case Bytecodes.OP_RTI: return(doRti());

                case Bytecodes.OP_SETINT: return(doSetInt());

                case Bytecodes.OP_SETPARAM: return(doSetParam());

                case Bytecodes.OP_MRB: return(doMrb());

                case Bytecodes.OP_DROPALL: return(doDropAll());

                case Bytecodes.OP_FLUSHINT: return(doFlushInt());

                case Bytecodes.OP_MIN: return(doMin());

                case Bytecodes.OP_MAX: return(doMax());

                case Bytecodes.OP_ARCCOS: return(doArcCos());

                case Bytecodes.OP_ARCSIN: return(doArcSin());

                default:
                    if (code >= (int)Bytecodes.OP_ICON_MIN &&
                        code <= (int)Bytecodes.OP_ICON_MAX)
                    {
                        return(doIcon((Int16)(code - (int)Bytecodes.OP_ICON_MIN)));
                    }
                    else if (code >= (int)Bytecodes.OP_SND_MIN &&
                             code <= (int)Bytecodes.OP_SND_MAX)
                    {
                        return(doSnd((Int16)(code - (int)Bytecodes.OP_SND_MIN)));
                    }
                    break;
                }
            }
            throw new VMachineException(this, "Unidentified instruction " + code.ToString() + ".");
        }