diff --git a/prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/lowering/PbsExecutableBodyLowerer.java b/prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/lowering/PbsExecutableBodyLowerer.java index 13e0d6c9..ef40fb11 100644 --- a/prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/lowering/PbsExecutableBodyLowerer.java +++ b/prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/lowering/PbsExecutableBodyLowerer.java @@ -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; diff --git a/prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/PbsFrontendCompilerTest.java b/prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/PbsFrontendCompilerTest.java index 4f996c01..9dfb3195 100644 --- a/prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/PbsFrontendCompilerTest.java +++ b/prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/PbsFrontendCompilerTest.java @@ -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 = """ diff --git a/test-projects/fragments/cartridge/program.pbx b/test-projects/fragments/cartridge/program.pbx index ba33b859..6be3f382 100644 Binary files a/test-projects/fragments/cartridge/program.pbx and b/test-projects/fragments/cartridge/program.pbx differ diff --git a/test-projects/fragments/src/main.pbs b/test-projects/fragments/src/main.pbs index a4eb381d..c325a22e 100644 --- a/test-projects/fragments/src/main.pbs +++ b/test-projects/fragments/src/main.pbs @@ -4,7 +4,7 @@ import { Gfx } from @sdk:gfx; declare global ticks: int = 0; declare const SCREEN_W: int = 320; declare const SCREEN_H: int = 180; -declare const CELL: int = 4; +declare const CELL: int = 8; [Init] fn init() -> void @@ -16,49 +16,12 @@ fn init() -> void fn frame() -> void { ticks += 1; - Gfx.fill_rect(0, 0, SCREEN_W, CELL, new Color((ticks * 97 + 500) % 65535)); - Gfx.fill_rect(0, 4, SCREEN_W, CELL, new Color((ticks * 110 + 1200) % 65535)); - Gfx.fill_rect(0, 8, SCREEN_W, CELL, new Color((ticks * 123 + 1900) % 65535)); - Gfx.fill_rect(0, 12, SCREEN_W, CELL, new Color((ticks * 136 + 2600) % 65535)); - Gfx.fill_rect(0, 16, SCREEN_W, CELL, new Color((ticks * 149 + 3300) % 65535)); - Gfx.fill_rect(0, 20, SCREEN_W, CELL, new Color((ticks * 162 + 4000) % 65535)); - Gfx.fill_rect(0, 24, SCREEN_W, CELL, new Color((ticks * 175 + 4700) % 65535)); - Gfx.fill_rect(0, 28, SCREEN_W, CELL, new Color((ticks * 188 + 5400) % 65535)); - Gfx.fill_rect(0, 32, SCREEN_W, CELL, new Color((ticks * 201 + 6100) % 65535)); - Gfx.fill_rect(0, 36, SCREEN_W, CELL, new Color((ticks * 214 + 6800) % 65535)); - Gfx.fill_rect(0, 40, SCREEN_W, CELL, new Color((ticks * 227 + 7500) % 65535)); - Gfx.fill_rect(0, 44, SCREEN_W, CELL, new Color((ticks * 240 + 8200) % 65535)); - Gfx.fill_rect(0, 48, SCREEN_W, CELL, new Color((ticks * 253 + 8900) % 65535)); - Gfx.fill_rect(0, 52, SCREEN_W, CELL, new Color((ticks * 266 + 9600) % 65535)); - Gfx.fill_rect(0, 56, SCREEN_W, CELL, new Color((ticks * 279 + 10300) % 65535)); - Gfx.fill_rect(0, 60, SCREEN_W, CELL, new Color((ticks * 292 + 11000) % 65535)); - Gfx.fill_rect(0, 64, SCREEN_W, CELL, new Color((ticks * 305 + 11700) % 65535)); - Gfx.fill_rect(0, 68, SCREEN_W, CELL, new Color((ticks * 318 + 12400) % 65535)); - Gfx.fill_rect(0, 72, SCREEN_W, CELL, new Color((ticks * 331 + 13100) % 65535)); - Gfx.fill_rect(0, 76, SCREEN_W, CELL, new Color((ticks * 344 + 13800) % 65535)); - Gfx.fill_rect(0, 80, SCREEN_W, CELL, new Color((ticks * 357 + 14500) % 65535)); - Gfx.fill_rect(0, 84, SCREEN_W, CELL, new Color((ticks * 370 + 15200) % 65535)); - Gfx.fill_rect(0, 88, SCREEN_W, CELL, new Color((ticks * 383 + 15900) % 65535)); - Gfx.fill_rect(0, 92, SCREEN_W, CELL, new Color((ticks * 396 + 16600) % 65535)); - Gfx.fill_rect(0, 96, SCREEN_W, CELL, new Color((ticks * 409 + 17300) % 65535)); - Gfx.fill_rect(0, 100, SCREEN_W, CELL, new Color((ticks * 422 + 18000) % 65535)); - Gfx.fill_rect(0, 104, SCREEN_W, CELL, new Color((ticks * 435 + 18700) % 65535)); - Gfx.fill_rect(0, 108, SCREEN_W, CELL, new Color((ticks * 448 + 19400) % 65535)); - Gfx.fill_rect(0, 112, SCREEN_W, CELL, new Color((ticks * 461 + 20100) % 65535)); - Gfx.fill_rect(0, 116, SCREEN_W, CELL, new Color((ticks * 474 + 20800) % 65535)); - Gfx.fill_rect(0, 120, SCREEN_W, CELL, new Color((ticks * 487 + 21500) % 65535)); - Gfx.fill_rect(0, 124, SCREEN_W, CELL, new Color((ticks * 500 + 22200) % 65535)); - Gfx.fill_rect(0, 128, SCREEN_W, CELL, new Color((ticks * 513 + 22900) % 65535)); - Gfx.fill_rect(0, 132, SCREEN_W, CELL, new Color((ticks * 526 + 23600) % 65535)); - Gfx.fill_rect(0, 136, SCREEN_W, CELL, new Color((ticks * 539 + 24300) % 65535)); - Gfx.fill_rect(0, 140, SCREEN_W, CELL, new Color((ticks * 552 + 25000) % 65535)); - Gfx.fill_rect(0, 144, SCREEN_W, CELL, new Color((ticks * 565 + 25700) % 65535)); - Gfx.fill_rect(0, 148, SCREEN_W, CELL, new Color((ticks * 578 + 26400) % 65535)); - Gfx.fill_rect(0, 152, SCREEN_W, CELL, new Color((ticks * 591 + 27100) % 65535)); - Gfx.fill_rect(0, 156, SCREEN_W, CELL, new Color((ticks * 604 + 27800) % 65535)); - Gfx.fill_rect(0, 160, SCREEN_W, CELL, new Color((ticks * 617 + 28500) % 65535)); - Gfx.fill_rect(0, 164, SCREEN_W, CELL, new Color((ticks * 630 + 29200) % 65535)); - Gfx.fill_rect(0, 168, SCREEN_W, CELL, new Color((ticks * 643 + 29900) % 65535)); - Gfx.fill_rect(0, 172, SCREEN_W, CELL, new Color((ticks * 656 + 30600) % 65535)); - Gfx.fill_rect(0, 176, SCREEN_W, CELL, new Color((ticks * 669 + 31300) % 65535)); + for y: int from 0 until SCREEN_H step CELL { + let gy = y / CELL; + for x: int from 0 until SCREEN_W step CELL { + let gx = x / CELL; + let color_raw = (gx * 257 + gy * 911 + ticks * 149) % 65535; + Gfx.fill_rect(x, y, CELL, CELL, new Color(color_raw)); + } + } }