diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-05-06 23:43:58 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-05-06 23:43:58 +0100 |
| commit | 73afea9ca774a63e8149ca02fe48860e2b06e365 (patch) | |
| tree | fe99dccacfab2e9e2ebad26e4ac9cef502b1940b /src | |
| parent | 2e958ec140e0ac146c8462eba6483320dbff8dd8 (diff) | |
Add ConstructedNativeType
Diffstat (limited to 'src')
12 files changed, 69 insertions, 15 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java b/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java index 7a2f181..edbc3fe 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java @@ -1,7 +1,7 @@ package org.zwobble.hobgoblin.compiler.builtins; -import org.zwobble.hobgoblin.compiler.types.NativeType; +import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; public class NativeTypes { - public static NativeType INT_32 = new NativeType("Int32"); + public static SimpleNativeType INT_32 = new SimpleNativeType("Int32"); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java index 2f5c620..d5af6d9 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java @@ -132,7 +132,10 @@ public class JavaTypesGenerator implements Generator { private JavaTypeRef generateTypeRef(Type type) { return switch (type) { - case NativeType nativeType -> { + case ConstructedNativeType constructedNativeType -> + throw new UnsupportedOperationException("TODO"); + + case SimpleNativeType simpleNativeType -> { if (type.equals(NativeTypes.INT_32)) { yield new JavaTypeRef(List.of(), "int"); } else { @@ -148,6 +151,9 @@ public class JavaTypesGenerator implements Generator { case TypeLevelValueType typeLevelValueType -> throw new UnsupportedOperationException("TODO"); + + case TypeParam typeParam -> + throw new UnsupportedOperationException("TODO"); }; } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java index e98afc0..4692127 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java @@ -1,7 +1,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.zwobble.hobgoblin.compiler.types.NamespaceName; -import org.zwobble.hobgoblin.compiler.types.NativeType; +import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; import org.zwobble.hobgoblin.compiler.types.Type; import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; @@ -18,7 +18,7 @@ public class TypeCheckerGlobalContext { private TypeCheckerGlobalContext() { } - public void addNativeType(NativeType type) { + public void addNativeType(SimpleNativeType type) { this.nativeTypes.put(type.name(), new TypeLevelValueType(type)); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstructedNativeType.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstructedNativeType.java new file mode 100644 index 0000000..0f8051a --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstructedNativeType.java @@ -0,0 +1,22 @@ +package org.zwobble.hobgoblin.compiler.types; + +import java.util.List; +import java.util.stream.Collectors; + +public record ConstructedNativeType( + TypeConstructor<SimpleNativeType> constructor, + List<Type> args +) implements Type { + @Override + public String name() { + return constructor.genericType().name(); + } + + @Override + public String describe() { + var argsString = args().stream() + .map(arg -> arg.describe()) + .collect(Collectors.joining(", ")); + return constructor.describe() + "[" + argsString + "]"; + } +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstructibleType.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstructibleType.java new file mode 100644 index 0000000..50b298d --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstructibleType.java @@ -0,0 +1,4 @@ +package org.zwobble.hobgoblin.compiler.types; + +public sealed interface ConstructibleType extends Type permits SimpleNativeType { +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/SimpleNativeType.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/SimpleNativeType.java new file mode 100644 index 0000000..0d73fc9 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/SimpleNativeType.java @@ -0,0 +1,8 @@ +package org.zwobble.hobgoblin.compiler.types; + +public record SimpleNativeType(String name) implements Type, ConstructibleType { + @Override + public String describe() { + return name; + } +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/Type.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/Type.java index eeccb8b..4af272f 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/types/Type.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/Type.java @@ -1,5 +1,5 @@ package org.zwobble.hobgoblin.compiler.types; -public sealed interface Type extends TypeLevelValue permits NativeType, StructType, SumType, TypeLevelValueType { +public sealed interface Type extends TypeLevelValue permits ConstructedNativeType, ConstructibleType, SimpleNativeType, StructType, SumType, TypeLevelValueType, TypeParam { String name(); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeConstructor.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeConstructor.java new file mode 100644 index 0000000..1e412d7 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeConstructor.java @@ -0,0 +1,13 @@ +package org.zwobble.hobgoblin.compiler.types; + +import java.util.List; + +public record TypeConstructor<T extends ConstructibleType>( + List<TypeParam> params, + T genericType +) implements TypeLevelValue { + @Override + public String describe() { + return genericType.describe(); + } +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java index 9740cf9..cf62eb1 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java @@ -1,5 +1,5 @@ package org.zwobble.hobgoblin.compiler.types; -public sealed interface TypeLevelValue permits Type { +public sealed interface TypeLevelValue permits Type, TypeConstructor { String describe(); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/NativeType.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeParam.java index 2a45a1f..479cd3d 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/types/NativeType.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeParam.java @@ -1,6 +1,7 @@ package org.zwobble.hobgoblin.compiler.types; -public record NativeType(String name) implements Type { +public record TypeParam(String name) implements Type { + // TODO: scope? @Override public String describe() { return name; diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java index 2409b65..6cd6732 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -11,7 +11,7 @@ import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode; import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.NamespaceName; -import org.zwobble.hobgoblin.compiler.types.NativeType; +import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; import org.zwobble.hobgoblin.compiler.types.StructType; import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; @@ -45,8 +45,8 @@ public class TypeCheckerStructDefinitionTests { @Test public void fieldsAreTypeChecked() { - var int32Type = new NativeType("Int32"); - var int64Type = new NativeType("Int64"); + var int32Type = new SimpleNativeType("Int32"); + var int64Type = new SimpleNativeType("Int64"); var untyped = new UntypedStructDefinitionNode( "X", List.of( @@ -92,7 +92,7 @@ public class TypeCheckerStructDefinitionTests { @Test public void structCanUseTypeDefinedLater() { - var int32Type = new NativeType("Int32"); + var int32Type = new SimpleNativeType("Int32"); var namespaceName = NamespaceName.of("a", "b"); var untyped = new UntypedNamespaceNode( namespaceName, diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java index b2dcc62..f0d3a81 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java @@ -3,7 +3,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.typed.TypedTypeLevelReferenceNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedArb; -import org.zwobble.hobgoblin.compiler.types.NativeType; +import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; import org.zwobble.hobgoblin.compiler.types.TypeSet; @@ -44,7 +44,7 @@ public class TypeCheckerTypeLevelReferenceTests { var untyped = UntypedArb.typeLevelReference("X"); var context = TypeCheckerContextArb.namespaceContext(); context.declare("X"); - var nativeType = new NativeType("Int"); + var nativeType = new SimpleNativeType("Int"); context.define("X", nativeType); var error = assertThrows( @@ -61,7 +61,7 @@ public class TypeCheckerTypeLevelReferenceTests { var untyped = UntypedArb.typeLevelReference("X"); var context = TypeCheckerContextArb.namespaceContext(); context.declare("X"); - var nativeType = new NativeType("Int"); + var nativeType = new SimpleNativeType("Int"); context.define("X", new TypeLevelValueType(nativeType)); var typed = TypeChecker.typeCheckTypeLevelExpression(untyped, context); |
