Esempio n. 1
0
  public Instruction writeConditional(BranchSet _branchSet, boolean _invert)
      throws CodeGenException {

    final LogicalExpressionNode logicalExpression = _branchSet.getLogicalExpression();
    if (!_invert) {
      logicalExpression.invert();
    }
    write(logicalExpression);
    return (_branchSet.getLast().getNextExpr());
  }
Esempio n. 2
0
  public void writeComposite(CompositeInstruction instruction) throws CodeGenException {
    if (instruction instanceof CompositeArbitraryScopeInstruction) {
      newLine();

      writeBlock(instruction.getFirstChild(), null);
    } else if (instruction instanceof CompositeIfInstruction) {
      newLine();
      write("if (");
      final Instruction blockStart = writeConditional(instruction.getBranchSet());

      write(")");
      writeBlock(blockStart, null);
    } else if (instruction instanceof CompositeIfElseInstruction) {
      newLine();
      write("if (");
      final Instruction blockStart = writeConditional(instruction.getBranchSet());
      write(")");
      Instruction elseGoto = blockStart;
      while (!(elseGoto.isBranch() && elseGoto.asBranch().isUnconditional())) {
        elseGoto = elseGoto.getNextExpr();
      }
      writeBlock(blockStart, elseGoto);
      write(" else ");
      writeBlock(elseGoto.getNextExpr(), null);
    } else if (instruction instanceof CompositeForSunInstruction) {
      newLine();
      write("for (");
      Instruction topBranch = instruction.getFirstChild();
      if (topBranch instanceof AssignToLocalVariable) {
        writeInstruction(topBranch);
        topBranch = topBranch.getNextExpr();
      }
      write("; ");
      final BranchSet branchSet = instruction.getBranchSet();
      final Instruction blockStart = writeConditional(branchSet);

      final Instruction lastGoto = instruction.getLastChild();

      if (branchSet.getFallThrough() == lastGoto) {
        // empty body no delta!
        write(";){}");
      } else {
        final Instruction delta = lastGoto.getPrevExpr();
        write("; ");
        if (!(delta instanceof CompositeInstruction)) {
          writeInstruction(delta);
          write(")");
          writeBlock(blockStart, delta);
        } else {
          write("){");
          in();
          writeSequence(blockStart, delta);

          newLine();
          writeSequence(delta, delta.getNextExpr());
          out();
          newLine();
          write("}");
        }
      }

    } else if (instruction instanceof CompositeWhileInstruction) {
      newLine();
      write("while (");
      final BranchSet branchSet = instruction.getBranchSet();
      final Instruction blockStart = writeConditional(branchSet);
      write(")");
      final Instruction lastGoto = instruction.getLastChild();
      writeBlock(blockStart, lastGoto);

    } else if (instruction instanceof CompositeEmptyLoopInstruction) {
      newLine();
      write("for (");
      Instruction topBranch = instruction.getFirstChild();
      if (topBranch instanceof AssignToLocalVariable) {
        writeInstruction(topBranch);
        topBranch = topBranch.getNextExpr();
      }
      write("; ");
      writeConditional(instruction.getBranchSet());
      write(";){}");

    } else if (instruction instanceof CompositeForEclipseInstruction) {
      newLine();
      write("for (");
      Instruction topGoto = instruction.getFirstChild();
      if (topGoto instanceof AssignToLocalVariable) {
        writeInstruction(topGoto);
        topGoto = topGoto.getNextExpr();
      }
      write("; ");
      Instruction last = instruction.getLastChild();
      while (last.getPrevExpr().isBranch()) {
        last = last.getPrevExpr();
      }
      writeConditional(instruction.getBranchSet(), true);
      write("; ");
      final Instruction delta = last.getPrevExpr();
      if (!(delta instanceof CompositeInstruction)) {
        writeInstruction(delta);
        write(")");
        writeBlock(topGoto.getNextExpr(), delta);
      } else {
        write("){");
        in();
        writeSequence(topGoto.getNextExpr(), delta);

        newLine();
        writeSequence(delta, delta.getNextExpr());
        out();
        newLine();
        write("}");
      }

    } else if (instruction instanceof CompositeDoWhileInstruction) {
      newLine();
      write("do");
      Instruction blockStart = instruction.getFirstChild();
      Instruction blockEnd = instruction.getLastChild();
      writeBlock(blockStart, blockEnd);
      write("while(");
      writeConditional(((CompositeInstruction) instruction).getBranchSet(), true);
      write(");");
      newLine();
    }
  }