Exemplo n.º 1
0
 private void Reset()
 {
     EIP = 0;
     TempPool.Clear();
     RuntimeStack.Clear();
     VarSeg      = Generator.VarSeg;
     DataSegment = new int[Generator.VarSeg.Count];
     Initialized = new bool[Generator.VarSeg.Count];
     for (int i = 0; i < DataSegment.Length; ++i)
     {
         DataSegment[i] = 0;
         Initialized[i] = false;
     }
 }
Exemplo n.º 2
0
        private void Execute(PNode cmd)
        {
            switch (cmd.INS)
            {
            case PCode.OPR:
                switch (cmd.Arg)
                {
                case 1:
                    Push(-Pop());
                    break;

                case 2:
                    Push(Pop() + Pop());
                    break;

                case 3:
                    int tmp1 = Pop();
                    Push(Pop() - tmp1);
                    break;

                case 4:
                    Push(Pop() * Pop());
                    break;

                case 5:
                    tmp1 = Pop();
                    Push(Pop() / tmp1);
                    break;

                case 6:
                    Push(Math.Abs(Pop()) & 1);
                    break;

                case 7:
                    Push(~(Math.Abs(Pop()) & 1));
                    break;

                case 8:
                    PushBoolean(Pop() == Pop());
                    break;

                case 9:
                    PushBoolean(Pop() != Pop());
                    break;

                case 10:
                    PushBoolean(Pop() > Pop());
                    break;

                case 11:
                    PushBoolean(Pop() <= Pop());
                    break;

                case 12:
                    PushBoolean(Pop() < Pop());
                    break;

                case 13:
                    PushBoolean(Pop() >= Pop());
                    break;
                }
                break;

            case PCode.CAL:
                Push(EIP + 1);
                EIP = cmd.Arg;
                return;

            case PCode.EXP:
                TempPool.Clear();
                EIP = Pop();
                return;

            case PCode.HALT:
                return;

            case PCode.JMP:
                EIP = cmd.Arg;
                TempPool.Clear();
                return;

            case PCode.JPC:
                if (Pop() == 1)
                {
                    EIP++;
                }
                else
                {
                    EIP = cmd.Arg;
                }
                TempPool.Clear();
                return;

            case PCode.LIT:
                Push(cmd.Arg);
                break;

            case PCode.LOD:
                if (cmd.DataType == 3)
                {
                    if (Initialized[cmd.Arg] == false)
                    {
                        throw new Exception($"使用了未初始化的变量{VarSeg[cmd.Arg].Value}");
                    }
                    Push(DataSegment[cmd.Arg]);
                }
                else
                {
                    Push(TempPool[cmd.Arg]);
                }
                break;

            case PCode.RED:
                string i;
                if (WriteString == null)
                {
                    Console.WriteLine($"Please input a value for {VarSeg[cmd.Arg].Value}");
                }
                else
                {
                    WriteString($"Please input a value for {VarSeg[cmd.Arg].Value}");
                }
                if (Read == null)
                {
                    i = Console.ReadLine();
                }
                else
                {
                    i = Read();
                }
                int res;
                while (!int.TryParse(i, out res))
                {
                    if (WriteString != null)
                    {
                        WriteString("Please input a legal INT");
                    }
                    else
                    {
                        Console.WriteLine("Please input a legal INT");
                    }
                    if (Read != null)
                    {
                        i = Read();
                    }
                    else
                    {
                        i = Console.ReadLine();
                    }
                }
                Initialized[cmd.Arg] = true;
                DataSegment[cmd.Arg] = res;
                break;

            case PCode.STO:
                if (cmd.DataType == 3)
                {
                    Initialized[cmd.Arg] = true;
                    DataSegment[cmd.Arg] = Pop();
                }
                else
                {
                    if (TempPool.ContainsKey(cmd.Arg))
                    {
                        TempPool[cmd.Arg] = Pop();
                    }
                    else
                    {
                        TempPool.Add(cmd.Arg, Pop());
                    }
                }
                break;

            case PCode.WRT:
                if (Write == null)
                {
                    Console.WriteLine(Pop());
                }
                else
                {
                    Write(Pop());
                }
                break;
            }
            ++EIP;
        }