Beispiel #1
0
        public void interpret()
        {
            Console.WriteLine("asd" + pc);
            Pcode tempCode = pcodeList[pc++];

            //output += pc.ToString() + "\t" + Pcode.PCODE[(int)tempCode.f].ToString() + "\t" + tempCode.l.ToString() + "\t" + tempCode.a.ToString() + "\n";
            switch (tempCode.f)
            {
            case Pcode.PC.LIT:
                runtimeStack[sp++] = tempCode.a;
                break;

            case Pcode.PC.OPR:
                //Console.WriteLine("vvvvvvv");
                switch (tempCode.a)
                {
                case 0:
                    sp = bp;
                    pc = runtimeStack[sp + 2];
                    bp = runtimeStack[sp + 1];
                    break;

                case 1:
                    runtimeStack[sp - 1] = -runtimeStack[sp - 1];
                    break;

                case 2:
                    sp--;
                    runtimeStack[sp - 1] += runtimeStack[sp];
                    break;

                case 3:
                    sp--;
                    runtimeStack[sp - 1] -= runtimeStack[sp];
                    break;

                case 4:
                    sp--;
                    runtimeStack[sp - 1] *= runtimeStack[sp];
                    break;

                case 5:
                    sp--;
                    runtimeStack[sp - 1] /= runtimeStack[sp];
                    break;

                case 6:
                    sp--;
                    runtimeStack[sp - 1] %= 2;
                    break;

                case 7:
                    sp--;
                    runtimeStack[sp - 1] %= runtimeStack[sp];
                    break;

                case 8:
                    sp--;
                    runtimeStack[sp - 1] = (runtimeStack[sp] == runtimeStack[sp - 1] ? 1 : 0);
                    break;

                case 9:
                    sp--;
                    runtimeStack[sp - 1] = (runtimeStack[sp] != runtimeStack[sp - 1] ? 1 : 0);
                    break;

                case 10:
                    sp--;
                    runtimeStack[sp - 1] = (runtimeStack[sp - 1] < runtimeStack[sp] ? 1 : 0);
                    break;

                case 11:
                    sp--;
                    runtimeStack[sp - 1] = (runtimeStack[sp - 1] >= runtimeStack[sp] ? 1 : 0);
                    break;

                case 12:
                    sp--;
                    runtimeStack[sp - 1] = (runtimeStack[sp - 1] > runtimeStack[sp] ? 1 : 0);
                    break;

                case 13:
                    sp--;
                    runtimeStack[sp - 1] = (runtimeStack[sp - 1] <= runtimeStack[sp] ? 1 : 0);
                    break;

                case 14:
                    outputFlag = 1;
                    output    += runtimeStack[sp - 1];
                    break;

                case 15:
                    outputFlag = 1;
                    output    += "\n";
                    break;

                case 16:
                    inputFlag = 1;
                    break;
                }
                break;

            case Pcode.PC.LOD:
                runtimeStack[sp] = runtimeStack[getBasePtr(tempCode.l, runtimeStack, bp) + tempCode.a];
                sp++;
                break;

            case Pcode.PC.STO:
                sp--;
                runtimeStack[getBasePtr(tempCode.l, runtimeStack, bp) + tempCode.a] = runtimeStack[sp];
                break;

            case Pcode.PC.CAL:
                runtimeStack[sp]     = getBasePtr(tempCode.l, runtimeStack, bp);
                runtimeStack[sp + 1] = bp;
                runtimeStack[sp + 2] = pc;
                bp = sp;
                pc = tempCode.a;
                break;

            case Pcode.PC.INT:
                sp += tempCode.a;
                break;

            case Pcode.PC.JMP:
                pc = tempCode.a;
                break;

            case Pcode.PC.JPC:
                sp--;
                if (runtimeStack[sp] == 0)
                {
                    pc = tempCode.a;
                }
                break;
            }
        }
Beispiel #2
0
 //生成P-Code代码
 public void produceCodes(Pcode.PC f, int l, int a)
 {
     pcodeList[arrayPtr++] = new Pcode(f, l, a);
 }