Esempio n. 1
0
  @Override
  public ArrayList<Instruction> generate() {
    ArrayList<Instruction> instructions = new ArrayList<>();
    instructions.addAll(r.generate());

    Instruction.Op op = null;

    switch (r.relOpToken.getLexeme()) {
      case ("=="):
        op = Instruction.Op.beq;
        break;
      case ("!="):
        op = Instruction.Op.bne;
        break;
      case ("<"):
        op = Instruction.Op.blt;
        break;
      case ("<="):
        op = Instruction.Op.ble;
        break;
      case (">"):
        op = Instruction.Op.bgt;
        break;
      case (">="):
        op = Instruction.Op.bge;
        break;
      default:
        break;
    }

    ArrayList<Instruction> thenBlock = tthen.generate();
    ArrayList<Instruction> elseBlock = new ArrayList<>();
    Instruction endTarget = new Instruction(Instruction.Op.noop, null, null);

    if (eelse != null) {
      elseBlock.addAll(eelse.generate());
    }

    elseBlock.add(new Instruction(Instruction.Op.bra, new Result(endTarget), null));

    instructions.add(
        new Instruction(
            op,
            new Result(instructions.get(instructions.size() - 1)),
            new Result(thenBlock.get(0))));

    instructions.addAll(elseBlock);
    instructions.addAll(thenBlock);
    instructions.add(endTarget);

    return instructions;
  }