summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-05-20 22:57:14 +0100
committerMichael Williamson <mike@zwobble.org>2026-05-20 22:57:47 +0100
commite99d998eb4543a1ebd3d1fd10e1f7f86e71bb264 (patch)
tree74535c5a482fa96caa9b7e7528fbd8b2fc87714b /src/main/java/org/zwobble
parent2337ff8b31d264e86575682e45f78f568ed90785 (diff)
Type check native type definitions
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNamespaceStatementNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java15
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java27
4 files changed, 45 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNamespaceStatementNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNamespaceStatementNode.java
index 6cab80c..7e96716 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNamespaceStatementNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNamespaceStatementNode.java
@@ -1,4 +1,4 @@
package org.zwobble.hobgoblin.compiler.ast.typed;
-public sealed interface TypedNamespaceStatementNode extends TypedNode permits TypedStructDefinitionNode, TypedSumDefinitionNode {
+public sealed interface TypedNamespaceStatementNode extends TypedNode permits TypedNativeTypeDefinitionNode, TypedStructDefinitionNode, TypedSumDefinitionNode {
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java
new file mode 100644
index 0000000..e81705b
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNativeTypeDefinitionNode.java
@@ -0,0 +1,15 @@
+package org.zwobble.hobgoblin.compiler.ast.typed;
+
+import org.zwobble.hobgoblin.compiler.ast.DocComment;
+import org.zwobble.hobgoblin.compiler.sources.Source;
+import org.zwobble.hobgoblin.compiler.types.SimpleNativeType;
+
+public record TypedNativeTypeDefinitionNode(
+ SimpleNativeType type,
+ DocComment docComment,
+ Source source
+) implements TypedNamespaceStatementNode {
+ public String name() {
+ return this.type.name();
+ }
+}
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 e859170..5d4ffb6 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
@@ -69,6 +69,10 @@ public class JavaTypesGenerator implements Generator {
for (var statement : namespace.body()) {
switch (statement) {
+ case TypedNativeTypeDefinitionNode nativeTypeDefinition -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+
case TypedStructDefinitionNode structDefinition -> {
var javaCompilationUnit = new JavaCompilationUnit(
packageParts,
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
index 29bcbf8..2579fbe 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -41,7 +41,7 @@ public class TypeChecker {
) {
switch (untyped) {
case UntypedNativeTypeDefinitionNode untypedNativeTypeDefinition ->
- throw new UnsupportedOperationException("TODO");
+ declareNativeTypeDefinition(untypedNativeTypeDefinition, context);
case UntypedStructDefinitionNode untypedStructDefinition ->
declareStructDefinition(untypedStructDefinition, context);
@@ -57,7 +57,7 @@ public class TypeChecker {
) {
return switch (untyped) {
case UntypedNativeTypeDefinitionNode untypedNativeTypeDefinition ->
- throw new UnsupportedOperationException("TODO");
+ defineNativeTypeDefinition(untypedNativeTypeDefinition, context);
case UntypedStructDefinitionNode untypedStructDefinition ->
defineStructDefinition(untypedStructDefinition, context);
@@ -67,6 +67,29 @@ public class TypeChecker {
};
}
+ private static void declareNativeTypeDefinition(
+ UntypedNativeTypeDefinitionNode untyped,
+ TypeCheckerNamespaceContext context
+ ) {
+ var nativeType = new SimpleNativeType(context.namespaceName(), untyped.name());
+ // TODO: tidy up declare vs define
+ context.declare(untyped.name());
+ context.define(untyped.name(), new TypeLevelValueType(nativeType));
+ }
+
+ private static TypedNativeTypeDefinitionNode defineNativeTypeDefinition(
+ UntypedNativeTypeDefinitionNode untyped,
+ TypeCheckerNamespaceContext context
+ ) {
+ var nativeType = (SimpleNativeType) lookupMetaType(untyped.name(), untyped.source(), context);
+
+ return new TypedNativeTypeDefinitionNode(
+ nativeType,
+ untyped.docComment(),
+ untyped.source()
+ );
+ }
+
private static void declareStructDefinition(
UntypedStructDefinitionNode untyped,
TypeCheckerNamespaceContext context