generate try/catch

This commit is contained in:
Dmitry Jemerov
2011-05-12 20:20:29 +02:00
parent 0e864dd9dc
commit e57ed7401d
2 changed files with 44 additions and 1 deletions
@@ -1206,6 +1206,48 @@ public class ExpressionCodegen extends JetVisitor {
}
}
@Override
public void visitTryExpression(JetTryExpression expression) {
if (expression.getFinallyBlock() != null) {
throw new UnsupportedOperationException("finally block in try/catch not yet supported");
}
Label tryStart = new Label();
v.mark(tryStart);
gen(expression.getTryBlock(), Type.VOID_TYPE);
Label tryEnd = new Label();
v.mark(tryEnd);
Label end = new Label();
v.goTo(end); // TODO don't generate goto if there's no code following try/catch
for (JetCatchClause clause : expression.getCatchClauses()) {
Label clauseStart = new Label();
v.mark(clauseStart);
VariableDescriptor descriptor = bindingContext.getVariableDescriptor(clause.getCatchParameter());
Type descriptorType = typeMapper.mapType(descriptor.getOutType());
myMap.enter(descriptor, 1);
int index = myMap.getIndex(descriptor);
v.store(index, descriptorType);
gen(clause.getCatchBody(), Type.VOID_TYPE);
v.goTo(end); // TODO don't generate goto if there's no code following try/catch
myMap.leave(descriptor);
v.visitTryCatchBlock(tryStart, tryEnd, clauseStart, descriptorType.getInternalName());
}
v.mark(end);
}
@Override
public void visitBinaryWithTypeRHSExpression(JetBinaryExpressionWithTypeRHS expression) {
JetSimpleNameExpression operationSign = expression.getOperationSign();
if (operationSign.getReferencedNameElementType() == JetTokens.COLON) {
gen(expression.getLeft());
}
else {
throw new UnsupportedOperationException("should generate a cast, but don't know how");
}
}
private static class CompilationException extends RuntimeException {
}
}
@@ -103,8 +103,9 @@ public class ControlStructuresTest extends CodegenTestCase {
public void testTryCatch() throws Exception {
loadFile("tryCatch.jet");
System.out.println(generateToText());
final Method main = generateFunction();
assertEquals("no message", main.invoke(null, "0"));
assertEquals("xxx", main.invoke(null, "a"));
assertEquals("For input string: \"a\"", main.invoke(null, "a"));
}
}