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 --- .../shedlang/compiler/stackir/StackCompiler.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/main/java/org/shedlang/compiler/stackir') 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