From a31c0145beb998c9f83d61770ba03f9230610c87 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 19 Jun 2026 17:30:18 +0100 Subject: Merge variable declaration and definition --- .../compiler/typechecker/TypeChecker.java | 24 ++++------------- .../typechecker/TypeCheckerNamespaceContext.java | 30 +++++++++------------- .../hobgoblin/compiler/typechecker/Variable.java | 15 ----------- 3 files changed, 17 insertions(+), 52 deletions(-) delete mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/typechecker/Variable.java (limited to 'src/main/java/org') 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 98b29a9..abc4bec 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -30,7 +30,7 @@ public class TypeChecker { var importedNamespaceFields = context.namespaceFields(importedNamespace); for (var importedName : untypedImport.importedNames()) { - namespaceContext.define(importedName, importedNamespaceFields.get(importedName)); + namespaceContext.declare(importedName, importedNamespaceFields.get(importedName)); } } @@ -92,10 +92,8 @@ public class TypeChecker { TypeCheckerNamespaceContext context ) { var nativeType = new SimpleNativeType(context.namespaceName(), untyped.name()); - // TODO: tidy up declare vs define - context.declare(untyped.name()); var metaType = new TypeLevelValueType(nativeType); - context.define(untyped.name(), metaType); + context.declare(untyped.name(), metaType); return metaType; } @@ -117,10 +115,8 @@ public class TypeChecker { TypeCheckerNamespaceContext context ) { var structType = new StructType(context.namespaceName(), untyped.name()); - // TODO: tidy up declare vs define - context.declare(untyped.name()); var metaType = new TypeLevelValueType(structType); - context.define(untyped.name(), metaType); + context.declare(untyped.name(), metaType); return metaType; } @@ -173,10 +169,8 @@ public class TypeChecker { TypeCheckerNamespaceContext context ) { var sumType = new SumType(context.namespaceName(), untyped.name()); - // TODO: tidy up declare vs define - context.declare(untyped.name()); var metaType = new TypeLevelValueType(sumType); - context.define(untyped.name(), metaType); + context.declare(untyped.name(), metaType); return metaType; } @@ -284,17 +278,9 @@ public class TypeChecker { Source source, TypeCheckerNamespaceContext context ) { - var variable = context.lookup(name) + var type = context.lookupType(name) .orElseThrow(() -> new UndeclaredVariableError(name, source)); - var type = switch (variable) { - case Variable.Declared declared -> - throw new UndefinedVariableError(name, source); - - case Variable.Defined defined -> - defined.type(); - }; - // TODO: handle not a type-level value if (!(type instanceof TypeLevelValueType(TypeLevelValue value))) { throw new UnexpectedTypeError(new TypeSet.MetaType(), type, source); 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 6366a12..69827a8 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java @@ -3,10 +3,10 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.zwobble.hobgoblin.compiler.types.*; import org.zwobble.hobgoblin.compiler.util.ManyToMany; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; public class TypeCheckerNamespaceContext { public static TypeCheckerNamespaceContext initial( @@ -15,23 +15,22 @@ public class TypeCheckerNamespaceContext { Map> fieldsOf, ManyToMany variants ) { - var variables = nativeTypes.entrySet().stream() - .collect(Collectors.toMap( - entry -> entry.getKey(), - entry -> Variable.defined(entry.getValue()) - )); - - return new TypeCheckerNamespaceContext(namespaceName, variables, fieldsOf, variants); + return new TypeCheckerNamespaceContext( + namespaceName, + new HashMap<>(nativeTypes), + fieldsOf, + variants + ); } private final NamespaceName namespaceName; - private final Map variables; + private final Map variables; private final Map> fieldsOf; private final ManyToMany variants; private TypeCheckerNamespaceContext( NamespaceName namespaceName, - Map variables, + Map variables, Map> fieldsOf, ManyToMany variants ) { @@ -45,17 +44,12 @@ public class TypeCheckerNamespaceContext { return this.namespaceName; } - public void declare(String name) { + public void declare(String name, Type type) { // TODO: Check variable is not already declared. - this.variables.put(name, new Variable.Declared()); - } - - public void define(String name, Type type) { - // TODO: Check variable is not already defined. - this.variables.put(name, new Variable.Defined(type)); + this.variables.put(name, type); } - public Optional lookup(String name) { + public Optional lookupType(String name) { return Optional.ofNullable(this.variables.get(name)); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/Variable.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/Variable.java deleted file mode 100644 index e80283f..0000000 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/Variable.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.zwobble.hobgoblin.compiler.typechecker; - -import org.zwobble.hobgoblin.compiler.types.Type; - -public sealed interface Variable { - public static Variable defined(Type type) { - return new Defined(type); - } - - record Declared() implements Variable { - } - - record Defined(Type type) implements Variable { - } -} -- cgit v1.2.3