Search in sources :

Example 1 with ForIntermediate

use of org.candle.decompiler.intermediate.code.loop.ForIntermediate in project candle-decompiler by bradsdavis.

the class WhileToForLoopIncrement method visitWhileIntermediate.

@Override
public void visitWhileIntermediate(WhileIntermediate line) {
    List<AbstractIntermediate> predecessors = Graphs.predecessorListOf(igc.getGraph(), line);
    //look at the predecessor lines;  validate there are only 2 predecessors.
    if (predecessors.size() == 2) {
        StatementIntermediate declaration = null;
        StatementIntermediate iteration = null;
        for (AbstractIntermediate predecessor : predecessors) {
            if (comparator.before(predecessor, line)) {
                if (predecessor instanceof StatementIntermediate) {
                    declaration = (StatementIntermediate) predecessor;
                    continue;
                } else {
                    //a for loop.
                    return;
                }
            }
            //if the 
            if (comparator.after(predecessor, line)) {
                if (predecessor instanceof StatementIntermediate) {
                    iteration = (StatementIntermediate) predecessor;
                } else {
                    return;
                }
            }
        }
        //at this point, both should be set.
        if (declaration != null && iteration != null) {
            if (declaration.getExpression() instanceof Declaration) {
                Declaration declarationExpression = (Declaration) declaration.getExpression();
                if (iteration.getExpression() instanceof Increment) {
                    Increment incrementExpression = (Increment) iteration.getExpression();
                    if (incrementExpression.getVariable().getType().equals(declarationExpression.getVariable().getType())) {
                        //now check names.
                        if (incrementExpression.getVariable().getName().equals(declarationExpression.getVariable().getName())) {
                            //we can actually convert this to a for loop.
                            ForIntermediate forIntermediate = new ForIntermediate(line, declarationExpression, incrementExpression);
                            //forIntermediate.setTrueBranch(line.getTrueBranch());
                            //forIntermediate.setFalseBranch(line.getFalseBranch());
                            igc.getGraph().addVertex(forIntermediate);
                            igc.redirectSuccessors(line, forIntermediate);
                            igc.redirectPredecessors(iteration, forIntermediate);
                            igc.redirectPredecessors(declaration, forIntermediate);
                            //remove the while loop, increment, and declaration.
                            igc.getGraph().removeVertex(line);
                            igc.getGraph().removeVertex(declaration);
                            igc.getGraph().removeVertex(iteration);
                        }
                    }
                }
            }
        }
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) StatementIntermediate(org.candle.decompiler.intermediate.code.StatementIntermediate) Increment(org.candle.decompiler.intermediate.expression.Increment) Declaration(org.candle.decompiler.intermediate.expression.Declaration) ForIntermediate(org.candle.decompiler.intermediate.code.loop.ForIntermediate)

Aggregations

AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)1 StatementIntermediate (org.candle.decompiler.intermediate.code.StatementIntermediate)1 ForIntermediate (org.candle.decompiler.intermediate.code.loop.ForIntermediate)1 Declaration (org.candle.decompiler.intermediate.expression.Declaration)1 Increment (org.candle.decompiler.intermediate.expression.Increment)1