Omit unnecessary continue $main

This commit is contained in:
Alexey Andreev 2015-05-04 16:09:16 +03:00
parent 77eba4c78a
commit b4e4dd8d43

View File

@ -67,6 +67,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
private Map<String, String> blockIdMap = new HashMap<>(); private Map<String, String> blockIdMap = new HashMap<>();
private List<Set<String>> debugNames = new ArrayList<>(); private List<Set<String>> debugNames = new ArrayList<>();
private List<String> cachedVariableNames = new ArrayList<>(); private List<String> cachedVariableNames = new ArrayList<>();
private boolean end;
private int currentPart;
private static class OperatorPrecedence { private static class OperatorPrecedence {
Priority priority; Priority priority;
@ -657,6 +659,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
writer.append(";").softNewLine(); writer.append(";").softNewLine();
} }
end = true;
currentPart = 0;
method.getBody().acceptVisitor(Renderer.this); method.getBody().acceptVisitor(Renderer.this);
} catch (IOException e) { } catch (IOException e) {
throw new RenderingException("IO error occured", e); throw new RenderingException("IO error occured", e);
@ -743,6 +747,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
emitSuspendChecker(); emitSuspendChecker();
} }
AsyncMethodPart part = methodNode.getBody().get(i); AsyncMethodPart part = methodNode.getBody().get(i);
end = true;
currentPart = i;
part.getStatement().acceptVisitor(Renderer.this); part.getStatement().acceptVisitor(Renderer.this);
writer.outdent(); writer.outdent();
} }
@ -901,9 +907,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
@Override @Override
public void visit(SequentialStatement statement) { public void visit(SequentialStatement statement) {
for (Statement part : statement.getSequence()) { visitStatements(statement.getSequence());
part.acceptVisitor(this);
}
} }
@Override @Override
@ -924,9 +928,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
debugEmitter.emitCallSite(); debugEmitter.emitCallSite();
writer.append(")").ws().append("{").softNewLine().indent(); writer.append(")").ws().append("{").softNewLine().indent();
for (Statement part : statement.getConsequent()) { visitStatements(statement.getConsequent());
part.acceptVisitor(this);
}
if (!statement.getAlternative().isEmpty()) { if (!statement.getAlternative().isEmpty()) {
writer.outdent().append("}").ws(); writer.outdent().append("}").ws();
if (statement.getAlternative().size() == 1 && if (statement.getAlternative().size() == 1 &&
@ -936,9 +938,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
continue; continue;
} }
writer.append("else").ws().append("{").indent().softNewLine(); writer.append("else").ws().append("{").indent().softNewLine();
for (Statement part : statement.getAlternative()) { visitStatements(statement.getAlternative());
part.acceptVisitor(this);
}
} }
break; break;
} }
@ -973,16 +973,22 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("case ").append(condition).append(":").softNewLine(); writer.append("case ").append(condition).append(":").softNewLine();
} }
writer.indent(); writer.indent();
boolean oldEnd = end;
for (Statement part : clause.getBody()) { for (Statement part : clause.getBody()) {
end = false;
part.acceptVisitor(this); part.acceptVisitor(this);
} }
end = oldEnd;
writer.outdent(); writer.outdent();
} }
if (statement.getDefaultClause() != null) { if (statement.getDefaultClause() != null) {
writer.append("default:").softNewLine().indent(); writer.append("default:").softNewLine().indent();
boolean oldEnd = end;
for (Statement part : statement.getDefaultClause()) { for (Statement part : statement.getDefaultClause()) {
end = false;
part.acceptVisitor(this); part.acceptVisitor(this);
} }
end = oldEnd;
writer.outdent(); writer.outdent();
} }
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
@ -1015,9 +1021,12 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("true"); writer.append("true");
} }
writer.append(")").ws().append("{").softNewLine().indent(); writer.append(")").ws().append("{").softNewLine().indent();
boolean oldEnd = end;
for (Statement part : statement.getBody()) { for (Statement part : statement.getBody()) {
end = false;
part.acceptVisitor(this); part.acceptVisitor(this);
} }
end = oldEnd;
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
} catch (IOException e) { } catch (IOException e) {
throw new RenderingException("IO error occured", e); throw new RenderingException("IO error occured", e);
@ -1047,9 +1056,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
public void visit(BlockStatement statement) { public void visit(BlockStatement statement) {
try { try {
writer.append(mapBlockId(statement.getId())).append(":").ws().append("{").softNewLine().indent(); writer.append(mapBlockId(statement.getId())).append(":").ws().append("{").softNewLine().indent();
for (Statement part : statement.getBody()) { visitStatements(statement.getBody());
part.acceptVisitor(this);
}
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
} catch (IOException e) { } catch (IOException e) {
throw new RenderingException("IO error occured", e); throw new RenderingException("IO error occured", e);
@ -2019,6 +2026,17 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
} }
private void visitStatements(List<Statement> statements) {
boolean oldEnd = end;
for (int i = 0; i < statements.size() - 1; ++i) {
end = false;
statements.get(i).acceptVisitor(this);
}
end = oldEnd;
statements.get(statements.size() - 1).acceptVisitor(this);
end = oldEnd;
}
@Override @Override
public void visit(TryCatchStatement statement) { public void visit(TryCatchStatement statement) {
try { try {
@ -2031,9 +2049,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
sequence.add(nextStatement); sequence.add(nextStatement);
protectedBody = nextStatement.getProtectedBody(); protectedBody = nextStatement.getProtectedBody();
} }
for (Statement part : protectedBody) { visitStatements(protectedBody);
part.acceptVisitor(this);
}
writer.outdent().append("}").ws().append("catch").ws().append("($e)") writer.outdent().append("}").ws().append("catch").ws().append("($e)")
.ws().append("{").indent().softNewLine(); .ws().append("{").indent().softNewLine();
writer.append("$je").ws().append("=").ws().append("$e.$javaException;").softNewLine(); writer.append("$je").ws().append("=").ws().append("$e.$javaException;").softNewLine();
@ -2048,9 +2064,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws() writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws()
.append("$je;").softNewLine(); .append("$je;").softNewLine();
} }
for (Statement part : catchClause.getHandler()) { visitStatements(catchClause.getHandler());
part.acceptVisitor(this);
}
writer.outdent().append("}").ws().append("else "); writer.outdent().append("}").ws().append("else ");
} }
writer.append("{").indent().softNewLine(); writer.append("{").indent().softNewLine();
@ -2067,7 +2081,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
try { try {
writer.append(pointerName()).ws().append("=").ws().append(statement.getPart()).append(";") writer.append(pointerName()).ws().append("=").ws().append(statement.getPart()).append(";")
.softNewLine(); .softNewLine();
if (!end || statement.getPart() != currentPart + 1) {
writer.append("continue ").append(mainLoopName()).append(";").softNewLine(); writer.append("continue ").append(mainLoopName()).append(";").softNewLine();
}
} catch (IOException ex){ } catch (IOException ex){
throw new RenderingException("IO error occured", ex); throw new RenderingException("IO error occured", ex);
} }