diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-28 14:15:43 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-28 14:15:43 +0100 |
| commit | 8bcc629a7c6d7d30b403c0a13ee032e7e75298e8 (patch) | |
| tree | ce7129121c0f494a6f73b95a605287236e9d93c1 /src | |
| parent | 49aee72f35f1ffdfc90a89162a500193746ef467 (diff) | |
Add define-struct instruction
Diffstat (limited to 'src')
6 files changed, 76 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 1a194db8..e4a72194 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -222,6 +222,10 @@ public class PatioCodeGen { yield context; } + case DefineStructInstruction defineStruct -> { + throw new UnsupportedOperationException("TODO"); + } + case DiscardInstruction discard -> context.temporaryPop().context(); diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index 5336c41d..6577e2f4 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.stackir; import org.shedlang.compiler.stackir.instructions.*; import org.shedlang.compiler.treetext.Tree; +import org.shedlang.compiler.types.StructType; import java.util.List; @@ -70,6 +71,17 @@ public class InstructionSerializer { )), treeNamed("body", instructionsToTree(defineFunctionInstruction.body())) ); + + case DefineStructInstruction defineStructInstruction -> + treeListIndented( + treeIdentifier("define-struct"), + treeNamed("type", structTypeToTree(defineStructInstruction.type())), + treeNamed("fields", treeList( + defineStructInstruction.fields().stream() + .map(field -> treeString(field.name())) + .toList() + )) + ); case DiscardInstruction discardInstruction -> treeList( @@ -94,7 +106,7 @@ public class InstructionSerializer { case FieldLoadInstruction.Receiver.Struct struct -> treeList( treeIdentifier("struct"), - treeString(struct.type().describe()) + structTypeToTree(struct.type()) ); }, treeString(fieldLoad.fieldName()) @@ -214,4 +226,8 @@ public class InstructionSerializer { ); }; } + + private static Tree structTypeToTree(StructType type) { + return treeString(type.describe()); + } } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/DefineStructInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/DefineStructInstruction.java new file mode 100644 index 00000000..bafa51bf --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/DefineStructInstruction.java @@ -0,0 +1,19 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.StructType; + +import java.util.List; + +/** + * Push a struct type value onto the stack with the given definition. + */ +public record DefineStructInstruction( + StructType type, + List<Field> fields, + Source source +) implements Instruction { + public record Field(String name, Source source) { + + } +} diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java index 87466287..e43acf56 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.stackir.instructions; import org.shedlang.compiler.sources.Source; public sealed interface Instruction - permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction { + permits CallInstruction, DefineFunctionInstruction, DefineStructInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction { Source source(); } diff --git a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java index 9f3fc75f..b0143a9b 100644 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java @@ -56,6 +56,26 @@ public class InstructionSerializerTests { } @Test + public void canSerializeDefineStruct() { + var instruction = Instructions.defineStruct( + Types.struct(NamespaceName.of("Example", "Project"), "User"), + List.of( + Instructions.defineStructField("name"), + Instructions.defineStructField("id") + ) + ); + + var string = serialize(instruction); + + assertThat(string, equalTo(""" + ( + define-struct + type="Example/Project.User" + fields=("name" "id") + )""")); + } + + @Test public void canSerializeDiscard() { var instruction = Instructions.discard(); diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java index f30c75c6..3ac0ac62 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java @@ -15,6 +15,21 @@ public class Instructions { return new CallInstruction(positionalArgCount, namedArgNames, Sources.NULL); } + public static Instruction defineStruct( + StructType type, + List<DefineStructInstruction.Field> fields + ) { + return new DefineStructInstruction( + type, + fields, + Sources.NULL + ); + } + + public static DefineStructInstruction.Field defineStructField(String name) { + return new DefineStructInstruction.Field(name, Sources.NULL); + } + public static Instruction discard() { return new DiscardInstruction(Sources.NULL); } |
