From 456b332d74e5fbbafd0fb5cf8d8dea2a10aa2e11 Mon Sep 17 00:00:00 2001 From: bQUARKz Date: Fri, 27 Mar 2026 22:35:09 +0000 Subject: [PATCH] added fragments --- .../lowering/PbsExecutableBodyLowerer.java | 9 +++ .../compiler/pbs/PbsFrontendCompilerTest.java | 31 ++++++++++ test-projects/fragments/cartridge/program.pbx | Bin 13955 -> 3667 bytes test-projects/fragments/src/main.pbs | 55 +++--------------- 4 files changed, 49 insertions(+), 46 deletions(-) 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 ba33b8596254c2a5159ca41b5adc971843b783f2..6be3f382050f05cbaf9a96bebadf7f0719c54710 100644 GIT binary patch literal 3667 zcma)GU8Gf7d>Hb$R&zCGg7K=TA_bFnsgib)$gZ*Rq-}2rK^ceKrB**z zL&lWb+_sFFcZR`*agFnlx;T0e!})7b7ob zme~jwT9na9L{bbVD-+D5MzWxwhkS&*P%Id?8;{H@Z+mieZ;l6J%-h-Dx|GcJR;3d5 z>E`tr2V=gu-@M&xw`wbI2LJCK6Sl>Kt&!06=hb8dWM!9_?Oa{z!>r^H1pD0>$5LLB zJ9QNpkGYj5Fl@j%G@bBZ|H85dC3&n}8Isde(htOh{|?Zch6ls{ zDp%mAph4tIx*It%J&e$Dg z#=L}b%A4@JkgvQLzYodpzLY=4LG>Qmq}+x-i8d>*!GDalD91PwOVCc`QhXWOtvm!j z6zx+UjvtKw72u zP1LJg!w*FQT~n^bKZyF2=i?V3`SF$d3-J#ld1r(d(OBh5dwz!`C8t$0S~dUyPl_e+=EPe31{im9Tv0N&SD=Th~#w z@(sNFu+}NZ@Fi%Gaw)zHEma#~KZnJStx4rj3v8hTPkbFSfuEa=&G%62?O=9L}S}PlJ>3W|PwjZCxjMS{z zv)D>@+>+Vo*KbTUWPM6YMfp%IW;bSA8kpk1%v^R|ntd{`ftLJ+dOtlPTDFjGEl7*5 z)l4>*OZiM>6qzFFswG`WwKV(Lf*nAKc`DOdYV6=W!^^~XDLF%f92tEmcen`+-?OWr srER{9-?Ix9&Xoy-a;w`+fF)ep;z@dq1CNujjko-@Ep^*Is+A-`eNwyxcn@ zMe)?*j~_Bvm4}X@Ow^1O^-4!kF?`WQQIu0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/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)); + } + } }