From 33888c1bac8a1e58fae55f275a0d0eafd16144c5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 20 Nov 2023 19:55:13 +0000 Subject: Introduce explicit notion of a BoundVariable --- .../shedlang/compiler/ast/typed/BoundVariable.java | 7 ++++++ .../compiler/ast/typed/TypedBlockNode.java | 8 +++---- .../compiler/ast/typed/TypedFunctionBodyNode.java | 4 ++-- .../ast/typed/TypedFunctionDefinitionNode.java | 26 +++++++++++----------- .../ast/typed/TypedFunctionExpressionNode.java | 24 ++++++++++---------- .../ast/typed/TypedTryHandlerMethodNode.java | 22 +++++++++--------- .../shedlang/compiler/stackir/StackCompiler.java | 18 +++++++-------- .../shedlang/compiler/typechecker/TypeChecker.java | 4 +++- .../org/shedlang/compiler/ast/typed/Typed.java | 4 ++-- .../compiler/ast/typed/TypedNodeMatchers.java | 6 ++--- .../compiler/stackir/StackCompilerValTests.java | 5 +++-- .../typechecker/TypeCheckerFunctionBodyTests.java | 9 ++++---- .../compiler/typechecker/TypeCheckerIfTests.java | 9 ++++---- 13 files changed, 79 insertions(+), 67 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/ast/typed/BoundVariable.java (limited to 'src') 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 if expressions. + * @param boundVariables The variables that are bound in the block's scope, not + * including any children scopes such as the bodies of + * if expressions. */ public record TypedBlockNode( List statements, - List boundVariableNames, + List 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 boundVariableNames() { - return block.boundVariableNames(); + public List 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 boundVariableNames, + PVector boundVariables, PVector 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) { @@ -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) { @@ -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 boundVariableNames, + PVector boundVariables, PVector 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) { 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) { 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 boundVariableNames, + PVector boundVariables, PVector 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) { @@ -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(); 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 shedVariableNames) { + public Context enterBlock(List 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() ); 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 717e94d0..81958cbe 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -23,9 +23,9 @@ public class Typed { public static TypedBlockNode block( List statements, - List boundVariableNames + List boundVariables ) { - return new TypedBlockNode(statements, boundVariableNames, Sources.NULL); + return new TypedBlockNode(statements, boundVariables, Sources.NULL); } public static TypedBlockReturnNode blockReturn(TypedExpressionNode expression) { diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java index faf6f31c..bb787024 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java @@ -46,10 +46,10 @@ public class TypedNodeMatchers { return has("statements", x -> x.statements(), statements); } - public static Matcher isTypedBlockNodeWithBoundVariableNames( - Matcher> boundVariableNames + public static Matcher isTypedBlockNodeWithBoundVariables( + Matcher> boundVariables ) { - return has("boundVariableNames", x -> x.boundVariableNames(), boundVariableNames); + return has("boundVariables", x -> x.boundVariables(), boundVariables); } public static Matcher isTypedBlockReturnNode( diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java index b72055fd..4d0e1451 100644 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.stackir; import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.BoundVariable; import org.shedlang.compiler.ast.typed.Typed; import org.shedlang.compiler.types.Types; @@ -53,7 +54,7 @@ public class StackCompilerValTests { Typed.val("x", Typed.intLiteral(11)), Typed.expressionStatement(Typed.reference("x")) ), - List.of("x") + List.of(BoundVariable.immutable("x")) ) ) ), @@ -62,7 +63,7 @@ public class StackCompilerValTests { Typed.val("x", Typed.intLiteral(12)), Typed.expressionStatement(Typed.reference("x")) ), - List.of("x") + List.of(BoundVariable.immutable("x")) ), Types.INT )), diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java index c6abd22a..6ac002b1 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionBodyTests.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.UntypedCallNode; import org.shedlang.compiler.typechecker.errors.CannotCaptureVarError; @@ -504,10 +505,10 @@ public class TypeCheckerFunctionBodyTests { assertThat( typed.value(), isTypedFunctionBodyNode() - .withBlock(isTypedBlockNodeWithBoundVariableNames(containsExactly( - equalTo("argPositional"), - equalTo("argNamed"), - equalTo("local1") + .withBlock(isTypedBlockNodeWithBoundVariables(containsExactly( + equalTo(BoundVariable.immutable("argPositional")), + equalTo(BoundVariable.immutable("argNamed")), + equalTo(BoundVariable.immutable("local1")) ))) ); } diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.java index 0cbca567..25347042 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.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.typechecker.errors.UnexpectedTypeError; import org.shedlang.compiler.typechecker.typesets.TypeSets; @@ -176,13 +177,13 @@ public class TypeCheckerIfTests { isSequence( isTypedConditionalBranchNode( anything(), - isTypedBlockNodeWithBoundVariableNames(containsExactly( - equalTo("local1") + isTypedBlockNodeWithBoundVariables(containsExactly( + equalTo(BoundVariable.immutable("local1")) )) ) ), - isTypedBlockNodeWithBoundVariableNames(containsExactly( - equalTo("local2") + isTypedBlockNodeWithBoundVariables(containsExactly( + equalTo(BoundVariable.immutable("local2")) )) ) ); -- cgit v1.2.3