From 334782898b59817a00a3ee9b8031cd974178d1ec Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 30 Jun 2023 19:51:50 +0100 Subject: Define builtin modules outside of code gen --- .../java/org/shedlang/compiler/stackir/StackCompiler.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (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 b8356ab4..e4c4f3e1 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.stackir; import org.pcollections.PVector; import org.shedlang.compiler.ast.typed.*; import org.shedlang.compiler.stackir.instructions.*; +import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.util.P; import java.util.stream.Collectors; @@ -104,9 +105,16 @@ public class StackCompiler { } public static PVector compileNamespace(TypedNamespaceNode node) { - var instructions = node.body().stream() - .map(statement -> compileNamespaceStatement(statement)) - .reduce(P.vector(), PVector::plusAll); + var instructions = P.vector(); + + instructions = instructions + .plus(new NamespaceImportInstruction(NamespaceName.of("Shed", "Builtins"), node.source())) + .plus(new FieldLoadInstruction(NamespaceName.of("Shed", "Builtins"), "print", node.source())) + .plus(new LocalStoreInstruction("print", node.source())); + + for (var statement : node.body()) { + instructions = instructions.plusAll(compileNamespaceStatement(statement)); + } for (var fieldName : node.fieldNames()) { instructions = instructions -- cgit v1.2.3