aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNode.java38
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java22
2 files changed, 59 insertions, 1 deletions
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<TypedStructFieldDefinitionNode> 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<TypedStructFieldDefinitionNode> 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