From 10317b0c2841591a85530a5c4c38cac0c4bf5fe8 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 11 Jul 2026 16:43:48 +0100 Subject: Support enums in Java transient-0 --- .../javatransient0/JavaTransient0Generator.java | 42 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'src/main') 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 59dcefe..be04982 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 @@ -330,7 +330,35 @@ public class JavaTransient0Generator implements Generator { ) { return switch (statement) { case TypedEnumDefinitionNode enumDefinition -> { - yield Stream.of(); + var ordinal = JavaIdentifier.of("ordinal"); + yield Stream.of( + generateEncodeMethod( + enumDefinition.type(), + new JavaBlock(generateEncode( + new JavaMethodCall( + new JavaRef(VALUE_NAME), + ordinal, + List.of() + ), + NativeTypes.INT_32 + )) + ), + generateDecodeMethod( + enumDefinition.type(), + new JavaBlock(Stream.concat( + generateDecode(ordinal, NativeTypes.INT_32).stream(), + Stream.of(new JavaReturn(new JavaArrayAccess( + // TODO: avoid duplicating array each time + new JavaStaticMethodCall( + this.javaGenerator.generateTypeRef(enumDefinition.type()), + JavaIdentifier.of("values"), + List.of() + ), + new JavaRef(ordinal) + ))) + ).toList()) + ) + ); } case TypedNativeTypeDefinitionNode nativeTypeDefinition -> { @@ -441,7 +469,11 @@ public class JavaTransient0Generator implements Generator { } case EnumType enumType -> { - throw new UnsupportedOperationException("TODO"); + yield List.of(generateEncode( + value, + enumType.namespaceName(), + enumType + )); } case SimpleNativeType simpleNativeType -> { @@ -501,7 +533,11 @@ public class JavaTransient0Generator implements Generator { } case EnumType enumType -> { - throw new UnsupportedOperationException("TODO"); + yield List.of(generateDecode( + target, + enumType.namespaceName(), + enumType + )); } case SimpleNativeType simpleNativeType -> { -- cgit v1.2.3