implements PR-05.1
This commit is contained in:
+61
@@ -123,6 +123,67 @@ class PbsFrontendCompilerTest {
|
||||
assertEquals("c", executableA.instructions().get(1).calleeCallableName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldStopLoweringReachablePathAfterReturn() {
|
||||
final var source = """
|
||||
fn a() -> int { return 1; }
|
||||
fn b() -> int { return 2; }
|
||||
fn main() -> int {
|
||||
return a();
|
||||
b();
|
||||
}
|
||||
""";
|
||||
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
final var fileBackend = compiler.compileFile(new FileId(100), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.isEmpty(), "Valid program should not report diagnostics");
|
||||
final var executableMain = fileBackend.executableFunctions().stream()
|
||||
.filter(fn -> fn.callableName().equals("main"))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
assertEquals(2, executableMain.instructions().size());
|
||||
assertEquals("a", executableMain.instructions().get(0).calleeCallableName());
|
||||
assertEquals(p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.RET, executableMain.instructions().get(1).kind());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEmitControlFlowInstructionsForIfAndWhile() {
|
||||
final var source = """
|
||||
fn cond() -> bool { return true; }
|
||||
fn t() -> int { return 1; }
|
||||
fn f() -> int { return 2; }
|
||||
fn loopBody() -> void { return; }
|
||||
fn main() -> int {
|
||||
while cond() {
|
||||
break;
|
||||
}
|
||||
if cond() {
|
||||
return t();
|
||||
} else {
|
||||
return f();
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
final var fileBackend = compiler.compileFile(new FileId(101), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.isEmpty(), "Valid program should not report diagnostics");
|
||||
final var executableMain = fileBackend.executableFunctions().stream()
|
||||
.filter(fn -> fn.callableName().equals("main"))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
assertTrue(executableMain.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.LABEL));
|
||||
assertTrue(executableMain.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.JMP));
|
||||
assertTrue(executableMain.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotLowerWhenSyntaxErrorsExist() {
|
||||
final var source = """
|
||||
|
||||
Reference in New Issue
Block a user