aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-21 12:42:18 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-21 12:42:18 +0100
commit2c3d90f2c9a6cbcbe0c5dc6758b326539aa7a9b1 (patch)
tree29d323e52b50679e777d095b97e5e26387156c61 /src/main/java
parent511304a1a553249a2d26f8b6ac227190641ec614 (diff)
Add Patio code gen for namespace imports
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java20
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java1
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java1
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java33
4 files changed, 53 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 d658a446..126488e5 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -188,6 +188,26 @@ public class PatioCodeGen {
yield context.temporaryPopIntoLocal(local, localStore.source());
}
+ case NamespaceImportInstruction namespaceImport -> {
+ var namespaceValueResult = context.allocateLocal(
+ PatioCodeGenLocalSpace.NAMESPACE_VALUE,
+ PatioCodeGenNamespaces.NAMESPACE_VALUE_TYPE
+ );
+ var namespaceValue = namespaceValueResult.value();
+ context = namespaceValueResult.context();
+
+ context = PatioCodeGenNamespaces.importValue(
+ namespaceValue,
+ namespaceImport.namespaceName(),
+ namespaceImport.source(),
+ context
+ );
+
+ context = context.temporaryPush(namespaceValue);
+
+ yield context;
+ }
+
case PushBoolInstruction pushBool ->
pushValue(pushBool.value() ? 1 : 0, pushBool.source(), context);
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
index fdfb8b0b..a1c4d846 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenContext.java
@@ -43,6 +43,7 @@ public record PatioCodeGenContext(
}
public CompilationUnitContext addElement(PatioCompilationUnitElement element) {
+ // TODO: handle existing element with name.
return new CompilationUnitContext(
this.typeInfo,
this.elementsByName.plus(element.name(), element)
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
index e487014e..dba6a523 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenLocalSpace.java
@@ -8,6 +8,7 @@ public enum PatioCodeGenLocalSpace {
CLOSURE_POINTER("closurePointer"),
CLOSURE_FUNCTION_POINTER("closureFunctionPointer"),
NAMESPACE_IS_INITED_POINTER("namespaceIsInitedPointer"),
+ NAMESPACE_VALUE("namespaceValue"),
LOCAL("local"),
RECEIVER("receiver"),
TEMPORARY("tmp");
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java
index 43101e7a..11a4477e 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenNamespaces.java
@@ -1,11 +1,15 @@
package org.shedlang.compiler.patio;
+import org.shedlang.compiler.patio.ast.PatioFunctionDeclaration;
import org.shedlang.compiler.patio.ast.PatioVisibility;
import org.shedlang.compiler.patio.ast.instructions.*;
import org.shedlang.compiler.patio.ast.operands.PatioBool;
import org.shedlang.compiler.patio.ast.operands.PatioDataPtr;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
import org.shedlang.compiler.patio.ast.types.PatioDataLayout;
+import org.shedlang.compiler.patio.ast.types.PatioType;
import org.shedlang.compiler.patio.ast.types.PatioTypes;
+import org.shedlang.compiler.sources.Source;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.NamespaceTypeInfo;
@@ -16,6 +20,8 @@ import java.util.Optional;
import java.util.function.Function;
public class PatioCodeGenNamespaces {
+ public static final PatioType NAMESPACE_VALUE_TYPE = PatioCodeGenTypes.VALUE_TYPE;
+
private PatioCodeGenNamespaces() {}
public static PatioCodeGenContext define(
@@ -64,7 +70,7 @@ public class PatioCodeGenNamespaces {
context = context.createFunction(
globalNameNamespaceImport(namespaceName),
PatioVisibility.PUBLIC,
- PatioCodeGenTypes.VALUE_TYPE,
+ NAMESPACE_VALUE_TYPE,
source,
importContext -> {
var isInitedResult = importContext.allocateLocal(
@@ -98,7 +104,7 @@ public class PatioCodeGenNamespaces {
var namespaceValueResult = importContext.allocateLocal(
PatioCodeGenLocalSpace.TEMPORARY,
- PatioCodeGenTypes.VALUE_TYPE
+ NAMESPACE_VALUE_TYPE
);
var namespaceValue = namespaceValueResult.value();
importContext = namespaceValueResult.context();
@@ -123,6 +129,29 @@ public class PatioCodeGenNamespaces {
return context;
}
+ public static PatioCodeGenContext importValue(
+ PatioLocalRef target,
+ NamespaceName namespaceName,
+ Source source,
+ PatioCodeGenContext context
+ ) {
+ context = context.addCompilationUnitElement(new PatioFunctionDeclaration(
+ globalNameNamespaceImport(namespaceName),
+ List.of(),
+ NAMESPACE_VALUE_TYPE,
+ source
+ ));
+
+ context = context.addInstruction(new PatioCall(
+ Optional.of(target),
+ globalNameNamespaceImport(namespaceName),
+ List.of(),
+ source
+ ));
+
+ return context;
+ }
+
private static PatioDataLayout namespaceDataLayout(NamespaceTypeInfo namespaceTypeInfo) {
return PatioTypes.structFlat(
Lists.repeat(