aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-02-02 18:54:58 +0000
committerMichael Williamson <mike@zwobble.org>2025-02-02 18:54:58 +0000
commit72f814103f94f65f02c67d1ee0bd0f76768bddad (patch)
tree02c63f9d1ed6fcac977ccfa2be5f4452d4789b2e /src
parent037fd3fee47b0fd94e8f7a3cff25ab7578a6e5da (diff)
Add Shid int relational instructions
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidSerializer.java12
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThan.java15
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThanOrEqual.java15
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThan.java15
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThanOrEqual.java15
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java12
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java36
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/Shid.java16
9 files changed, 137 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
index 590c3d39..b5be44ce 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
@@ -173,6 +173,18 @@ public class ShidSerializer {
case ShidIntEquals intEquals ->
treeList(treeIdentifier("int-eq"));
+ case ShidIntGreaterThan intGreaterThan ->
+ treeList(treeIdentifier("int-gt"));
+
+ case ShidIntGreaterThanOrEqual intGreaterThanOrEqual ->
+ treeList(treeIdentifier("int-gte"));
+
+ case ShidIntLessThan intLessThan ->
+ treeList(treeIdentifier("int-lt"));
+
+ case ShidIntLessThanOrEqual intLessThanOrEqual ->
+ treeList(treeIdentifier("int-lte"));
+
case ShidIntNotEqual intNotEqual ->
treeList(treeIdentifier("int-ne"));
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
index 963b9b80..c04e3687 100644
--- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
@@ -3,7 +3,7 @@ package org.shedlang.compiler.shid.instructions;
import org.shedlang.compiler.sources.Source;
public sealed interface ShidInstruction
- permits ShidBlock, ShidCall, ShidDeclareNativeFunction, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntNotEqual, ShidIntSub, ShidLocalDrop, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidMethodLoad, ShidMethodStore, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore {
+ permits ShidBlock, ShidCall, ShidDeclareNativeFunction, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntGreaterThan, ShidIntGreaterThanOrEqual, ShidIntLessThan, ShidIntLessThanOrEqual, ShidIntNotEqual, ShidIntSub, ShidLocalDrop, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidMethodLoad, ShidMethodStore, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore {
/**
* If an instruction is a terminator, execution is guaranteed to leave the
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThan.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThan.java
new file mode 100644
index 00000000..9d33319f
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThan.java
@@ -0,0 +1,15 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Pop two values from the temporary value stack, compare them as Ints, and push
+ * a Bool onto the temporary value stack: true if the first is greater than the
+ * second, false otherwise.
+ */
+public record ShidIntGreaterThan(Source source) implements ShidInstruction {
+ @Override
+ public boolean isBlockTerminator() {
+ return false;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThanOrEqual.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThanOrEqual.java
new file mode 100644
index 00000000..d0517181
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntGreaterThanOrEqual.java
@@ -0,0 +1,15 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Pop two values from the temporary value stack, compare them as Ints, and push
+ * a Bool onto the temporary value stack: true if the first is greater than or
+ * equal to the second, false otherwise.
+ */
+public record ShidIntGreaterThanOrEqual(Source source) implements ShidInstruction {
+ @Override
+ public boolean isBlockTerminator() {
+ return false;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThan.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThan.java
new file mode 100644
index 00000000..b16e032a
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThan.java
@@ -0,0 +1,15 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Pop two values from the temporary value stack, compare them as Ints, and push
+ * a Bool onto the temporary value stack: true if the first is less than the
+ * second, false otherwise.
+ */
+public record ShidIntLessThan(Source source) implements ShidInstruction {
+ @Override
+ public boolean isBlockTerminator() {
+ return false;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThanOrEqual.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThanOrEqual.java
new file mode 100644
index 00000000..1d8a16a3
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidIntLessThanOrEqual.java
@@ -0,0 +1,15 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Pop two values from the temporary value stack, compare them as Ints, and push
+ * a Bool onto the temporary value stack: true if the first is less than or
+ * equal to the second, false otherwise.
+ */
+public record ShidIntLessThanOrEqual(Source source) implements ShidInstruction {
+ @Override
+ public boolean isBlockTerminator() {
+ return false;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
index b039789a..efadc62a 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
@@ -478,6 +478,18 @@ public class ShidToPatio {
context
);
+ case ShidIntGreaterThan intGreaterThan ->
+ throw new UnsupportedOperationException("TODO");
+
+ case ShidIntGreaterThanOrEqual intGreaterThanOrEqual ->
+ throw new UnsupportedOperationException("TODO");
+
+ case ShidIntLessThan intLessThan ->
+ throw new UnsupportedOperationException("TODO");
+
+ case ShidIntLessThanOrEqual intLessThanOrEqual ->
+ throw new UnsupportedOperationException("TODO");
+
case ShidIntNotEqual intNotEqual ->
generateComparison(
(destination, left, right, operationContext) ->
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java
index 115cd597..55b828b4 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java
@@ -259,6 +259,42 @@ public class ShidSerializerTests {
}
@Test
+ public void canSerializeIntGreaterThan() {
+ var instruction = Shid.intGreaterThan();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-gt)"));
+ }
+
+ @Test
+ public void canSerializeIntGreaterThanOrEqual() {
+ var instruction = Shid.intGreaterThanOrEqual();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-gte)"));
+ }
+
+ @Test
+ public void canSerializeIntLessThan() {
+ var instruction = Shid.intLessThan();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-lt)"));
+ }
+
+ @Test
+ public void canSerializeIntLessThanOrEqual() {
+ var instruction = Shid.intLessThanOrEqual();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-lte)"));
+ }
+
+ @Test
public void canSerializeIntNotEqual() {
var instruction = Shid.intNotEqual();
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
index 49e0c0a1..761769ad 100644
--- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
@@ -95,6 +95,22 @@ public class Shid {
return new ShidIntEquals(Sources.NULL);
}
+ public static ShidInstruction intGreaterThan() {
+ return new ShidIntGreaterThan(Sources.NULL);
+ }
+
+ public static ShidInstruction intGreaterThanOrEqual() {
+ return new ShidIntGreaterThanOrEqual(Sources.NULL);
+ }
+
+ public static ShidInstruction intLessThan() {
+ return new ShidIntLessThan(Sources.NULL);
+ }
+
+ public static ShidInstruction intLessThanOrEqual() {
+ return new ShidIntLessThanOrEqual(Sources.NULL);
+ }
+
public static ShidInstruction intNotEqual() {
return new ShidIntNotEqual(Sources.NULL);
}