added fragments
This commit is contained in:
+9
@@ -285,20 +285,29 @@ final class PbsExecutableBodyLowerer {
|
||||
private boolean lowerForStatement(
|
||||
final PbsAst.ForStatement forStatement,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var iteratorSlot = context.declareLocalSlot(forStatement.iteratorName());
|
||||
lowerExpression(forStatement.fromExpression(), context);
|
||||
emitSetLocal(iteratorSlot, forStatement.span(), context);
|
||||
final var loopStart = context.nextLabel("for_start");
|
||||
final var loopExit = context.nextLabel("for_exit");
|
||||
final var loopContinue = context.nextLabel("for_continue");
|
||||
emitLabel(loopStart, forStatement.span(), context);
|
||||
emitGetLocal(iteratorSlot, forStatement.span(), context);
|
||||
lowerExpression(forStatement.untilExpression(), context);
|
||||
emitBinaryOperatorInstruction("<", forStatement.span(), context);
|
||||
emitJump(IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE, loopExit, forStatement.span(), context);
|
||||
context.pushLoop(loopContinue, loopExit);
|
||||
lowerBlock(forStatement.body(), context);
|
||||
context.popLoop();
|
||||
emitLabel(loopContinue, forStatement.span(), context);
|
||||
emitGetLocal(iteratorSlot, forStatement.span(), context);
|
||||
if (forStatement.stepExpression() != null) {
|
||||
lowerExpression(forStatement.stepExpression(), context);
|
||||
} else {
|
||||
emitPushI32(1, forStatement.span(), context);
|
||||
}
|
||||
emitBinaryOperatorInstruction("+", forStatement.span(), context);
|
||||
emitSetLocal(iteratorSlot, forStatement.span(), context);
|
||||
emitJump(IRBackendExecutableFunction.InstructionKind.JMP, loopStart, forStatement.span(), context);
|
||||
emitLabel(loopExit, forStatement.span(), context);
|
||||
return false;
|
||||
|
||||
+31
@@ -369,6 +369,37 @@ class PbsFrontendCompilerTest {
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldLowerForLoopIteratorBoundsAndStep() {
|
||||
final var source = """
|
||||
fn main() -> int {
|
||||
let total: int = 0;
|
||||
for i: int from 0 until 10 step 2 {
|
||||
total += i;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
""";
|
||||
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
final var fileBackend = compiler.compileFile(new FileId(102), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
|
||||
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.LT));
|
||||
assertTrue(executableMain.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.ADD));
|
||||
assertTrue(executableMain.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.SET_LOCAL));
|
||||
assertTrue(executableMain.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.GET_LOCAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotLowerWhenSyntaxErrorsExist() {
|
||||
final var source = """
|
||||
|
||||
Reference in New Issue
Block a user