summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hobgoblin/src/ast/untyped.hob2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java20
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java6
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java2
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java2
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java10
6 files changed, 17 insertions, 25 deletions
diff --git a/hobgoblin/src/ast/untyped.hob b/hobgoblin/src/ast/untyped.hob
index 0300e8a..10b501a 100644
--- a/hobgoblin/src/ast/untyped.hob
+++ b/hobgoblin/src/ast/untyped.hob
@@ -40,7 +40,7 @@ struct UntypedEnumVariantDefinitionNode {
struct UntypedNativeTypeDefinitionNode {
field name: String;
- field typeParams: List[UntypedTypeParamNode];
+ field typeParams: Option[List[UntypedTypeParamNode]];
field docComment: DocComment;
field source: Source;
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java
index 43d642e..c992a0d 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNode.java
@@ -7,17 +7,17 @@ package org.zwobble.hobgoblin.compiler.ast.untyped;
public record UntypedNativeTypeDefinitionNode(
String name,
- java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode> typeParams,
+ java.util.Optional<java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>> typeParams,
org.zwobble.hobgoblin.compiler.ast.DocComment docComment,
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode {
public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder("", java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder("", java.util.Optional.empty(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
}
public record Builder(
String name,
- java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode> typeParams,
+ java.util.Optional<java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>> typeParams,
org.zwobble.hobgoblin.compiler.ast.DocComment docComment,
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode.Builder {
@@ -29,20 +29,12 @@ public record UntypedNativeTypeDefinitionNode(
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
}
- public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withTypeParams(java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode> typeParams) {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
- }
-
- public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder addTypeParam(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode typeParam) {
- var typeParams = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>(this.typeParams);
- typeParams.add(typeParam);
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withTypeParams(java.util.Optional<java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>> typeParams) {
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
}
- public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder addTypeParam(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode.Builder typeParam) {
- var typeParams = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>(this.typeParams);
- typeParams.add(typeParam.build());
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, typeParams, docComment, source);
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withTypeParams(java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode> typeParams) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder(name, java.util.Optional.of(typeParams), docComment, source);
}
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
index 1e03672..e66d271 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
@@ -319,9 +319,9 @@ public class Parser {
);
}
- private static List<UntypedTypeParamNode> parseTypeParams(TokenIterator tokens) {
+ private static Optional<List<UntypedTypeParamNode>> parseTypeParams(TokenIterator tokens) {
if (!tokens.trySkip(TokenType.SYMBOL_SQUARE_OPEN)) {
- return List.of();
+ return Optional.empty();
}
var typeParams = parseMany(
@@ -341,7 +341,7 @@ public class Parser {
tokens.skip(TokenType.SYMBOL_SQUARE_CLOSE);
- return typeParams;
+ return Optional.of(typeParams);
}
static UntypedTypeLevelExpressionNode parseTypeLevelExpression(
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 e1ad8d2..3aac97f 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -169,7 +169,7 @@ public class TypeChecker {
var nativeTypeOrConstructor = untyped.typeParams().isEmpty()
? new TypeOrConstructor.Type<>(nativeType)
: new TypeOrConstructor.Constructor<>(new NativeTypeConstructor(
- untyped.typeParams().stream()
+ untyped.typeParams().get().stream()
.map(typeParam -> new TypeParam(typeParam.name()))
.toList(),
nativeType
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java
index e1f02a0..3105dae 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNativeTypeDefinitionNodeMatcher.java
@@ -36,7 +36,7 @@ public final class UntypedNativeTypeDefinitionNodeMatcher implements org.zwobble
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher(submatchers);
}
- public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher withTypeParams(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>> typeParams) {
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher withTypeParams(org.zwobble.precisely.Matcher<? super java.util.Optional<java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNode>>> typeParams) {
var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode>>(this.submatchers);
submatchers.add(org.zwobble.precisely.Matchers.has("typeParams", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode::typeParams, typeParams));
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNodeMatcher(submatchers);
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java
index ca0a71d..444b392 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNativeTypeDefinitionTests.java
@@ -39,7 +39,7 @@ public class ParserNativeTypeDefinitionTests {
);
assertThat(node, isUntypedNativeTypeDefinitionNode()
- .withTypeParams(isSequence())
+ .withTypeParams(isOptionalEmpty())
);
}
@@ -55,9 +55,9 @@ public class ParserNativeTypeDefinitionTests {
);
assertThat(node, isUntypedNativeTypeDefinitionNode()
- .withTypeParams(isSequence(
+ .withTypeParams(isOptionalOf(isSequence(
isUntypedTypeParamNode().withName(equalTo("TPoint"))
- ))
+ )))
);
}
@@ -73,10 +73,10 @@ public class ParserNativeTypeDefinitionTests {
);
assertThat(node, isUntypedNativeTypeDefinitionNode()
- .withTypeParams(isSequence(
+ .withTypeParams(isOptionalOf(isSequence(
isUntypedTypeParamNode().withName(equalTo("K")),
isUntypedTypeParamNode().withName(equalTo("V"))
- ))
+ )))
);
}
}