aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-20 19:55:13 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-20 19:55:13 +0000
commit33888c1bac8a1e58fae55f275a0d0eafd16144c5 (patch)
treedb148f62366b4b84fab50f4f6b19ffa5f2e09f05 /src/main/java
parent992336f41a4f6881f2a43e61e479f66e062609fd (diff)
Introduce explicit notion of a BoundVariable
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/BoundVariable.java7
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedBlockNode.java8
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionBodyNode.java4
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionDefinitionNode.java26
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionExpressionNode.java24
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedTryHandlerMethodNode.java22
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java18
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java4
8 files changed, 61 insertions, 52 deletions
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/BoundVariable.java b/src/main/java/org/shedlang/compiler/ast/typed/BoundVariable.java
new file mode 100644
index 00000000..dcd2d113
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/ast/typed/BoundVariable.java
@@ -0,0 +1,7 @@
+package org.shedlang.compiler.ast.typed;
+
+public record BoundVariable(String name) {
+ public static BoundVariable immutable(String name) {
+ return new BoundVariable(name);
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedBlockNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedBlockNode.java
index 9753f974..2b06755c 100644
--- a/src/main/java/org/shedlang/compiler/ast/typed/TypedBlockNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedBlockNode.java
@@ -8,13 +8,13 @@ import org.shedlang.compiler.util.Lists;
import java.util.List;
/**
- * @param boundVariableNames The names of variables that are bound in the
- * block's scope, not including any children scopes
- * such as the bodies of <code>if</code> expressions.
+ * @param boundVariables The variables that are bound in the block's scope, not
+ * including any children scopes such as the bodies of
+ * <code>if</code> expressions.
*/
public record TypedBlockNode(
List<? extends TypedFunctionStatementNode> statements,
- List<String> boundVariableNames,
+ List<BoundVariable> boundVariables,
Source source
) implements TypedNode {
public TypedFunctionStatementBehaviour behaviour() {
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionBodyNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionBodyNode.java
index 01225d71..cae9cf43 100644
--- a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionBodyNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionBodyNode.java
@@ -11,8 +11,8 @@ public record TypedFunctionBodyNode(
Type type,
Source source
) implements TypedNode {
- public List<String> boundVariableNames() {
- return block.boundVariableNames();
+ public List<BoundVariable> boundVariables() {
+ return block.boundVariables();
}
public TypedFunctionStatementBehaviour behaviour() {
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionDefinitionNode.java
index eb664e71..c1a28516 100644
--- a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionDefinitionNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionDefinitionNode.java
@@ -30,7 +30,7 @@ public record TypedFunctionDefinitionNode(
String name,
TypedFunctionSignatureNode.Builder signature,
FreeVariables.Builder freeVariables,
- PVector<String> boundVariableNames,
+ PVector<BoundVariable> boundVariables,
PVector<TypedFunctionStatementNode> body,
FunctionType type
) {
@@ -40,7 +40,7 @@ public record TypedFunctionDefinitionNode(
signature.build(),
new TypedFunctionBodyNode(
freeVariables.build(),
- new TypedBlockNode(body, boundVariableNames, Sources.NULL),
+ new TypedBlockNode(body, boundVariables, Sources.NULL),
Types.UNIT,
Sources.NULL
),
@@ -54,7 +54,7 @@ public record TypedFunctionDefinitionNode(
name,
this.signature,
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
@@ -65,10 +65,10 @@ public record TypedFunctionDefinitionNode(
this.name,
this.signature.addPositionalParam(name, type),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
- ).addBoundVariable(name);
+ ).addBoundVariable(BoundVariable.immutable(name));
}
public TypedFunctionDefinitionNode.Builder addNamedParam(String name, TypedTypeLevelExpressionNode<Type> type) {
@@ -76,10 +76,10 @@ public record TypedFunctionDefinitionNode(
this.name,
this.signature.addNamedParam(name, type),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
- ).addBoundVariable(name);
+ ).addBoundVariable(BoundVariable.immutable(name));
}
public TypedFunctionDefinitionNode.Builder withEffect(TypedTypeLevelExpressionNode<Effect> effect) {
@@ -87,7 +87,7 @@ public record TypedFunctionDefinitionNode(
this.name,
this.signature.withEffect(effect),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
@@ -98,7 +98,7 @@ public record TypedFunctionDefinitionNode(
this.name,
this.signature.withReturnType(returnType),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
@@ -109,18 +109,18 @@ public record TypedFunctionDefinitionNode(
this.name,
this.signature,
this.freeVariables.plus(freeVariable, Sources.NULL),
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
}
- public Builder addBoundVariable(String name) {
+ public Builder addBoundVariable(BoundVariable boundVariable) {
return new TypedFunctionDefinitionNode.Builder(
this.name,
this.signature,
this.freeVariables,
- this.boundVariableNames.plus(name),
+ this.boundVariables.plus(boundVariable),
this.body,
this.type
);
@@ -131,7 +131,7 @@ public record TypedFunctionDefinitionNode(
this.name,
this.signature,
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body.plus(statement),
this.type
);
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionExpressionNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionExpressionNode.java
index e11c35d0..fd53a26d 100644
--- a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionExpressionNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionExpressionNode.java
@@ -27,7 +27,7 @@ public record TypedFunctionExpressionNode(
public record Builder(
TypedFunctionSignatureNode.Builder signature,
FreeVariables.Builder freeVariables,
- PVector<String> boundVariableNames,
+ PVector<BoundVariable> boundVariables,
PVector<TypedFunctionStatementNode> body,
FunctionType type
) {
@@ -36,7 +36,7 @@ public record TypedFunctionExpressionNode(
signature.build(),
new TypedFunctionBodyNode(
freeVariables.build(),
- new TypedBlockNode(body, boundVariableNames, Sources.NULL),
+ new TypedBlockNode(body, boundVariables, Sources.NULL),
Types.UNIT,
Sources.NULL
),
@@ -49,27 +49,27 @@ public record TypedFunctionExpressionNode(
return new Builder(
this.signature.addPositionalParam(name, type),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
- ).addBoundVariable(name);
+ ).addBoundVariable(BoundVariable.immutable(name));
}
public Builder addNamedParam(String name, TypedTypeLevelExpressionNode<Type> type) {
return new Builder(
this.signature.addNamedParam(name, type),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
- ).addBoundVariable(name);
+ ).addBoundVariable(BoundVariable.immutable(name));
}
public Builder withEffect(TypedTypeLevelExpressionNode<Effect> effect) {
return new Builder(
this.signature.withEffect(effect),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
@@ -79,7 +79,7 @@ public record TypedFunctionExpressionNode(
return new Builder(
this.signature.withReturnType(returnType),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
@@ -89,17 +89,17 @@ public record TypedFunctionExpressionNode(
return new Builder(
this.signature,
this.freeVariables.plus(freeVariable, Sources.NULL),
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
}
- public Builder addBoundVariable(String name) {
+ public Builder addBoundVariable(BoundVariable boundVariable) {
return new Builder(
this.signature,
this.freeVariables,
- this.boundVariableNames.plus(name),
+ this.boundVariables.plus(boundVariable),
this.body,
this.type
);
@@ -109,7 +109,7 @@ public record TypedFunctionExpressionNode(
return new Builder(
this.signature,
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body.plus(statement),
this.type
);
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedTryHandlerMethodNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedTryHandlerMethodNode.java
index 14253c67..e9cbd4d8 100644
--- a/src/main/java/org/shedlang/compiler/ast/typed/TypedTryHandlerMethodNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedTryHandlerMethodNode.java
@@ -30,7 +30,7 @@ public record TypedTryHandlerMethodNode(
String name,
TypedFunctionSignatureNode.Builder signature,
FreeVariables.Builder freeVariables,
- PVector<String> boundVariableNames,
+ PVector<BoundVariable> boundVariables,
PVector<TypedFunctionStatementNode> body,
FunctionType type
) {
@@ -40,7 +40,7 @@ public record TypedTryHandlerMethodNode(
signature.build().params(),
new TypedFunctionBodyNode(
freeVariables.build(),
- new TypedBlockNode(body, boundVariableNames, Sources.NULL),
+ new TypedBlockNode(body, boundVariables, Sources.NULL),
Types.UNIT,
Sources.NULL
),
@@ -54,7 +54,7 @@ public record TypedTryHandlerMethodNode(
name,
this.signature,
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
@@ -65,10 +65,10 @@ public record TypedTryHandlerMethodNode(
this.name,
this.signature.addPositionalParam(name, type),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
- ).addBoundVariable(name);
+ ).addBoundVariable(BoundVariable.immutable(name));
}
public TypedTryHandlerMethodNode.Builder addNamedParam(String name, TypedTypeLevelExpressionNode<Type> type) {
@@ -76,10 +76,10 @@ public record TypedTryHandlerMethodNode(
this.name,
this.signature.addNamedParam(name, type),
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
- ).addBoundVariable(name);
+ ).addBoundVariable(BoundVariable.immutable(name));
}
public Builder addFreeVariable(String freeVariable) {
@@ -87,18 +87,18 @@ public record TypedTryHandlerMethodNode(
this.name,
this.signature,
this.freeVariables.plus(freeVariable, Sources.NULL),
- this.boundVariableNames,
+ this.boundVariables,
this.body,
this.type
);
}
- public Builder addBoundVariable(String name) {
+ public Builder addBoundVariable(BoundVariable boundVariable) {
return new TypedTryHandlerMethodNode.Builder(
this.name,
this.signature,
this.freeVariables,
- this.boundVariableNames.plus(name),
+ this.boundVariables.plus(boundVariable),
this.body,
this.type
);
@@ -109,7 +109,7 @@ public record TypedTryHandlerMethodNode(
this.name,
this.signature,
this.freeVariables,
- this.boundVariableNames,
+ this.boundVariables,
this.body.plus(statement),
this.type
);
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index 8110e6da..885e26e4 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -217,7 +217,7 @@ public class StackCompiler {
Source source,
Context context
) {
- context = context.enterBlock(body.boundVariableNames());
+ context = context.enterBlock(body.boundVariables());
var c = context;
@@ -355,7 +355,7 @@ public class StackCompiler {
Context context
) {
if (conditionalBranches.isEmpty()) {
- context = context.enterBlock(elseBody.boundVariableNames());
+ context = context.enterBlock(elseBody.boundVariables());
context = compileBlockWithYield(elseBody, context);
var elseInstructions = context.instructions;
context = context.exitBlock();
@@ -366,7 +366,7 @@ public class StackCompiler {
var conditionalBranch = conditionalBranches.get(0);
context = compileExpression(conditionalBranch.condition(), context);
- context = context.enterBlock(conditionalBranch.body().boundVariableNames());
+ context = context.enterBlock(conditionalBranch.body().boundVariables());
context = compileBlockWithYield(conditionalBranch.body(), context);
var ifTrueInstructions = context.instructions;
context = context.exitBlock();
@@ -622,7 +622,7 @@ public class StackCompiler {
context = compileTryHandlerMethod(handlerMethod, context);
}
- context = context.enterBlock(node.body().boundVariableNames());
+ context = context.enterBlock(node.body().boundVariables());
context = compileBlockWithYield(node.body(), context);
var bodyInstructions = context.instructions;
context = context.exitBlock();
@@ -674,7 +674,7 @@ public class StackCompiler {
var switchCases = new ArrayList<SwitchInstruction.Case>();
for (var whenCase : node.cases()) {
- context = context.enterBlock(whenCase.body().boundVariableNames());
+ context = context.enterBlock(whenCase.body().boundVariables());
context = compileBlockWithYield(whenCase.body(), context);
var caseBodyInstructions = context.instructions;
context = context.exitBlock();
@@ -696,7 +696,7 @@ public class StackCompiler {
}
private static Context compileWhile(TypedWhileNode node, Context context) {
- context = context.enterBlock(node.body().boundVariableNames());
+ context = context.enterBlock(node.body().boundVariables());
context = compileBlockWithoutYield(node.body(), context);
var ifTrueInstructions = context.instructions;
context = context.exitBlock();
@@ -754,7 +754,7 @@ public class StackCompiler {
this.shedVariablesByName = shedVariablesByName;
}
- public Context enterBlock(List<String> shedVariableNames) {
+ public Context enterBlock(List<BoundVariable> shedVariables) {
var context = new Context(
this.instructionsStack.plus(this.instructions),
P.vector(),
@@ -763,8 +763,8 @@ public class StackCompiler {
this.shedVariablesByName
);
- for (var shedVariableName : shedVariableNames) {
- context = context.addShedVariable(shedVariableName);
+ for (var shedVariable : shedVariables) {
+ context = context.addShedVariable(shedVariable.name());
}
return context;
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index 9963ee56..1ea3b680 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -225,7 +225,9 @@ public class TypeChecker {
var typed = new TypedBlockNode(
typedStatements,
- declaredVariableNames,
+ declaredVariableNames.stream()
+ .map(BoundVariable::new)
+ .toList(),
untyped.source()
);