diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-28 13:52:34 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-28 13:52:34 +0100 |
| commit | af0f173fcc0bf84316e188a0e6d4da4dfe11b021 (patch) | |
| tree | b9b24c3422ee113913fed383d99f372cd1560bfd /src/main/java | |
| parent | 3ce11f3a242c2287a14d5f5a4ae230b4c79beaaa (diff) | |
Throw appropriate error if enum variant is not recognised
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java index 3a7f0ff..4197a24 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java @@ -653,11 +653,30 @@ public class JavaTransient0Generator implements Generator { enumDefinition.type(), new JavaBlock(Stream.concat( generateDecode(ordinal, NativeTypes.INT_32, context).stream(), - // TODO: handle ordinal not in array - Stream.of(new JavaReturn(new JavaArrayAccess( - new JavaRef(enumValues), - new JavaRef(ordinal) - ))) + Stream.of(new JavaIfStatement( + new JavaBinaryOperation( + JavaBinaryOperator.LOGICAL_AND, + new JavaBinaryOperation( + JavaBinaryOperator.GREATER_THAN_OR_EQUAL, + new JavaRef(ordinal), + new JavaIntegerLiteral(0) + ), + new JavaBinaryOperation( + JavaBinaryOperator.LESS_THAN, + new JavaRef(ordinal), + new JavaIntegerLiteral(enumDefinition.variants().size()) + ) + ), + new JavaBlock(List.of( + new JavaReturn(new JavaArrayAccess( + new JavaRef(enumValues), + new JavaRef(ordinal) + )) + )), + new JavaBlock(List.of( + throwUnrecognisedVariantException(enumDefinition.type(), new JavaRef(ordinal)) + )) + )) ).toList()) ) ); @@ -804,11 +823,7 @@ public class JavaTransient0Generator implements Generator { cases.add(new JavaSwitchRule( new JavaCaseDefault(), new JavaBlock(List.of( - throwIoException(new JavaBinaryOperation( - JavaBinaryOperator.ADD, - new JavaStringLiteral("unrecognised variant tag for " + sumDefinition.type().describe() + ": "), - new JavaRef(variantTag) - )) + throwUnrecognisedVariantException(sumDefinition.type(), new JavaRef(variantTag)) )) )); @@ -1631,6 +1646,17 @@ public class JavaTransient0Generator implements Generator { ); } + private static JavaThrowStatement throwUnrecognisedVariantException( + Type type, + JavaExpression tag + ) { + return throwIoException(new JavaBinaryOperation( + JavaBinaryOperator.ADD, + new JavaStringLiteral("unrecognised variant tag for " + type.describe() + ": "), + tag + )); + } + private static JavaThrowStatement throwIoException(String errorMessage) { return throwIoException(new JavaStringLiteral(errorMessage)); } |
