From 7c6fc48c7ab8a23f8568a2f0c57c66e77a5aa69c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 13 Dec 2023 23:34:49 +0000 Subject: Use Shid bound variables to generate Patio variables --- .../org/shedlang/compiler/shid/ShidCodeGen.java | 46 ++++++++++++---------- .../org/shedlang/compiler/shid/ShidSerializer.java | 22 ++++++++--- .../compiler/shid/instructions/ShidBlock.java | 2 +- .../compiler/shid/instructions/ShidVariable.java | 13 ++++++ 4 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/shid/instructions/ShidVariable.java (limited to 'src/main/java/org/shedlang/compiler/shid') 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 instructions; - private final PMap shedVariablesByName; - private final PVector boundLocals; + private final PMap shedVariablesByName; + private final PVector boundVariables; private final Source source; public BlockContext( PVector instructions, - PMap shedVariablesByName, - PVector boundLocals, + PMap shedVariablesByName, + PVector 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 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 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 boundLocals, + List boundVariables, List 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 + } +} -- cgit v1.2.3