Add wielding of try/catch statements

This commit is contained in:
konsoletyper 2015-02-06 01:56:39 +04:00
parent 5dfc8a3ed6
commit de7dc645bc
3 changed files with 49 additions and 1 deletions

View File

@ -321,9 +321,45 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
return false; return false;
} }
} }
wieldTryCatch(statements);
return true; return true;
} }
private void wieldTryCatch(List<Statement> statements) {
for (int i = 0; i < statements.size() - 1; ++i) {
if (statements.get(i) instanceof TryCatchStatement && statements.get(i + 1) instanceof TryCatchStatement) {
TryCatchStatement first = (TryCatchStatement)statements.get(i);
TryCatchStatement second = (TryCatchStatement)statements.get(i + 1);
if (Objects.equals(first.getExceptionType(), second.getExceptionType()) &&
Objects.equals(first.getExceptionVariable(), second.getExceptionVariable()) &&
briefStatementComparison(first.getHandler(), second.getHandler())) {
first.getProtectedBody().addAll(second.getProtectedBody());
statements.remove(i + 1);
wieldTryCatch(first.getProtectedBody());
--i;
continue;
}
}
}
}
private boolean briefStatementComparison(List<Statement> firstSeq, List<Statement> secondSeq) {
if (firstSeq.isEmpty() && secondSeq.isEmpty()) {
return true;
}
if (firstSeq.size() != 1 || secondSeq.size() != 1) {
return false;
}
Statement first = firstSeq.get(0);
Statement second = secondSeq.get(0);
if (first instanceof BreakStatement && second instanceof BreakStatement) {
BreakStatement firstBreak = (BreakStatement)first;
BreakStatement secondBreak = (BreakStatement)second;
return firstBreak.getTarget() == secondBreak.getTarget();
}
return false;
}
private void eliminateRedundantBreaks(List<Statement> statements, IdentifiedStatement exit) { private void eliminateRedundantBreaks(List<Statement> statements, IdentifiedStatement exit) {
if (statements.isEmpty()) { if (statements.isEmpty()) {
return; return;

View File

@ -641,7 +641,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(")").ws().append("{").indent().softNewLine(); writer.append(")").ws().append("{").indent().softNewLine();
AsyncMethodPart part = methodNode.getBody().get(i); AsyncMethodPart part = methodNode.getBody().get(i);
part.getStatement().acceptVisitor(Renderer.this); part.getStatement().acceptVisitor(Renderer.this);
writer.append("return $return($rt_asyncError($guard));").softNewLine();
writer.outdent().append("},").ws().append("$return);").softNewLine(); writer.outdent().append("},").ws().append("$return);").softNewLine();
} }
writer.append("return $part_0();").softNewLine(); writer.append("return $part_0();").softNewLine();

View File

@ -55,5 +55,18 @@ public final class AsyncProgram {
} }
} }
System.out.println("Complete async"); System.out.println("Complete async");
System.out.println("Throwing exception");
try {
throwException();
} catch (IllegalStateException e) {
System.out.println("Exception caught");
}
}
private static void throwException() {
Thread.yield();
System.out.println("Thread.yield called");
throw new IllegalStateException();
} }
} }