List<Instruction> (@NotNull List<Instruction> instructions) { List<Instruction> simplified = new ArrayList<>(); Instruction prev = null; for (Instruction instruction : instructions) { Assignment assignment = tryCast(instruction, Assignment.class); if (assignment != null) { Declaration declaration = tryCast(prev, Declaration.class); if (declaration != null && declaration.myLhs == assignment.myLhs) { Declaration merged = new Declaration(declaration.myLhs, assignment.myRhs); simplified.set(simplified.size() - 1, merged); prev = merged; continue; } } else { Check check = tryCast(instruction, Check.class); if (check != null && !check.hasElseBranch()) check.myInstructions = run(check.myInstructions); } simplified.add(instruction); prev = instruction; } return simplified; }