aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-31 22:25:53 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-31 22:25:53 +0100
commitfa5df9169c64e6b7abaf6ec7825c4788d7638bc4 (patch)
tree9f05afddae5f91f5dfee4b08c32ae8e8428fae77 /src/main/java
parentee3d75f1f230ae05803db83ce387eb8b79bc1d95 (diff)
Turn Variable into a record
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/EnvironmentBuilder.java2
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java28
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java46
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/Variable.java16
4 files changed, 35 insertions, 57 deletions
diff --git a/src/main/java/org/shedlang/compiler/typechecker/EnvironmentBuilder.java b/src/main/java/org/shedlang/compiler/typechecker/EnvironmentBuilder.java
index c30c745a..284b4d31 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/EnvironmentBuilder.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/EnvironmentBuilder.java
@@ -31,7 +31,7 @@ public class EnvironmentBuilder {
if (environment.containsKey(name)) {
throw new DuplicateVariableName(name, source);
}
- return new EnvironmentBuilder(environment.plus(name, new Variable.Undefined()));
+ return new EnvironmentBuilder(environment.plus(name, Variable.undefined()));
}
public EnvironmentBuilder add(String name, Variable variable, Source source) {
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index 5377f562..499f41a3 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -42,34 +42,24 @@ public class TypeChecker {
}
private static Type compileTimeLookup(String name, Source source, TypeCheckerContext context) {
- var type = context.compileTimeLookup(name);
+ var variable = context.compileTimeLookup(name);
- if (type.isEmpty()) {
+ if (variable.isEmpty()) {
throw new UnknownVariableError(name, source);
}
- return switch (type.get()) {
- case Variable.Defined defined ->
- defined.type();
-
- case Variable.Undefined undefined ->
- throw new ReferenceBeforeDefinitionError(name, source);
- };
+ return variable.get().type()
+ .orElseThrow(() -> new ReferenceBeforeDefinitionError(name, source));
}
private static TypeCheckerResult<Type> runtimeLookup(String name, Source source, TypeCheckerContext context) {
- return context.runtimeLookup(name, source).map(type -> {
- if (type.isEmpty()) {
+ return context.runtimeLookup(name, source).map(variable -> {
+ if (variable.isEmpty()) {
throw new UnknownVariableError(name, source);
}
- return switch (type.get()) {
- case Variable.Defined defined ->
- defined.type();
-
- case Variable.Undefined undefined ->
- throw new ReferenceBeforeDefinitionError(name, source);
- };
+ return variable.get().type()
+ .orElseThrow(() -> new ReferenceBeforeDefinitionError(name, source));
});
}
@@ -435,7 +425,7 @@ public class TypeChecker {
);
return context
- .declare(untyped.name(), functionType, untyped.source())
+ .define(untyped.name(), functionType, untyped.source())
.addLexicalState(untyped.name(), new TypeCheckFunctionDefinitionState(
functionType,
typedParams,
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java
index 30778094..a4cfedce 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java
@@ -254,15 +254,6 @@ public class TypeCheckerContext {
return clazz.cast(state);
}
- public TypeCheckerContext declare(
- String name,
- Type type,
- Source source
- ) {
- // TODO: differentiation declaration and definition
- return define(name, type, source);
- }
-
public TypeCheckerContext define(
String name,
Type type,
@@ -274,27 +265,24 @@ public class TypeCheckerContext {
throw new UnknownVariableError(name, source);
}
- switch (variable) {
- case Variable.Defined defined -> {
- throw new VariableIsAlreadyDefinedError(name, source);
- }
- case Variable.Undefined undefined -> {
- if (!this.boundVariables().contains(name)) {
- throw new CannotDefineVariableDeclaredInOuterScopeError(name, source);
- }
-
- return this.updateLexicalContext(
- lexicalContext -> new LexicalContext(
- lexicalContext.environment.plus(name, new Variable.Defined(type)),
- lexicalContext.effect,
- lexicalContext.returnType,
- lexicalContext.boundVariables,
- lexicalContext.freeVariables,
- lexicalContext.state
- )
- );
- }
+ if (variable.isDefined()) {
+ throw new VariableIsAlreadyDefinedError(name, source);
+ }
+
+ if (!this.boundVariables().contains(name)) {
+ throw new CannotDefineVariableDeclaredInOuterScopeError(name, source);
}
+
+ return this.updateLexicalContext(
+ lexicalContext -> new LexicalContext(
+ lexicalContext.environment.plus(name, Variable.defined(type)),
+ lexicalContext.effect,
+ lexicalContext.returnType,
+ lexicalContext.boundVariables,
+ lexicalContext.freeVariables,
+ lexicalContext.state
+ )
+ );
}
private PMap<String, Variable> environment() {
diff --git a/src/main/java/org/shedlang/compiler/typechecker/Variable.java b/src/main/java/org/shedlang/compiler/typechecker/Variable.java
index 7fd7ca23..6c6be749 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/Variable.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/Variable.java
@@ -2,18 +2,18 @@ package org.shedlang.compiler.typechecker;
import org.shedlang.compiler.types.Type;
-public sealed interface Variable {
- static Variable defined(Type type) {
- return new Variable.Defined(type);
- }
+import java.util.Optional;
- static Variable undefined() {
- return new Variable.Undefined();
+public record Variable(Optional<Type> type) {
+ public static Variable defined(Type type) {
+ return new Variable(Optional.of(type));
}
- record Undefined() implements Variable {
+ public static Variable undefined() {
+ return new Variable(Optional.empty());
}
- record Defined(Type type) implements Variable {
+ public boolean isDefined() {
+ return type.isPresent();
}
}