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/main/java/org | |
| parent | 49aee72f35f1ffdfc90a89162a500193746ef467 (diff) | |
Add define-struct instruction
Diffstat (limited to 'src/main/java/org')
4 files changed, 41 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(); } |
