From 15a356d4c09944174e482b75f6dab24ca474b285 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 29 Dec 2023 10:18:42 +0000 Subject: Change TypedSumTypeDefinitionNode to have typeOrConstructor() --- .../org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java | 3 ++- src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java | 3 ++- src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java | 2 +- src/main/java/org/shedlang/compiler/types/StructuredType.java | 2 +- src/main/java/org/shedlang/compiler/types/SumType.java | 2 +- src/test/java/org/shedlang/compiler/ast/typed/Typed.java | 5 ++++- src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java | 5 ++++- 7 files changed, 15 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java index cb6b6418..109a7ace 100644 --- a/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java @@ -2,9 +2,10 @@ package org.shedlang.compiler.ast.typed; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.types.SumType; +import org.shedlang.compiler.types.TypeOrConstructor; public record TypedSumTypeDefinitionNode( - SumType type, + TypeOrConstructor typeOrConstructor, Source source ) implements TypedNamespaceStatementNode { } diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java index 501e12e5..d61aa192 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -571,7 +571,8 @@ public class ShidCodeGen { ) { var defineSumTypeInstruction = new ShidPushUnit(node.source()); - var localName = context.shedVariableToLocal(node.type().name()); + var sumType = node.typeOrConstructor().toType(); + var localName = context.shedVariableToLocal(sumType.name()); var storeInstruction = new ShidLocalStore(localName, node.source()); return context diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index ac3fde79..4f428c1f 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -1621,7 +1621,7 @@ public class TypeChecker { var type = context.getLexicalState(SumType.class, untyped.name()); return context.toResult(new TypedSumTypeDefinitionNode( - type, + TypeOrConstructor.type(type), untyped.source() )); } diff --git a/src/main/java/org/shedlang/compiler/types/StructuredType.java b/src/main/java/org/shedlang/compiler/types/StructuredType.java index 114da180..bc836379 100644 --- a/src/main/java/org/shedlang/compiler/types/StructuredType.java +++ b/src/main/java/org/shedlang/compiler/types/StructuredType.java @@ -1,5 +1,5 @@ package org.shedlang.compiler.types; -public sealed interface StructuredType extends Type permits StructType { +public sealed interface StructuredType extends Type permits StructType, SumType { String name(); } diff --git a/src/main/java/org/shedlang/compiler/types/SumType.java b/src/main/java/org/shedlang/compiler/types/SumType.java index 8c293fca..08fef92f 100644 --- a/src/main/java/org/shedlang/compiler/types/SumType.java +++ b/src/main/java/org/shedlang/compiler/types/SumType.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.types; /** * A discriminated union of several different types. */ -public record SumType(NamespaceName namespaceName, String name) implements Type { +public record SumType(NamespaceName namespaceName, String name) implements StructuredType { @Override public String describe() { return namespaceName.toString() + "." + name; diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java index 58b66557..6ef8df43 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -226,7 +226,10 @@ public class Typed { } public static TypedSumTypeDefinitionNode sumTypeDefinition(SumType sumType) { - return new TypedSumTypeDefinitionNode(sumType, Sources.NULL); + return new TypedSumTypeDefinitionNode( + TypeOrConstructor.type(sumType), + Sources.NULL + ); } public static TypedTargetNode targetIgnore() { diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java index 65f5f24b..73aa5c2f 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java @@ -306,7 +306,10 @@ public class TypedNodeMatchers { ) { return instanceOf( TypedSumTypeDefinitionNode.class, - has("type", x -> x.type(), equalTo(type)) + has("typeOrConstructor", x -> x.typeOrConstructor(), instanceOf( + TypeOrConstructor.Type.class, + has("value", x -> x.value(), equalTo(type)) + )) ); } -- cgit v1.2.3