Skip to content

Commit

Permalink
WIP to implement proper try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed Apr 12, 2024
1 parent ccb2897 commit f9dafe8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public interface StatementCompiler {

Optional<FunctionHeader> getFunctionHeader();

Optional<TypeID> getThrownType();

void addLocalVariable(CompilingVariable variable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public ExpressionCompiler withLocalVariables(List<CompilingVariable> variables)
@Override
public StatementCompiler forLambda(LambdaClosure closure, FunctionHeader header) {
LocalSymbols newLocals = locals.forLambda(closure, header);
return new StatementCompilerImpl(context, localType, types, header, newLocals);
return new StatementCompilerImpl(context, localType, types, header, newLocals, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ExpressionCompiler forFieldInitializers() {

@Override
public StatementCompiler forMethod(FunctionHeader header) {
return new StatementCompilerImpl(context, localType, types, header, new LocalSymbols(header));
return new StatementCompilerImpl(context, localType, types, header, new LocalSymbols(header), null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ public class StatementCompilerImpl implements StatementCompiler {
private final LocalType localType;
private final LocalSymbols locals;
private final FunctionHeader functionHeader;
private final TypeID thrownType;
private final TypeBuilder types;

public StatementCompilerImpl(CompileContext context, LocalType localType, TypeBuilder types, FunctionHeader functionHeader, LocalSymbols locals) {
public StatementCompilerImpl(CompileContext context, LocalType localType, TypeBuilder types, FunctionHeader functionHeader, LocalSymbols locals, TypeID thrownType) {
this.context = context;
this.localType = localType;
this.functionHeader = functionHeader;
this.locals = locals;
this.types = types.withGeneric(functionHeader.typeParameters);
this.thrownType = thrownType;
expressionCompiler = new ExpressionCompilerImpl(context, localType, this.types, functionHeader.thrownType, locals, functionHeader);
}

Expand All @@ -48,19 +50,19 @@ public ResolvedType resolve(TypeID type) {

@Override
public StatementCompiler forBlock() {
return new StatementCompilerImpl(context, localType, types, functionHeader, locals.forBlock());
return new StatementCompilerImpl(context, localType, types, functionHeader, locals.forBlock(), thrownType);
}

@Override
public StatementCompiler forLoop(CompilingLoopStatement loop) {
return new StatementCompilerImpl(context, localType, types, functionHeader, locals.forLoop(loop, loop.getLabels().toArray(new String[0])));
return new StatementCompilerImpl(context, localType, types, functionHeader, locals.forLoop(loop, loop.getLabels().toArray(new String[0])), thrownType);
}

@Override
public StatementCompiler forCatch(CompilingVariable exceptionVariable) {
LocalSymbols locals = this.locals.forBlock();
locals.add(exceptionVariable);
return new StatementCompilerImpl(context, localType, types, functionHeader.withThrownType(exceptionVariable.getActualType()), locals);
return new StatementCompilerImpl(context, localType, types, functionHeader.withThrownType(exceptionVariable.getActualType()), locals, null);
}

@Override
Expand All @@ -73,6 +75,11 @@ public Optional<FunctionHeader> getFunctionHeader() {
return Optional.ofNullable(functionHeader);
}

@Override
public Optional<TypeID> getThrownType() {
return thrownType == null ? Optional.ofNullable(functionHeader).map(header -> header.thrownType) : Optional.of(thrownType);
}

@Override
public void addLocalVariable(CompilingVariable variable) {
locals.add(variable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public MemberCompiler forExpansionMembers(TypeID extended, TypeSymbol expansion)

@Override
public StatementCompiler forScripts(FunctionHeader scriptHeader) {
return new StatementCompilerImpl(context, null, localTypeBuilder, scriptHeader, new LocalSymbols(scriptHeader));
return new StatementCompilerImpl(context, null, localTypeBuilder, scriptHeader, new LocalSymbols(scriptHeader), null);
}

private class FileTypeBuilder extends AbstractTypeBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public ParsedStatementThrow(CodePosition position, ParsedAnnotation[] annotation
@Override
public CompilingStatement compile(StatementCompiler compiler, CodeBlock lastBlock) {
Optional<FunctionHeader> maybeHeader = compiler.getFunctionHeader();
if (!maybeHeader.isPresent())
if (!compiler.getThrownType().isPresent())
return new InvalidCompilingStatement(position, lastBlock, CompileErrors.cannotThrowHere());

FunctionHeader header = maybeHeader.get();
FunctionHeader header = maybeHeader.orElse(null);
return new Compiling(compiler, expression.compile(compiler.expressions()), lastBlock, header);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#output: TestException
#output: test

try {
if true {
throw new TestException1("test");
} else {
throw new TestException2("test");
}
} catch ex as TestException1 {
println(typeof(ex));
println(ex.message);
} catch ex as TestException2 {
println(typeof(ex));
println(ex.message);
}

0 comments on commit f9dafe8

Please sign in to comment.