aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-20 16:47:47 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-20 16:54:29 +0100
commit1bf46b8978d87e40012da86af60545e0c37dfb79 (patch)
tree3921b6c522eb2ed6254eec0c1c6a11ebdb1392d3 /src/main/java
parent48217d912243feadde179709a61b1741941606f3 (diff)
Use appropriate name for init function
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/PatioBackend.java1
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java12
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java47
3 files changed, 55 insertions, 5 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/PatioBackend.java b/src/main/java/org/shedlang/compiler/backends/PatioBackend.java
index c6a091dd..53ab3c6b 100644
--- a/src/main/java/org/shedlang/compiler/backends/PatioBackend.java
+++ b/src/main/java/org/shedlang/compiler/backends/PatioBackend.java
@@ -28,6 +28,7 @@ public class PatioBackend implements Backend {
System.out.println(namespaceName);
var compilationUnit = PatioCodeGen.generateNamespace(
+ namespaceName,
instructions,
PatioCodeGenContext.EMPTY
);
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
index 6846ec40..c4f040dc 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -8,6 +8,7 @@ import org.shedlang.compiler.patio.ast.instructions.PatioReturn;
import org.shedlang.compiler.sources.Source;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.stackir.instructions.*;
+import org.shedlang.compiler.types.NamespaceName;
import java.util.ArrayList;
import java.util.Collections;
@@ -20,14 +21,15 @@ public class PatioCodeGen {
private PatioCodeGen() {}
public static PatioCompilationUnit generateNamespace(
+ NamespaceName namespaceName,
List<Instruction> stackIrInstructions,
PatioCodeGenContext context
) {
- context = context.createFunction(
- "init",
- false,
- Sources.NULL,
- initContext -> generateInstructions(stackIrInstructions, initContext)
+ context = PatioCodeGenNamespaces.define(
+ namespaceName,
+ initContext ->
+ generateInstructions(stackIrInstructions, initContext),
+ context
);
// TODO: set more appropriate source
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java
new file mode 100644
index 00000000..b7903eb7
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java
@@ -0,0 +1,47 @@
+package org.shedlang.compiler.patio;
+
+import org.shedlang.compiler.sources.Sources;
+import org.shedlang.compiler.types.NamespaceName;
+
+import java.util.function.Function;
+
+public class PatioCodeGenNamespaces {
+ private PatioCodeGenNamespaces() {}
+
+ public static PatioCodeGenContext define(
+ NamespaceName namespaceName,
+ Function<PatioCodeGenContext, PatioCodeGenContext> generateInitBody,
+ PatioCodeGenContext context
+ ) {
+ context = context.createFunction(
+ globalNameNamespaceInit(namespaceName),
+ false,
+ Sources.NULL,
+ initContext ->
+ generateInitBody.apply(initContext)
+ );
+
+ return context;
+ }
+
+ // TODO: allocate names through context
+ private static String globalNameNamespaceValue(NamespaceName namespaceName) {
+ return "shed__namespace__" + namespaceNameToPatioIdentifier(namespaceName);
+ }
+
+ private static String globalNameNamespaceInit(NamespaceName namespaceName) {
+ return "shed__namespaceInit__" + namespaceNameToPatioIdentifier(namespaceName);
+ }
+
+ private static String globalNameNamespaceIsInited(NamespaceName namespaceName) {
+ return "shed__namespaceIsInited__" + namespaceNameToPatioIdentifier(namespaceName);
+ }
+
+ private static String globalNameNamespaceImport(NamespaceName namespaceName) {
+ return "shed__namespaceImport__" + namespaceNameToPatioIdentifier(namespaceName);
+ }
+
+ public static String namespaceNameToPatioIdentifier(NamespaceName namespaceName) {
+ return String.join("_", namespaceName.parts());
+ }
+}