aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/shid
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-13 23:34:49 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-13 23:34:49 +0000
commit7c6fc48c7ab8a23f8568a2f0c57c66e77a5aa69c (patch)
treed202daa047112c06868bf15b7b0d8a511db7b039 /src/main/java/org/shedlang/compiler/shid
parent86b4a6cdb0e2c311bdcbdcb109e77d04b094a86f (diff)
Use Shid bound variables to generate Patio variables
Diffstat (limited to 'src/main/java/org/shedlang/compiler/shid')
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java46
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidSerializer.java22
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidVariable.java13
4 files changed, 57 insertions, 26 deletions
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
index a16ae895..fee218db 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
@@ -507,16 +507,16 @@ public class ShidCodeGen {
}
private static Context compileReference(TypedReferenceNode node, Context context) {
- var shedVariable = context.shedVariable(node.name());
+ var shidVariable = context.shedVariableToShidVariable(node.name());
- if (shedVariable.isVar) {
+ if (shidVariable.isVar()) {
return context.addInstruction(new ShidVarLoad(
- shedVariable.shidLocalName,
+ shidVariable.name(),
node.source()
));
} else {
return context.addInstruction(new ShidLocalLoad(
- shedVariable.shidLocalName,
+ shidVariable.name(),
node.source()
));
}
@@ -767,37 +767,37 @@ public class ShidCodeGen {
);
private final PVector<ShidInstruction> instructions;
- private final PMap<String, ShedVariable> shedVariablesByName;
- private final PVector<String> boundLocals;
+ private final PMap<String, ShidVariable> shedVariablesByName;
+ private final PVector<ShidVariable> boundVariables;
private final Source source;
public BlockContext(
PVector<ShidInstruction> instructions,
- PMap<String, ShedVariable> shedVariablesByName,
- PVector<String> boundLocals,
+ PMap<String, ShidVariable> shedVariablesByName,
+ PVector<ShidVariable> boundVariables,
Source source
) {
this.instructions = instructions;
this.shedVariablesByName = shedVariablesByName;
- this.boundLocals = boundLocals;
+ this.boundVariables = boundVariables;
this.source = source;
}
public ShidBlock toBlock() {
// TODO: test collection of bound locals
- return new ShidBlock(this.boundLocals, this.instructions, this.source);
+ return new ShidBlock(this.boundVariables, this.instructions, this.source);
}
public BlockContext addInstruction(ShidInstruction instruction) {
return new BlockContext(
this.instructions.plus(instruction),
this.shedVariablesByName,
- this.boundLocals,
+ this.boundVariables,
this.source
);
}
- public ShedVariable shedVariable(String variableName) {
+ public ShidVariable shedVariable(String variableName) {
var shedVariable = this.shedVariablesByName.get(variableName);
if (shedVariable == null) {
@@ -807,11 +807,11 @@ public class ShidCodeGen {
return shedVariable;
}
- public BlockContext addShedVariable(String name, ShedVariable variable) {
+ public BlockContext addShedVariable(String shedName, ShidVariable shidVariable) {
return new BlockContext(
this.instructions,
- this.shedVariablesByName.plus(name, variable),
- this.boundLocals.plus(variable.shidLocalName),
+ this.shedVariablesByName.plus(shedName, shidVariable),
+ this.boundVariables.plus(shidVariable),
this.source
);
}
@@ -875,16 +875,16 @@ public class ShidCodeGen {
}
public String shedVariableToLocal(String variableName) {
- return shedVariable(variableName).shidLocalName;
+ return shedVariableToShidVariable(variableName).name();
}
- public ShedVariable shedVariable(String variableName) {
+ public ShidVariable shedVariableToShidVariable(String variableName) {
return this.block.shedVariable(variableName);
}
// TODO: make private / remove? Variables should be declared using enterBlock().
public Context addShedVal(String variableName) {
- return addShedVariable(BoundVariable.immutable(variableName));
+ return addShedVariable(BoundVariable.val(variableName));
}
// TODO: make private? Variables should be declared using enterBlock().
@@ -893,7 +893,14 @@ public class ShidCodeGen {
return new Context(
this.blockStack,
- this.block.addShedVariable(boundVariable.name(), new ShedVariable(localName, boundVariable.isMutable())),
+ this.block.addShedVariable(
+ boundVariable.name(),
+ new ShidVariable(localName, switch (boundVariable.variant()) {
+ case PARAM -> ShidVariable.Variant.PARAM;
+ case VAL -> ShidVariable.Variant.VAL;
+ case VAR -> ShidVariable.Variant.VAR;
+ })
+ ),
this.localNames.plus(localName)
);
}
@@ -921,5 +928,4 @@ public class ShidCodeGen {
public record Result<T>(T value, Context context) {}
}
- private record ShedVariable(String shidLocalName, boolean isVar) {}
}
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
index e764edca..66f6e049 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
@@ -25,7 +25,22 @@ public class ShidSerializer {
}
private static Tree blockToTree(ShidBlock block) {
- return instructionsToTree(block.instructions());
+ return treeListIndented(
+ treeIdentifier("block"),
+ treeListIndented(
+ block.boundVariables().stream()
+ .map(variable -> treeList(
+ treeIdentifier(switch (variable.variant()) {
+ case PARAM -> "param";
+ case VAL -> "val";
+ case VAR -> "var";
+ }),
+ treeString(variable.name())
+ ))
+ .toList()
+ ),
+ instructionsToTree(block.instructions())
+ );
}
private static Tree instructionsToTree(List<? extends ShidInstruction> instructions) {
@@ -39,10 +54,7 @@ public class ShidSerializer {
private static Tree instructionToTree(ShidInstruction instruction) {
return switch (instruction) {
case ShidBlock blockInstruction ->
- treeListIndented(
- treeIdentifier("block"),
- instructionsToTree(blockInstruction.instructions())
- );
+ blockToTree(blockInstruction);
case ShidCall callInstruction ->
treeList(
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java
index 9bf17b3f..d8eaca5c 100644
--- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java
@@ -5,7 +5,7 @@ import org.shedlang.compiler.sources.Source;
import java.util.List;
public record ShidBlock(
- List<String> boundLocals,
+ List<ShidVariable> boundVariables,
List<ShidInstruction> instructions,
Source source
) implements ShidInstruction {
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidVariable.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidVariable.java
new file mode 100644
index 00000000..a183c955
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidVariable.java
@@ -0,0 +1,13 @@
+package org.shedlang.compiler.shid.instructions;
+
+public record ShidVariable(String name, Variant variant) {
+ public boolean isVar() {
+ return this.variant == Variant.VAR;
+ }
+
+ public enum Variant {
+ PARAM,
+ VAL,
+ VAR
+ }
+}