コード例 #1
0
        public static void interpret(
            GlobalInterpreterState globalState,
            LocalInterpreterState localState,
            bool debugExecution,
            out bool programExecutedSuccessful,
            out bool hardExecutionError
            )
        {
            programExecutedSuccessful = false;
            hardExecutionError        = false;

            /*
             * if( arguments.lengthOfProgram >= 4 ) {
             *  if( arguments.program[0] == 3 && arguments.program[1] == 62 && arguments.program[2] == 2 && arguments.program[3] == 31 ) {
             *      int debugHere = 5;
             *  }
             * }
             * //*/

            if (localState.program.length >= 3)
            {
                if (localState.program[0] == 35 && localState.program[1] == 32 && localState.program[2] == InstructionInterpreter.convInstructionAndRelativeToInstruction(1, -3))
                {
                    int debugHere = 5;
                }
            }

            for (int instructionsRetired = 0; instructionsRetired < localState.maxNumberOfRetiredInstructions; instructionsRetired++)
            {
                bool instructionPointerValid = localState.instructionPointer >= 0 && localState.instructionPointer < localState.program.length;
                if (!instructionPointerValid)
                {
                    hardExecutionError = true;
                    break;
                }

                uint currentInstruction = localState.program[localState.instructionPointer];

                if (debugExecution)
                {
                    Console.WriteLine("program=");

                    throw new NotImplementedException(); // TODO< logger >
                    //Program2.debug(null, arguments.program);

                    Console.WriteLine("ip={0}", localState.instructionPointer);
                    Console.Write("arr=");

                    throw new NotImplementedException(); // TODO< logger >
                    //Program2.debug(null, arguments.interpreterState.arrayState.array);
                    Console.WriteLine("array index={0}", globalState.arrayState.index);
                }

                if (InstructionInterpreter.isTerminating(globalState, localState, currentInstruction))
                {
                    programExecutedSuccessful = true; // the program executed successfully only if we return
                    break;
                }

                bool instructionExecutedSuccessfull;
                int  indirectCallIndex;
                InstructionInterpreter.dispatch(globalState, localState, new Instruction(currentInstruction), out instructionExecutedSuccessfull, out indirectCallIndex);
                if (!instructionExecutedSuccessfull)
                {
                    hardExecutionError = true;
                    break;
                }

                if (indirectCallIndex != -1)
                {
                    // an indirect call is a call to an (interpreted) function

                    // try to dispatch indirect call
                    LocalInterpreterState indirectLocalInterpreterState;
                    if (!localState.indirectCallTable.TryGetValue((uint)indirectCallIndex, out indirectLocalInterpreterState))
                    {
                        hardExecutionError = true;
                        break;
                    }

                    indirectLocalInterpreterState.reset();

                    bool calleeExecutedSuccessfully, calleeHardExecutionError;
                    interpret(globalState, indirectLocalInterpreterState, debugExecution, out calleeExecutedSuccessfully, out calleeHardExecutionError);
                    if (calleeExecutedSuccessfully)
                    {
                        // do nothing
                    }
                    else if (calleeHardExecutionError)
                    {
                        hardExecutionError = true;
                        break;
                    }
                }
            }

            if (hardExecutionError)
            {
                programExecutedSuccessful = false;
            }
        }