From cab0787d8c259b21baa376ba1ec2d95740964bd9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 2 Feb 2025 19:06:21 +0000 Subject: Add Shid to Patio codegen for relational int operations --- .../shedlang/compiler/shidtopatio/ShidToPatio.java | 52 +++++++++- .../compiler/shidtopatio/ShidToPatioInts.java | 108 +++++++++++++++++++++ 2 files changed, 156 insertions(+), 4 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java index efadc62a..4ea228b2 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java @@ -479,16 +479,60 @@ public class ShidToPatio { ); case ShidIntGreaterThan intGreaterThan -> - throw new UnsupportedOperationException("TODO"); + generateComparison( + (destination, left, right, operationContext) -> + ShidToPatioInts.greaterThan( + destination, + left, + right, + intGreaterThan.source(), + operationContext + ), + intGreaterThan.source(), + context + ); case ShidIntGreaterThanOrEqual intGreaterThanOrEqual -> - throw new UnsupportedOperationException("TODO"); + generateComparison( + (destination, left, right, operationContext) -> + ShidToPatioInts.greaterThanOrEqual( + destination, + left, + right, + intGreaterThanOrEqual.source(), + operationContext + ), + intGreaterThanOrEqual.source(), + context + ); case ShidIntLessThan intLessThan -> - throw new UnsupportedOperationException("TODO"); + generateComparison( + (destination, left, right, operationContext) -> + ShidToPatioInts.lessThan( + destination, + left, + right, + intLessThan.source(), + operationContext + ), + intLessThan.source(), + context + ); case ShidIntLessThanOrEqual intLessThanOrEqual -> - throw new UnsupportedOperationException("TODO"); + generateComparison( + (destination, left, right, operationContext) -> + ShidToPatioInts.lessThanOrEqual( + destination, + left, + right, + intLessThanOrEqual.source(), + operationContext + ), + intLessThanOrEqual.source(), + context + ); case ShidIntNotEqual intNotEqual -> generateComparison( diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java index 68b9547c..3fe08470 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioInts.java @@ -65,6 +65,46 @@ public class ShidToPatioInts { Sources.RUNTIME ); + private static final PatioFunctionDeclaration INT_LESS_THAN_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_less_than", + List.of( + PatioLocal.param("left", ShidToPatioTypes.VALUE_TYPE), + PatioLocal.param("right", ShidToPatioTypes.VALUE_TYPE) + ), + PatioTypes.BOOL, + Sources.RUNTIME + ); + + private static final PatioFunctionDeclaration INT_LESS_THAN_OR_EQUAL_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_less_than_or_equal", + List.of( + PatioLocal.param("left", ShidToPatioTypes.VALUE_TYPE), + PatioLocal.param("right", ShidToPatioTypes.VALUE_TYPE) + ), + PatioTypes.BOOL, + Sources.RUNTIME + ); + + private static final PatioFunctionDeclaration INT_GREATER_THAN_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_greater_than", + List.of( + PatioLocal.param("left", ShidToPatioTypes.VALUE_TYPE), + PatioLocal.param("right", ShidToPatioTypes.VALUE_TYPE) + ), + PatioTypes.BOOL, + Sources.RUNTIME + ); + + private static final PatioFunctionDeclaration INT_GREATER_THAN_OR_EQUAL_DECLARATION = new PatioFunctionDeclaration( + "shed__runtime__int_greater_than_or_equal", + List.of( + PatioLocal.param("left", ShidToPatioTypes.VALUE_TYPE), + PatioLocal.param("right", ShidToPatioTypes.VALUE_TYPE) + ), + PatioTypes.BOOL, + Sources.RUNTIME + ); + public static final PatioType INT_TYPE = ShidToPatioTypes.VALUE_TYPE; private ShidToPatioInts() { @@ -153,4 +193,72 @@ public class ShidToPatioInts { source )); } + + public static ShidToPatioContext lessThan( + PatioLocalRef destination, + PatioOperand left, + PatioOperand right, + Source source, + ShidToPatioContext context + ) { + return context + .ensureCompilationUnitElement(INT_LESS_THAN_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(destination), + INT_LESS_THAN_DECLARATION.name(), + List.of(left, right), + source + )); + } + + public static ShidToPatioContext lessThanOrEqual( + PatioLocalRef destination, + PatioOperand left, + PatioOperand right, + Source source, + ShidToPatioContext context + ) { + return context + .ensureCompilationUnitElement(INT_LESS_THAN_OR_EQUAL_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(destination), + INT_LESS_THAN_OR_EQUAL_DECLARATION.name(), + List.of(left, right), + source + )); + } + + public static ShidToPatioContext greaterThan( + PatioLocalRef destination, + PatioOperand left, + PatioOperand right, + Source source, + ShidToPatioContext context + ) { + return context + .ensureCompilationUnitElement(INT_GREATER_THAN_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(destination), + INT_GREATER_THAN_DECLARATION.name(), + List.of(left, right), + source + )); + } + + public static ShidToPatioContext greaterThanOrEqual( + PatioLocalRef destination, + PatioOperand left, + PatioOperand right, + Source source, + ShidToPatioContext context + ) { + return context + .ensureCompilationUnitElement(INT_GREATER_THAN_OR_EQUAL_DECLARATION) + .addInstruction(new PatioCall( + Optional.of(destination), + INT_GREATER_THAN_OR_EQUAL_DECLARATION.name(), + List.of(left, right), + source + )); + } } -- cgit v1.2.3