aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-19 22:51:03 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-19 22:51:03 +0000
commit3f3a1f963e986fe61e353b5bdf9b1ec7fb384504 (patch)
treed6f018f9c0ce21c53029be5245fa1e766e942515 /src/main/java
parentcb2d43a2462b6fefd646bbfeae3268b3ef59066d (diff)
Extract isMutable() methods
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java18
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/Variable.java4
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/VariableType.java12
3 files changed, 20 insertions, 14 deletions
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> 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;
+ };
+ }
}