From 04fa68550d0826bdfd8a2cf56fdb0ad1df8c15cb Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 28 Sep 2023 14:30:42 +0100 Subject: Generate stack IR for struct definition --- .../ast/typed/TypedStructDefinitionNode.java | 38 ++++++++++++++++++++++ .../shedlang/compiler/stackir/StackCompiler.java | 22 ++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNode.java index ac66bfae..eee3fcb8 100644 --- a/src/main/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNode.java @@ -1,7 +1,12 @@ package org.shedlang.compiler.ast.typed; +import org.pcollections.PVector; import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.StructType; +import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.types.Types; +import org.shedlang.compiler.util.P; import java.util.List; @@ -10,4 +15,37 @@ public record TypedStructDefinitionNode( List fields, Source source ) implements TypedNamespaceStatementNode { + public static Builder builder() { + return new Builder( + Types.struct(NamespaceName.of("Default", "Test", "Namespace"), "X"), + P.vector() + ); + } + + public record Builder( + StructType type, + PVector fields + ) { + public TypedStructDefinitionNode build() { + return new TypedStructDefinitionNode( + this.type, + this.fields, + Sources.NULL + ); + } + + public Builder withType(StructType type) { + return new Builder( + type, + this.fields + ); + } + + public Builder addField(TypedStructFieldDefinitionNode field) { + return new Builder( + this.type, + this.fields.plus(field) + ); + } + } } diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 8e0ec59e..dc171e00 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -365,7 +365,7 @@ public class StackCompiler { compileFunctionDefinition(functionDefinition, context); case TypedStructDefinitionNode structDefinition -> - throw new UnsupportedOperationException("TODO"); + compileStructDefinition(structDefinition, context); }; } @@ -393,6 +393,26 @@ public class StackCompiler { return context.addInstruction(instruction); } + private static Context compileStructDefinition( + TypedStructDefinitionNode node, + Context context + ) { + var defineStructInstruction = new DefineStructInstruction( + node.type(), + node.fields().stream() + .map(field -> new DefineStructInstruction.Field(field.name(), field.source())) + .toList(), + node.source() + ); + + var localName = context.shedVariableToLocal(node.type().name()); + var storeInstruction = new LocalStoreInstruction(localName, node.source()); + + return context + .addInstruction(defineStructInstruction) + .addInstruction(storeInstruction); + } + private static Context compileTarget( TypedTargetNode target, Context context -- cgit v1.2.3