aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-20 20:10:02 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-20 20:10:02 +0000
commit51a0d5a8b6db54368872dc3df9ade204f47c59c7 (patch)
tree10f6640f1f924cb9fea737c122eb0b6268f183a9 /src
parent23c76b64213fbe54dafd5bbf6b9d10efb051d16d (diff)
Store mutability of bound variables in namespace node
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedNamespaceNode.java12
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java4
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java4
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java5
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java4
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java4
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java8
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java6
8 files changed, 29 insertions, 18 deletions
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedNamespaceNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedNamespaceNode.java
index abc325ce..8894fc64 100644
--- a/src/main/java/org/shedlang/compiler/ast/typed/TypedNamespaceNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedNamespaceNode.java
@@ -12,7 +12,7 @@ public record TypedNamespaceNode(
NamespaceName namespaceName,
List<TypedImportNode> imports,
List<TypedNamespaceStatementNode> body,
- List<String> boundVariableNames,
+ List<BoundVariable> boundVariables,
List<String> fieldNames,
Source source
) implements TypedNode {
@@ -30,7 +30,7 @@ public record TypedNamespaceNode(
NamespaceName namespaceName,
PVector<TypedImportNode> imports,
PVector<TypedNamespaceStatementNode> body,
- PVector<String> boundVariableNames,
+ PVector<BoundVariable> boundVariables,
PVector<String> fieldNames
) {
public TypedNamespaceNode build() {
@@ -38,7 +38,7 @@ public record TypedNamespaceNode(
namespaceName,
imports,
body,
- boundVariableNames,
+ boundVariables,
fieldNames,
Sources.NULL
);
@@ -49,17 +49,17 @@ public record TypedNamespaceNode(
this.namespaceName,
this.imports.plus(import_),
this.body,
- this.boundVariableNames,
+ this.boundVariables,
this.fieldNames
);
}
- public Builder addBoundVariableName(String variableName) {
+ public Builder addBoundVariable(BoundVariable boundVariable) {
return new Builder(
this.namespaceName,
this.imports,
this.body,
- this.boundVariableNames.plus(variableName),
+ this.boundVariables.plus(boundVariable),
this.fieldNames
);
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index c46e9368..a6da0635 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -410,8 +410,8 @@ public class StackCompiler {
))
.addInstruction(new LocalStoreInstruction(printLocalName, node.source()));
- for (var boundVariableName : node.boundVariableNames()) {
- context = context.addShedVal(boundVariableName);
+ for (var boundVariable : node.boundVariables()) {
+ context = context.addShedVariable(boundVariable);
}
for (var importNode : node.imports()) {
diff --git a/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java b/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java
index a19916aa..8258208d 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/DeclaredVariable.java
@@ -21,4 +21,8 @@ public record DeclaredVariable(
node.source()
);
}
+
+ public boolean isMutable() {
+ return variableType().isMutable();
+ }
}
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index 27210ec5..ca8f89ec 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -1233,7 +1233,10 @@ public class TypeChecker {
typedImportNodes,
typedBody,
declaredVariables.stream()
- .map(boundVariable -> boundVariable.name())
+ .map(boundVariable -> new BoundVariable(
+ boundVariable.name(),
+ boundVariable.isMutable()
+ ))
.toList(),
fieldNames,
untyped.source()
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
index 81958cbe..6ec6c0fb 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
@@ -170,14 +170,14 @@ public class Typed {
public static TypedNamespaceNode namespace(
NamespaceName namespaceName,
List<TypedNamespaceStatementNode> body,
- List<String> boundVariableNames,
+ List<BoundVariable> boundVariables,
List<String> fieldNames
) {
return new TypedNamespaceNode(
namespaceName,
List.of(),
body,
- boundVariableNames,
+ boundVariables,
fieldNames,
Sources.NULL
);
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java
index fbe4a0f9..ae90d3d4 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java
@@ -51,10 +51,10 @@ public class TypedNamespaceNodeMatcher implements Matcher<TypedNamespaceNode> {
}
public Matcher<TypedNamespaceNode> withBoundVariableNames(
- Matcher<Iterable<? extends String>> boundVariableNames
+ Matcher<Iterable<? extends BoundVariable>> boundVariables
) {
return new TypedNamespaceNodeMatcher(
- submatchers.plus(has("boundVariableNames", x -> x.boundVariableNames(), boundVariableNames))
+ submatchers.plus(has("boundVariables", x -> x.boundVariables(), boundVariables))
);
}
}
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java
index e6206b9a..322bf49f 100644
--- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java
@@ -27,7 +27,7 @@ public class StackCompilerNamespaceTests {
.addStatement(Typed.expressionStatement(Typed.stringLiteral("hello")))
.build()
),
- List.of("f"),
+ List.of(BoundVariable.immutable("f")),
List.of("f")
);
@@ -53,9 +53,9 @@ public class StackCompilerNamespaceTests {
public void importsAreLoaded() {
var node = TypedNamespaceNode.builder()
.addImport(Typed.import_(NamespaceName.of("A")))
- .addBoundVariableName("A")
+ .addBoundVariable(BoundVariable.immutable("A"))
.addImport(Typed.import_(NamespaceName.of("B")))
- .addBoundVariableName("B")
+ .addBoundVariable(BoundVariable.immutable("B"))
.build();
var stackIrNamespace = StackCompiler.compileNamespace(node);
@@ -87,7 +87,7 @@ public class StackCompilerNamespaceTests {
.withType(structType)
.build()
),
- List.of("f", "None"),
+ List.of(BoundVariable.immutable("f"), BoundVariable.immutable("None")),
List.of("f", "None")
);
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java
index 82ec14f7..65baadce 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java
@@ -1,6 +1,7 @@
package org.shedlang.compiler.typechecker;
import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.BoundVariable;
import org.shedlang.compiler.ast.untyped.Untyped;
import org.shedlang.compiler.ast.untyped.UntypedFunctionDefinitionNode;
import org.shedlang.compiler.ast.untyped.UntypedNamespaceNode;
@@ -53,7 +54,10 @@ public class TypeCheckerNamespaceTests {
isTypedImportNode(NamespaceName.of("A")),
isTypedImportNode(NamespaceName.of("B"))
))
- .withBoundVariableNames(isSequence(equalTo("A"), equalTo("B")))
+ .withBoundVariableNames(isSequence(
+ equalTo(BoundVariable.immutable("A")),
+ equalTo(BoundVariable.immutable("B"))
+ ))
);
}