diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-11-29 23:26:27 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-11-29 23:26:27 +0000 |
| commit | f727123a92878a96bfb6a357207c1a6c4ec902d3 (patch) | |
| tree | a38de3de7419b4fca95cf8103d907ece069e6f43 | |
| parent | 05a28864e45137cef640902d5a0c65fa2b53dc13 (diff) | |
Declare variables up-front
23 files changed, 395 insertions, 128 deletions
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 3af3abb..5029d1b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -8,10 +8,7 @@ import org.zwobble.hobgoblin.compiler.typechecker.errors.*; import org.zwobble.hobgoblin.compiler.typechecker.errors.NoSuchFieldError; import org.zwobble.hobgoblin.compiler.types.*; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; import static org.zwobble.hobgoblin.compiler.typechecker.NamespaceNameResolution.resolveNamespaceName; @@ -31,7 +28,21 @@ public class TypeChecker { var namespaceContexts = untypedNamespaces.stream() .collect(Collectors.toMap( untypedNamespace -> untypedNamespace.namespaceName(), - untypedNamespace -> context.enterNamespace(untypedNamespace.namespaceName()) + untypedNamespace -> { + var namespaceContext = context.enterNamespace(untypedNamespace.namespaceName()); + + for (var import_ : untypedNamespace.imports()) { + for (var importedName : import_.importedNames()) { + namespaceContext.declare(importedName, import_.source()); + } + } + + for (var statement : untypedNamespace.body()) { + namespaceContext.declare(statement.name(), statement.source()); + } + + return namespaceContext.endDeclarations(); + } )); for (var untypedNamespace : untypedNamespaces) { @@ -72,7 +83,7 @@ public class TypeChecker { importedNamespace, untypedImport.source() )); - namespaceContext.declare(importedName, fieldType, untypedImport.source()); + namespaceContext.defineVariable(importedName, fieldType, untypedImport.source()); } } @@ -154,7 +165,7 @@ public class TypeChecker { var databaseSchema = new DatabaseSchema(context.namespaceName(), untyped.name()); var type = Types.databaseSchema(databaseSchema); - context.declare(untyped.name(), type, untyped.source()); + context.defineVariable(untyped.name(), type, untyped.source()); return type; } @@ -191,7 +202,7 @@ public class TypeChecker { ) { var nativeType = new EnumType(context.namespaceName(), untyped.name()); var metaType = new TypeLevelValueType(nativeType); - context.declare(untyped.name(), metaType, untyped.source()); + context.defineVariable(untyped.name(), metaType, untyped.source()); return metaType; } @@ -238,7 +249,7 @@ public class TypeChecker { )); var metaType = new TypeLevelValueType(nativeTypeOrConstructor.value()); - context.declare(untyped.name(), metaType, untyped.source()); + context.defineVariable(untyped.name(), metaType, untyped.source()); return metaType; } @@ -281,7 +292,7 @@ public class TypeChecker { )); var metaType = new TypeLevelValueType(structTypeOrConstructor.value()); - context.declare(untyped.name(), metaType, untyped.source()); + context.defineVariable(untyped.name(), metaType, untyped.source()); return metaType; } @@ -327,11 +338,17 @@ public class TypeChecker { case TypeOrConstructor.Constructor<SimpleStructType> constructor -> constructor.value().params(); }; - var bodyContext = context.enter(); + + var bodyContextDeclaring = context.enter(); for (var typeParam : typeParams) { - // TODO: better source - bodyContext.declare(typeParam.name(), Types.metaType(typeParam), untyped.source()); + bodyContextDeclaring.define( + typeParam.name(), + Types.metaType(typeParam), + // TODO: better source + untyped.source() + ); } + var bodyContext = bodyContextDeclaring.endDeclarations(); var typeCheckedFieldDefinitions = untyped.fields().isPresent() ? Optional.of(typeCheckFieldDefinitions(untyped.fields().get(), bodyContext)) @@ -394,7 +411,7 @@ public class TypeChecker { ) { var sumType = new SumType(context.namespaceName(), untyped.name()); var metaType = new TypeLevelValueType(sumType); - context.declare(untyped.name(), metaType, untyped.source()); + context.defineVariable(untyped.name(), metaType, untyped.source()); return metaType; } @@ -658,8 +675,8 @@ public class TypeChecker { Source source, TypeCheckerNamespaceContext context ) { - var type = context.lookupType(name) - .orElseThrow(() -> new UndeclaredVariableError(name, source)); + var type = context.lookupVariable(name) + .toDefinedType(name, source); // TODO: handle not a type-level value if (!(type instanceof TypeLevelValueType(TypeLevelValue value))) { @@ -794,7 +811,7 @@ public class TypeChecker { ); var typedParams = new ArrayList<TypedParamNode>(); - var bodyContext = context.enter(); + var bodyContextDeclaring = context.enter(); for (var paramIndex = 0; paramIndex < untyped.params().size(); paramIndex++) { var untypedParam = untyped.params().get(paramIndex); @@ -812,9 +829,11 @@ public class TypeChecker { ); typedParams.add(typedParam); - bodyContext.declare(typedParam.name(), typedParam.type(), typedParam.source()); + bodyContextDeclaring.define(typedParam.name(), typedParam.type(), typedParam.source()); } + var bodyContext = bodyContextDeclaring.endDeclarations(); + var paramTypes = typedParams.stream() .map(param -> param.type()) .toList(); @@ -896,8 +915,8 @@ public class TypeChecker { UntypedReferenceNode reference, TypeCheckerNamespaceContext context ) { - var type = context.lookupType(reference.name()) - .orElseThrow(() -> new UndeclaredVariableError(reference.name(), reference.source())); + var type = context.lookupVariable(reference.name()) + .toDefinedType(reference.name(), reference.source()); return new TypedReferenceNode( reference.name(), 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 c66c999..438fc6c 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java @@ -31,7 +31,9 @@ public class TypeCheckerGlobalContext { this.nativeTypes.put(typeConstructor.genericType().name(), new TypeLevelValueType(typeConstructor)); } - public TypeCheckerNamespaceContext enterNamespace(NamespaceName namespaceName) { + public TypeCheckerNamespaceContext.Declaring enterNamespace( + NamespaceName namespaceName + ) { return TypeCheckerNamespaceContext.initial( namespaceName, this.nativeTypes, diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java index 7e56877..c0faece 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.typechecker; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.sources.Source; import org.zwobble.hobgoblin.compiler.typechecker.errors.DuplicateVariableNameError; import org.zwobble.hobgoblin.compiler.types.*; @@ -10,28 +11,38 @@ import java.util.Map; import java.util.Optional; public class TypeCheckerNamespaceContext { - public static TypeCheckerNamespaceContext initial( + public static Declaring initial( NamespaceName namespaceName, Map<String, Type> nativeTypes, TypesInfoInMemory typesInfo ) { + var variables = new HashMap<String, Variable.Declared>(); + for (var nativeType : nativeTypes.entrySet()) { + var variable = Variable.defined( + nativeType.getKey(), + nativeType.getValue(), + NullSource.INSTANCE + ); + variables.put(nativeType.getKey(), variable); + } + return new TypeCheckerNamespaceContext( namespaceName, Optional.empty(), - new HashMap<>(nativeTypes), + variables, typesInfo - ); + ).enter(); } private final NamespaceName namespaceName; private final Optional<TypeCheckerNamespaceContext> parent; - private final Map<String, Type> variables; + private final Map<String, Variable.Declared> variables; private final TypesInfoInMemory typesInfo; private TypeCheckerNamespaceContext( NamespaceName namespaceName, Optional<TypeCheckerNamespaceContext> parent, - Map<String, Type> variables, + Map<String, Variable.Declared> variables, TypesInfoInMemory typesInfo ) { this.namespaceName = namespaceName; @@ -49,17 +60,58 @@ public class TypeCheckerNamespaceContext { return this.namespaceName; } - public void declare(String name, Type type, Source source) { - if (this.variables.containsKey(name)) { - throw new DuplicateVariableNameError(name, source); + public void defineVariable(String name, Type type, Source source) { + var variable = this.variables.get(name); + + if (variable == null) { + throw new UnsupportedOperationException("TODO: variable not declared"); } - this.variables.put(name, type); + switch (variable) { + case Variable.Defined defined -> { + throw new UnsupportedOperationException("TODO: variable already defined"); + } + + case Variable.Undefined undefined -> { + this.variables.put(name, Variable.defined(name, type, source)); + } + } } - public Optional<Type> lookupType(String name) { - return Optional.ofNullable(this.variables.get(name)) - .or(() -> this.parent.flatMap(parent -> parent.lookupType(name))); + private static void defineVariable( + Map<String, Variable.Declared> variables, + String name, + Type type, + Source source + ) { + var variable = variables.get(name); + + if (variable == null) { + throw new UnsupportedOperationException("TODO: variable not declared"); + } + + switch (variable) { + case Variable.Defined defined -> { + throw new UnsupportedOperationException("TODO: variable already defined"); + } + + case Variable.Undefined undefined -> { + variables.put(name, Variable.defined(name, type, source)); + } + } + } + + Variable lookupVariable(String name) { + var variable = this.variables.get(name); + if (variable != null) { + return variable; + } + + if (parent.isPresent()) { + return parent.get().lookupVariable(name); + } + + return new Variable.Undeclared(); } public void defineDatabaseSchema( @@ -106,14 +158,62 @@ public class TypeCheckerNamespaceContext { return this.typesInfo.fieldsOf(type); } - public TypeCheckerNamespaceContext enter() { - // TODO: variables should be declared up front so that shadowing can't - // happen midway through a scope. - return new TypeCheckerNamespaceContext( + public Declaring enter() { + var variablesMap = new HashMap<String, Variable.Declared>(); + + return new Declaring( this.namespaceName, Optional.of(this), - new HashMap<>(), + variablesMap, this.typesInfo ); } + + public static class Declaring { + private final NamespaceName namespaceName; + private final Optional<TypeCheckerNamespaceContext> parent; + private final Map<String, Variable.Declared> variables; + private final TypesInfoInMemory typesInfo; + + private Declaring( + NamespaceName namespaceName, + Optional<TypeCheckerNamespaceContext> parent, + Map<String, Variable.Declared> variables, + TypesInfoInMemory typesInfo + ) { + this.namespaceName = namespaceName; + this.parent = parent; + this.variables = variables; + this.typesInfo = typesInfo; + } + + public Declaring declare(String name, Source source) { + if (this.variables.containsKey(name)) { + throw new DuplicateVariableNameError(name, source); + } + + this.variables.put(name, new Variable.Undefined(name, source)); + + return this; + } + + public Declaring define(String name, Type type, Source source) { + if (this.variables.containsKey(name)) { + throw new DuplicateVariableNameError(name, source); + } + + this.variables.put(name, new Variable.Defined(name, type, source)); + + return this; + } + + public TypeCheckerNamespaceContext endDeclarations() { + return new TypeCheckerNamespaceContext( + namespaceName, + parent, + variables, + typesInfo + ); + } + } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/Variable.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/Variable.java new file mode 100644 index 0000000..d2831ec --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/Variable.java @@ -0,0 +1,39 @@ +package org.zwobble.hobgoblin.compiler.typechecker; + +import org.zwobble.hobgoblin.compiler.sources.Source; +import org.zwobble.hobgoblin.compiler.typechecker.errors.UndeclaredVariableError; +import org.zwobble.hobgoblin.compiler.types.Type; + +sealed interface Variable { + Type toDefinedType(String name, Source source); + + sealed interface Declared extends Variable { + String name(); + Source source(); + } + + record Undeclared() implements Variable { + @Override + public Type toDefinedType(String name, Source source) { + throw new UndeclaredVariableError(name, source); + } + } + + record Undefined(String name, Source source) implements Variable, Declared { + @Override + public Type toDefinedType(String name, Source source) { + throw new UnsupportedOperationException("TODO: not defined"); + } + } + + static Defined defined(String name, Type type, Source source) { + return new Defined(name, type, source); + } + + record Defined(String name, Type type, Source source) implements Variable, Declared { + @Override + public Type toDefinedType(String name, Source source) { + return type; + } + } +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/util/Pair.java b/src/main/java/org/zwobble/hobgoblin/compiler/util/Pair.java new file mode 100644 index 0000000..86389ee --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/util/Pair.java @@ -0,0 +1,4 @@ +package org.zwobble.hobgoblin.compiler.util; + +public record Pair<T1, T2>(T1 left, T2 right) { +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java index 64e2be3..d99037b 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java @@ -28,9 +28,10 @@ public class TypeCheckerBinaryOperationTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); - namespaceContext.declare("b", NativeTypes.INT_32, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("a", NativeTypes.INT_32, NullSource.INSTANCE) + .define("b", NativeTypes.INT_32, NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); @@ -51,9 +52,10 @@ public class TypeCheckerBinaryOperationTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); - namespaceContext.declare("b", NativeTypes.STRING, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("a", NativeTypes.INT_32, NullSource.INSTANCE) + .define("b", NativeTypes.STRING, NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnsupportedBinaryOperationError.class, diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java index 49aa4b6..84e930a 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java @@ -21,11 +21,12 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("List"), List.of(UntypedArb.typeLevelReference("Int32")) ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); - context.declare("List", new TypeLevelValueType(listType), NullSource.INSTANCE); var int32Type = SimpleNativeType.builtin("Int32"); - context.declare("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(listType), NullSource.INSTANCE) + .define("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -42,12 +43,13 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("List"), List.of(UntypedArb.typeLevelReference("Int32")) ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); var listTypeConstructor = Types.constructor(List.of(new TypeParam(listType, "T")), listType); - context.declare("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE); var int32Type = SimpleNativeType.builtin("Int32"); - context.declare("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContext().enter() + .define("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE) + .define("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckTypeLevelExpression(untyped, context); @@ -62,10 +64,11 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("List"), List.of() ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); var listTypeConstructor = Types.constructor(List.of(new TypeParam(listType, "T")), listType); - context.declare("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( WrongNumberOfTypeLevelArgsError.class, @@ -85,10 +88,11 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("Int64") ) ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); var listTypeConstructor = Types.constructor(List.of(new TypeParam(listType, "T")), listType); - context.declare("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( WrongNumberOfTypeLevelArgsError.class, diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java index 4959aa0..e8d44d1 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java @@ -16,6 +16,18 @@ public class TypeCheckerContextArb { public static TypeCheckerNamespaceContext namespaceContext(NamespaceName namespaceName) { return TypeCheckerGlobalContext.initial() + .enterNamespace(namespaceName) + .endDeclarations(); + } + + public static TypeCheckerNamespaceContext.Declaring namespaceContextDeclaring() { + return namespaceContextDeclaring(NamespaceName.of("example")); + } + + public static TypeCheckerNamespaceContext.Declaring namespaceContextDeclaring( + NamespaceName namespaceName + ) { + return TypeCheckerGlobalContext.initial() .enterNamespace(namespaceName); } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java index 63ed810..37434f3 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java @@ -15,6 +15,7 @@ import java.util.Optional; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedDatabaseSchemaDefinitionNodeMatcher.isTypedDatabaseSchemaDefinitionNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedTableDefinitionNodeMatcher.isTypedTableDefinitionNode; import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement; +import static org.zwobble.hobgoblin.compiler.typechecker.VariableMatchers.isDefinedVariable; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -33,9 +34,11 @@ public class TypeCheckerDatabaseSchemaDefinitionTests { ) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleStructType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .declare("blogApp", NullSource.INSTANCE) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(authorType, Optional.of(List.of( new Field("id", NativeTypes.INT_32, NullSource.INSTANCE) ))); @@ -51,8 +54,10 @@ public class TypeCheckerDatabaseSchemaDefinitionTests { )) ); assertThat( - namespaceContext.lookupType("blogApp"), - isOptionalOf(equalTo(Types.databaseSchema(new DatabaseSchema(NamespaceName.of("App"), "blogApp")))) + namespaceContext.lookupVariable("blogApp"), + isDefinedVariable( + Types.databaseSchema(new DatabaseSchema(NamespaceName.of("App"), "blogApp")) + ) ); var typesInfo = namespaceContext.toTypesInfo(); var schemaInfo = typesInfo.databaseSchemaInfo(new DatabaseSchema(NamespaceName.of("App"), "blogApp")); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java index ac85b7d..f47eb1c 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java @@ -3,6 +3,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.typed.*; import org.zwobble.hobgoblin.compiler.ast.untyped.*; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.*; import java.util.List; @@ -17,7 +18,9 @@ public class TypeCheckerEnumDefinitionTests { var untyped = UntypedEnumDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -42,7 +45,9 @@ public class TypeCheckerEnumDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); + .enterNamespace(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java index a03beae..af8005f 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java @@ -30,8 +30,9 @@ public class TypeCheckerFunctionExpressionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("a", NativeTypes.INT_32, NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); @@ -54,7 +55,8 @@ public class TypeCheckerFunctionExpressionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var typeHint = new FunctionType( List.of(NativeTypes.INT_64, NativeTypes.STRING), @@ -82,7 +84,8 @@ public class TypeCheckerFunctionExpressionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var typeHint = new FunctionType( List.of(NativeTypes.INT_32), @@ -100,6 +103,6 @@ public class TypeCheckerFunctionExpressionTests { NativeTypes.INT_32 ))) ); - assertThat(namespaceContext.lookupType("a"), isOptionalEmpty()); + assertThat(namespaceContext.lookupVariable("a"), equalTo(new Variable.Undeclared())); } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java index 8c5e053..6bd99d2 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java @@ -26,9 +26,10 @@ public class TypeCheckerMemberAccessTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); var databaseSchema = new DatabaseSchema(namespaceName, "blogApp"); - namespaceContext.declare("blogApp", Types.databaseSchema(databaseSchema), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("blogApp", Types.databaseSchema(databaseSchema), NullSource.INSTANCE) + .endDeclarations(); var table = new Table(databaseSchema, "authors"); var rowType = new SimpleStructType(namespaceName, "Author"); var idField = new Field("id", NativeTypes.INT_32, NullSource.INSTANCE); @@ -54,10 +55,11 @@ public class TypeCheckerMemberAccessTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); var databaseSchema = new DatabaseSchema(namespaceName, "blogApp"); var table = new Table(databaseSchema, "authors"); - namespaceContext.declare("authors", Types.table(table), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("authors", Types.table(table), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java index 713cd13..76bbb59 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java @@ -18,9 +18,10 @@ public class TypeCheckerMetaTypeTests { @Test public void whenVariableIsNotTypeLevelValueThenErrorIsThrown() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", nativeType, NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", nativeType, NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -34,10 +35,11 @@ public class TypeCheckerMetaTypeTests { @Test public void whenVariableIsTypeLevelValueButNotMetaTypeThenErrorIsThrown() { var untyped = UntypedArb.typeLevelReference("List"); - var context = TypeCheckerContextArb.namespaceContext(); var innerType = SimpleNativeType.builtin("List"); var typeConstructor = Types.constructor(List.of(new TypeParam(innerType, "T")), innerType); - context.declare("List", new TypeLevelValueType(typeConstructor), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(typeConstructor), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -51,9 +53,10 @@ public class TypeCheckerMetaTypeTests { @Test public void whenNameIsTypeLevelValueThenValueIsType() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckMetaType(untyped, context); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java index 4f22537..8c93776 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java @@ -12,14 +12,14 @@ import static org.zwobble.precisely.Matchers.equalTo; public class TypeCheckerNamespaceContextTests { @Test - public void declaringVariablesWithSameThrowsError() { + public void declaringVariablesWithSameNameThrowsError() { var context = TypeCheckerGlobalContext.initial() .enterNamespace(NamespaceName.of()); - context.declare("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE); + context.define("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE); var error = assertThrows( DuplicateVariableNameError.class, - () -> context.declare("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE) + () -> context.define("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE) ); assertThat(error.name(), equalTo("Shape")); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java index 1777506..4409261 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java @@ -3,6 +3,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.typed.TypedNativeTypeDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.NamespaceName; import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; import org.zwobble.hobgoblin.compiler.types.TypeOrConstructor; @@ -17,7 +18,9 @@ public class TypeCheckerNativeTypeDefinitionTests { var untyped = UntypedNativeTypeDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java index 40df722..47c6c0b 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java @@ -21,7 +21,8 @@ public class TypeCheckerNumberLiteralTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java index 43ffb04..e28e2d5 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java @@ -27,10 +27,11 @@ public class TypeCheckerQueryDefinitionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); var databaseSchema = new DatabaseSchema(namespaceName, "blogApp"); var table = new Table(databaseSchema, "authors"); - namespaceContext.declare("authors", Types.table(table), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("authors", Types.table(table), NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java index 4671a22..2fb179b 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java @@ -22,8 +22,9 @@ public class TypeCheckerReferenceTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("count", NativeTypes.INT_32, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("count", NativeTypes.INT_32, NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); @@ -40,7 +41,8 @@ public class TypeCheckerReferenceTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var error = assertThrows( UndeclaredVariableError.class, 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 f25a600..982e449 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -5,6 +5,7 @@ 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.untyped.*; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.typechecker.errors.SingletonStructCannotHaveTypeParamsError; import org.zwobble.hobgoblin.compiler.types.*; @@ -13,6 +14,7 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement; +import static org.zwobble.hobgoblin.compiler.typechecker.VariableMatchers.isDefinedVariable; import static org.zwobble.hobgoblin.compiler.types.TypeMatchers.isField; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -23,7 +25,9 @@ public class TypeCheckerStructDefinitionTests { var untyped = UntypedStructDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -36,8 +40,8 @@ public class TypeCheckerStructDefinitionTests { equalTo(TypeOrConstructor.type(expectedType)) ) )); - assertThat(context.lookupType("X"), isOptionalOf( - equalTo(Types.metaType(expectedType)) + assertThat(context.lookupVariable("X"), isDefinedVariable( + Types.metaType(expectedType) )); } @@ -51,7 +55,9 @@ public class TypeCheckerStructDefinitionTests { ))) .withFields(List.of()) .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -71,8 +77,8 @@ public class TypeCheckerStructDefinitionTests { equalTo(TypeOrConstructor.constructor(expectedTypeConstructor)) ) )); - assertThat(context.lookupType("X"), isOptionalOf( - equalTo(Types.metaType(expectedTypeConstructor)) + assertThat(context.lookupVariable("X"), isDefinedVariable( + Types.metaType(expectedTypeConstructor) )); } @@ -91,7 +97,9 @@ public class TypeCheckerStructDefinitionTests { globalContext.addNativeType(int32Type); globalContext.addNativeType(int64Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -130,7 +138,9 @@ public class TypeCheckerStructDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -158,7 +168,9 @@ public class TypeCheckerStructDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); assertThrows( SingletonStructCannotHaveTypeParamsError.class, @@ -187,7 +199,9 @@ public class TypeCheckerStructDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); 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 a1cb5a9..1474e39 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java @@ -27,7 +27,9 @@ public class TypeCheckerSumDefinitionTests { var untyped = UntypedSumDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -54,9 +56,11 @@ public class TypeCheckerSumDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); - namespaceContext.declare(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE); - namespaceContext.declare(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE); + .enterNamespace(NamespaceName.of("a", "b")) + .define(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE) + .define(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -103,9 +107,11 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeTypeConstructor(NativeTypes.BOX); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); - namespaceContext.declare(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE); - namespaceContext.declare(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE); + .enterNamespace(NamespaceName.of("a", "b")) + .define(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE) + .define(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -147,7 +153,9 @@ public class TypeCheckerSumDefinitionTests { globalContext.addNativeType(NativeTypes.INT_32); globalContext.addNativeTypeConstructor(NativeTypes.BOX); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); + .enterNamespace(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( SumVariantMustBeStructError.class, @@ -171,8 +179,10 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeTypeConstructor(NativeTypes.BOX); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); - namespaceContext.declare("Variant", new TypeLevelValueType(variantType), NullSource.INSTANCE); + .enterNamespace(NamespaceName.of("a", "b")) + .define("Variant", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( SumVariantMustBeInSameNamespaceError.class, @@ -203,7 +213,9 @@ public class TypeCheckerSumDefinitionTests { globalContext.addNativeType(int32Type); globalContext.addNativeType(int64Type); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); + .enterNamespace(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -246,10 +258,12 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("Shape", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(variantType, Optional.of(List.of())); var error = assertThrows( @@ -283,10 +297,12 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("Shape", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(variantType, Optional.of(List.of(new Field("area", int64Type, NullSource.INSTANCE)))); // TODO: more specific error @@ -319,10 +335,12 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("Shape", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(variantType, Optional.of(List.of())); assertThrows( @@ -340,11 +358,13 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var sumType = new SumType(namespaceName, "Shape"); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Sum", new TypeLevelValueType(sumType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Sum", new TypeLevelValueType(sumType), NullSource.INSTANCE) + .declare("Square", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineSumType( sumType, List.of(new SumVariant(0, variantType, variantType)), diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java index 9dbee97..ef89ee0 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java @@ -30,9 +30,10 @@ public class TypeCheckerTableDefinitionTests { .withPrimaryKey(UntypedTypeLevelReferenceNode.arbitrary().withName("id")) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleStructType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(authorType, Optional.of(List.of( new Field("id", NativeTypes.INT_32, NullSource.INSTANCE) ))); @@ -61,9 +62,10 @@ public class TypeCheckerTableDefinitionTests { ) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleNativeType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( TableRowTypeMustBeStructError.class, @@ -88,9 +90,10 @@ public class TypeCheckerTableDefinitionTests { .withPrimaryKey(UntypedTypeLevelReferenceNode.arbitrary().withName("id")) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleStructType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(authorType, Optional.of(List.of( new Field("author_id", NativeTypes.INT_32, NullSource.INSTANCE) ))); 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 98f4f22..003641e 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java @@ -31,9 +31,10 @@ public class TypeCheckerTypeLevelReferenceTests { @Test public void whenVariableIsNotTypeLevelValueThenErrorIsThrown() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", nativeType, NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", nativeType, NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -47,9 +48,10 @@ public class TypeCheckerTypeLevelReferenceTests { @Test public void whenNameIsTypeLevelValueThenValueIsType() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckTypeLevelExpression(untyped, context); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/VariableMatchers.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/VariableMatchers.java new file mode 100644 index 0000000..9e17a99 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/VariableMatchers.java @@ -0,0 +1,21 @@ +package org.zwobble.hobgoblin.compiler.typechecker; + +import org.zwobble.hobgoblin.compiler.types.Type; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.*; + +public class VariableMatchers { + public VariableMatchers() {} + + static Matcher<Variable> isDefinedVariable(Type type) { + return instanceOf( + Variable.Defined.class, + has( + "type", + Variable.Defined::type, + equalTo(type) + ) + ); + } +} |
