diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-11-20 20:06:04 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-11-20 20:06:21 +0000 |
| commit | 23c76b64213fbe54dafd5bbf6b9d10efb051d16d (patch) | |
| tree | abe5d1ad67570eb270fe8ecae9049363c29b69e9 /src/main/java | |
| parent | 5a9bf0b81e43a747fccd59dedaeb8432431e4e1a (diff) | |
Mark mutability of variables up-front
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/stackir/StackCompiler.java | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 885e26e4..c46e9368 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -398,7 +398,7 @@ public class StackCompiler { public static StackIrNamespace compileNamespace(TypedNamespaceNode node) { var context = Context.EMPTY; - context = context.addShedVariable(ShedBuiltins.PRINT_NAME); + context = context.addShedVal(ShedBuiltins.PRINT_NAME); var printLocalName = context.shedVariableToLocal(ShedBuiltins.PRINT_NAME); context = context @@ -411,7 +411,7 @@ public class StackCompiler { .addInstruction(new LocalStoreInstruction(printLocalName, node.source())); for (var boundVariableName : node.boundVariableNames()) { - context = context.addShedVariable(boundVariableName); + context = context.addShedVal(boundVariableName); } for (var importNode : node.imports()) { @@ -609,9 +609,6 @@ public class StackCompiler { ) ); - // TODO: better to store this information in the block node with the bound variables? - context = context.markShedVariableAsVar(var.name()); - yield context; } }; @@ -764,7 +761,7 @@ public class StackCompiler { ); for (var shedVariable : shedVariables) { - context = context.addShedVariable(shedVariable.name()); + context = context.addShedVariable(shedVariable); } return context; @@ -814,27 +811,19 @@ public class StackCompiler { return shedVariable; } - public Context addShedVariable(String variableName) { - var localName = generateLocalNameForVariable(variableName); - - return new Context( - this.instructionsStack, - this.instructions, - this.localNames.plus(localName), - this.shedVariablesByNameStack, - this.shedVariablesByName.plus(variableName, new ShedVariable(localName, false)) - ); + public Context addShedVal(String variableName) { + return addShedVariable(BoundVariable.immutable(variableName)); } - public Context markShedVariableAsVar(String variableName) { - var localName = shedVariableToLocal(variableName); + public Context addShedVariable(BoundVariable boundVariable) { + var localName = generateLocalNameForVariable(boundVariable.name()); return new Context( this.instructionsStack, this.instructions, - this.localNames, + this.localNames.plus(localName), this.shedVariablesByNameStack, - this.shedVariablesByName.plus(variableName, new ShedVariable(localName, true)) + this.shedVariablesByName.plus(boundVariable.name(), new ShedVariable(localName, boundVariable.isMutable())) ); } |
