示例#1
0
 /// <name>EndBlock</name>
 /// <type>Method</type>
 /// <summary>
 /// End block code generator.
 /// </summary>
 private void EndBlock()
 {
     Structure s = structureStack.Pop();
     if (s.Type == StructType.ELSE_STRUCT && nextOperator.Lexeme == "endif")
     {
         // End of an else block
         FillAddress(s.JumpLocation, jvm.ProgramCounter);
     }
     else if (s.Type == StructType.IF_STRUCT && nextOperator.Lexeme == "else")
     {
         // End of an if block but start of else block
         Structure elseStruct = new Structure(StructType.ELSE_STRUCT);
         elseStruct.JumpLocation = jvm.ProgramCounter;
         jvm.Emit(Opcode.GOTO, 0);
         structureStack.Push(elseStruct);
         FillAddress(s.JumpLocation, jvm.ProgramCounter);
     }
     else if (s.Type == StructType.IF_STRUCT && nextOperator.Lexeme == "endif")
     {
         // End of an if block with no else block
         FillAddress(s.JumpLocation, jvm.ProgramCounter);
     }
     else if (s.Type == StructType.WHILE_STRUCT && nextOperator.Lexeme == "endwhile")
     {
         // End of a while block
         jvm.Emit(Opcode.GOTO, s.ConditionLocation - jvm.ProgramCounter);
         FillAddress(s.JumpLocation, jvm.ProgramCounter);
     }
     else if (s.Type == StructType.PROC_STRUCT && nextOperator.Lexeme == "endproc")
     {
         // End of a procedure
         jvm.Emit(Opcode.ASTORE_0);
         jvm.Emit(Opcode.RET, (byte)0);
         symbolTable["$MAIN"].Address = jvm.ProgramCounter;
     }
     else if (s.Type == StructType.PROGRAM_STRUCT && nextOperator.Lexeme == "endprogram")
     {
         // End of the program
         jvm.Emit(Opcode.RETURN);
         status = Status.EXIT;
     }
     else
     {
         Compiler.ThrowCompilerException("Structure type does not match control block");
     }
 }
示例#2
0
        /// <name>BeginCondition</name>
        /// <type>Method</type>
        /// <summary>
        /// Begin condition code generator.
        /// </summary>
        private void BeginCondition()
        {
            if (nextOperator.Lexeme == "if")
            {
                // Start of if block, push the new structure onto the stack
                Structure ifStruct = new Structure(StructType.IF_STRUCT);
                ifStruct.ConditionLocation = jvm.ProgramCounter;
                Advance();

                // Handle the conditional part of the statement
                CompileCondition();

                // Now set the return location to the beginning of the branch
                ifStruct.JumpLocation = jvm.ProgramCounter - 3;
                structureStack.Push(ifStruct);
            }
            else
            {
                // Start of while block, push the new structure onto the stack
                Structure whileStruct = new Structure(StructType.WHILE_STRUCT);
                whileStruct.ConditionLocation = jvm.ProgramCounter;
                Advance();

                // Handle the conditional part of the statement
                CompileCondition();

                // Now set the return location to the beginning of the branch
                whileStruct.JumpLocation = jvm.ProgramCounter - 3;
                structureStack.Push(whileStruct);
            }
        }