diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-08-27 21:56:25 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-08-27 21:56:25 +0100 |
| commit | d408f4f80670ed7f73d09f8ccfefdcb8a7835406 (patch) | |
| tree | d298ac2139b3ee98606fcb6164ef306c16605b8f | |
| parent | 7afd4bc26233bae7bc087b778c4378dbd8f6ff73 (diff) | |
Add Patio code gen for loop instructions
7 files changed, 136 insertions, 6 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopBreakTests]/[method:canGenerateCodeForLoopBreak(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopBreakTests]/[method:canGenerateCodeForLoopBreak(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..fda23449 --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopBreakTests]/[method:canGenerateCodeForLoopBreak(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,12 @@ +Compilation unit elements: + + +Locals: + +Instructions: + +( + (break) +) + +Temporary stack: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopContinueTests]/[method:canGenerateCodeForLoopContinue(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopContinueTests]/[method:canGenerateCodeForLoopContinue(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..4409f1f5 --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopContinueTests]/[method:canGenerateCodeForLoopContinue(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,12 @@ +Compilation unit elements: + + +Locals: + +Instructions: + +( + (continue) +) + +Temporary stack: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopTests]/[method:canGenerateCodeForLoop(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopTests]/[method:canGenerateCodeForLoop(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..b48157df --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenLoopTests]/[method:canGenerateCodeForLoop(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,18 @@ +Compilation unit elements: + + +Locals: + * (val "tmp__0" int) + +Instructions: + +( + ( + loop + ( + (local-store (local "tmp__0") (int 1)) + ) + ) +) + +Temporary stack: diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 0fa95c91..948bfd6a 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -346,14 +346,24 @@ public class PatioCodeGen { (local, c) -> c.temporaryPopIntoLocal(local, localStore.source()) ); - case LoopBreakInstruction loopBreakInstruction -> - throw new UnsupportedOperationException("TODO"); + case LoopBreakInstruction loopBreak -> + context.addInstruction(new PatioLoopBreak(loopBreak.source())); - case LoopContinueInstruction loopContinueInstruction -> - throw new UnsupportedOperationException("TODO"); + case LoopContinueInstruction loopContinue -> + context.addInstruction(new PatioLoopContinue(loopContinue.source())); - case LoopInstruction loopInstruction -> - throw new UnsupportedOperationException("TODO"); + case LoopInstruction loop -> { + context = context.enterBlock(); + context = generateInstructions(loop.body(), context); + + var bodyInstructionsResult = context.exitBlock(); + var bodyInstructions = bodyInstructionsResult.value(); + context = bodyInstructionsResult.context(); + + context = context.addInstruction(new PatioLoop(bodyInstructions, loop.source())); + + yield context; + } case NamespaceFieldStoreInstruction namespaceFieldStore -> { var fieldValueResult = context.temporaryPop(); diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopBreakTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopBreakTests.java new file mode 100644 index 00000000..76a3799c --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopBreakTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class PatioCodeGenLoopBreakTests { + @Test + public void canGenerateCodeForLoopBreak(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.loopBreak(); + var context = PatioCodeGenContext.EMPTY; + + var result = PatioCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopContinueTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopContinueTests.java new file mode 100644 index 00000000..281f6dcd --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopContinueTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class PatioCodeGenLoopContinueTests { + @Test + public void canGenerateCodeForLoopContinue(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.loopContinue(); + var context = PatioCodeGenContext.EMPTY; + + var result = PatioCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java new file mode 100644 index 00000000..ff74a17b --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java @@ -0,0 +1,30 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class PatioCodeGenLoopTests { + @Test + public void canGenerateCodeForLoop(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.loop( + List.of( + Instructions.pushInt(1), + Instructions.discard() + ) + ); + var context = PatioCodeGenContext.EMPTY; + + var result = PatioCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} |
