summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/types/Field.java4
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java11
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java15
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java5
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/types/TypeMatchers.java10
7 files changed, 37 insertions, 14 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java
index 9223ec5..4869d3c 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java
@@ -15,6 +15,8 @@ public sealed interface UntypedNamespaceStatementNode permits org.zwobble.hobgob
public String name();
+ public org.zwobble.hobgoblin.compiler.sources.Source source();
+
// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode body
// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode body
}
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 408a0b2..f1b5067 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -47,7 +47,7 @@ public class TypeChecker {
var fields = new ArrayList<Field>();
for (var untypedStatement : untyped.body()) {
var type = declareNamespaceStatement(untypedStatement, namespaceContext);
- var field = new Field(untypedStatement.name(), type);
+ var field = new Field(untypedStatement.name(), type, untypedStatement.source());
fields.add(field);
}
context.defineNamespace(untyped.namespaceName(), fields);
@@ -223,7 +223,7 @@ public class TypeChecker {
untypedField.source()
);
typedFields.add(typedField);
- fields.add(new Field(untypedField.name(), fieldType.value()));
+ fields.add(new Field(untypedField.name(), fieldType.value(), untypedField.source()));
}
return new TypeCheckFieldDefinitionsResult(typedFields, fields);
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/Field.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/Field.java
index e806c11..3d1f909 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/types/Field.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/Field.java
@@ -1,4 +1,6 @@
package org.zwobble.hobgoblin.compiler.types;
-public record Field(String name, Type type) {
+import org.zwobble.hobgoblin.compiler.sources.Source;
+
+public record Field(String name, Type type, Source source) {
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java
index 82cccf3..1c19daf 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceTests.java
@@ -4,12 +4,14 @@ import org.junit.jupiter.api.Test;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceNode;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode;
import org.zwobble.hobgoblin.compiler.ast.untyped.*;
+import org.zwobble.hobgoblin.compiler.sources.NullSource;
import org.zwobble.hobgoblin.compiler.types.*;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.zwobble.hobgoblin.compiler.types.TypeMatchers.isField;
import static org.zwobble.hobgoblin.compiler.types.TypeMatchers.isMetaType;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
@@ -73,8 +75,11 @@ public class TypeCheckerNamespaceTests {
.build();
var context = TypeCheckerContextArb.globalContext();
context.defineNamespace(NamespaceName.of("a", "c"), List.of(
- new Field("Y", new TypeLevelValueType(new SimpleNativeType(NamespaceName.of("a", "c"), "Y"))
- )));
+ new Field(
+ "Y",
+ new TypeLevelValueType(new SimpleNativeType(NamespaceName.of("a", "c"), "Y")),
+ NullSource.INSTANCE
+ )));
var typed = TypeChecker.typeCheckNamespace(untyped, context);
@@ -94,7 +99,7 @@ public class TypeCheckerNamespaceTests {
assertThat(
context.toTypesInfo().fieldsOf(new StructType(NamespaceName.of("a", "b"), "X")),
isSequence(
- equalTo(new Field("y", new SimpleNativeType(NamespaceName.of("a", "c"), "Y")))
+ isField("y", new SimpleNativeType(NamespaceName.of("a", "c"), "Y"))
)
);
}
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 81bf9b8..0efe3b4 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java
@@ -5,13 +5,18 @@ import org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceNode;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructFieldDefinitionNode;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedTypeLevelExpressionNode;
-import org.zwobble.hobgoblin.compiler.ast.untyped.*;
-import org.zwobble.hobgoblin.compiler.types.*;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedArb;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceNode;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode;
+import org.zwobble.hobgoblin.compiler.types.NamespaceName;
+import org.zwobble.hobgoblin.compiler.types.SimpleNativeType;
+import org.zwobble.hobgoblin.compiler.types.StructType;
import java.util.List;
-import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement;
+import static org.zwobble.hobgoblin.compiler.types.TypeMatchers.isField;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
@@ -83,8 +88,8 @@ public class TypeCheckerStructDefinitionTests {
assertThat(
typesInfo.fieldsOf(new StructType(namespaceName, "X")),
isSequence(
- equalTo(new Field("a", int32Type)),
- equalTo(new Field("b", int64Type))
+ isField("a", int32Type),
+ isField("b", int64Type)
)
);
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java
index c19ee7f..597fb42 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java
@@ -6,6 +6,7 @@ import org.zwobble.hobgoblin.compiler.ast.typed.TypedSumDefinitionNode;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedSumVariantDefinitionNode;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedTypeLevelExpressionNode;
import org.zwobble.hobgoblin.compiler.ast.untyped.*;
+import org.zwobble.hobgoblin.compiler.sources.NullSource;
import org.zwobble.hobgoblin.compiler.types.*;
import java.util.List;
@@ -193,7 +194,7 @@ public class TypeCheckerSumDefinitionTests {
.enterNamespace(namespaceName);
var variantType = new StructType(namespaceName, "Square");
namespaceContext.declare("Square", new TypeLevelValueType(variantType));
- namespaceContext.defineStructType(variantType, List.of(new Field("area", int64Type)));
+ namespaceContext.defineStructType(variantType, List.of(new Field("area", int64Type, NullSource.INSTANCE)));
// TODO: more specific error
var error = assertThrows(
@@ -254,7 +255,7 @@ public class TypeCheckerSumDefinitionTests {
namespaceContext.defineSumType(
sumType,
List.of(variantType),
- List.of(new Field("area", int32Type))
+ List.of(new Field("area", int32Type, NullSource.INSTANCE))
);
assertThrows(
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/types/TypeMatchers.java b/src/test/java/org/zwobble/hobgoblin/compiler/types/TypeMatchers.java
index d676d89..42d5d4e 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/types/TypeMatchers.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/types/TypeMatchers.java
@@ -2,7 +2,8 @@ package org.zwobble.hobgoblin.compiler.types;
import org.zwobble.precisely.Matcher;
-import static org.zwobble.precisely.Matchers.equalTo;
+import static org.zwobble.precisely.Matchers.*;
+import static org.zwobble.precisely.Matchers.has;
public class TypeMatchers {
private TypeMatchers() {}
@@ -10,4 +11,11 @@ public class TypeMatchers {
public static Matcher<Type> isMetaType(Type type) {
return equalTo(new TypeLevelValueType(type));
}
+
+ public static Matcher<Field> isField(String name, Type type) {
+ return allOf(
+ has("name", Field::name, equalTo(name)),
+ has("type", Field::type, equalTo(type))
+ );
+ }
}