From 3f3a1f963e986fe61e353b5bdf9b1ec7fb384504 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 19 Nov 2023 22:51:03 +0000 Subject: Extract isMutable() methods --- .../org/shedlang/compiler/typechecker/TypeChecker.java | 18 +++++------------- .../org/shedlang/compiler/typechecker/Variable.java | 4 ++++ .../shedlang/compiler/typechecker/VariableType.java | 12 +++++++++++- 3 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index e9dec032..9963ee56 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -644,19 +644,11 @@ public class TypeChecker { freeVariable.firstReferenceSource() )); - if (!signature.isEffectHandlerMethod()) { - switch (variable.variableType()) { - case CONSTANT -> {} - case IMPORT -> {} - case PARAM -> {} - case VAL -> {} - - case VAR -> - throw new CannotCaptureVarError( - freeVariable.name(), - freeVariable.firstReferenceSource() - ); - }; + if (!signature.isEffectHandlerMethod() && variable.isMutable()) { + throw new CannotCaptureVarError( + freeVariable.name(), + freeVariable.firstReferenceSource() + ); } } diff --git a/src/main/java/org/shedlang/compiler/typechecker/Variable.java b/src/main/java/org/shedlang/compiler/typechecker/Variable.java index fc06acc9..264942a8 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/Variable.java +++ b/src/main/java/org/shedlang/compiler/typechecker/Variable.java @@ -16,4 +16,8 @@ public record Variable(Optional type, VariableType variableType) { public boolean hasType() { return type.isPresent(); } + + public boolean isMutable() { + return variableType.isMutable(); + } } diff --git a/src/main/java/org/shedlang/compiler/typechecker/VariableType.java b/src/main/java/org/shedlang/compiler/typechecker/VariableType.java index 64652060..a6cd12df 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/VariableType.java +++ b/src/main/java/org/shedlang/compiler/typechecker/VariableType.java @@ -5,5 +5,15 @@ public enum VariableType { IMPORT, PARAM, VAL, - VAR, + VAR; + + boolean isMutable() { + return switch (this) { + case CONSTANT -> false; + case IMPORT -> false; + case PARAM -> false; + case VAL -> false; + case VAR -> true; + }; + } } -- cgit v1.2.3