aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-22 22:31:24 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-22 22:31:24 +0100
commit6f4afccfe077fa305d5cabe24c1a133bc8c20fcc (patch)
treeda541836a8e1a1fe258773df642238e090de97f6 /src
parentd1d0984bc8a157a2f001d3c991dae5c9498cd790 (diff)
Add proper description of function type
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/types/FunctionType.java39
-rw-r--r--src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java90
2 files changed, 127 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/types/FunctionType.java b/src/main/java/org/shedlang/compiler/types/FunctionType.java
index e40439f7..c52de92e 100644
--- a/src/main/java/org/shedlang/compiler/types/FunctionType.java
+++ b/src/main/java/org/shedlang/compiler/types/FunctionType.java
@@ -5,6 +5,8 @@ import org.pcollections.PVector;
import java.util.List;
+import static org.shedlang.compiler.util.Iterables.forEachInterspersed;
+
public record FunctionType(
List<Type> positionalParams,
List<NamedParam> namedParams,
@@ -17,8 +19,41 @@ public record FunctionType(
@Override
public String describe() {
- // TODO: implement this sensibly
- return toString();
+ var description = new StringBuilder();
+ description.append("fn (");
+
+ forEachInterspersed(
+ positionalParams,
+ param -> description.append(param.describe()),
+ () -> description.append(", ")
+ );
+
+ if (!positionalParams.isEmpty() && !namedParams.isEmpty()) {
+ description.append(", ");
+ }
+
+ forEachInterspersed(
+ namedParams,
+ param -> {
+ description.append(".");
+ description.append(param.name());
+ description.append(": ");
+ description.append(param.type().describe());
+ },
+ () -> description.append(", ")
+ );
+
+ description.append(")");
+
+ if (!effect.equals(Effects.NONE)) {
+ description.append(" ! ");
+ description.append(effect.describe());
+ }
+
+ description.append(" -> ");
+ description.append(returnType.describe());
+
+ return description.toString();
}
public record Builder(
diff --git a/src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java b/src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java
new file mode 100644
index 00000000..d182ce19
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java
@@ -0,0 +1,90 @@
+package org.shedlang.compiler.types;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class FunctionTypeTests {
+ @Test
+ public void descriptionIncludesReturnType() {
+ var functionType = new FunctionType(
+ List.of(),
+ List.of(),
+ Effects.NONE,
+ Types.STRING
+ );
+
+ var result = functionType.describe();
+
+ assertThat(result, equalTo("fn () -> String"));
+ }
+
+ @Test
+ public void descriptionIncludesPositionalParams() {
+ var functionType = new FunctionType(
+ List.of(
+ Types.INT,
+ Types.BOOL
+ ),
+ List.of(),
+ Effects.NONE,
+ Types.STRING
+ );
+
+ var result = functionType.describe();
+
+ assertThat(result, equalTo("fn (Int, Bool) -> String"));
+ }
+
+ @Test
+ public void descriptionIncludesNamedParams() {
+ var functionType = new FunctionType(
+ List.of(),
+ List.of(
+ new NamedParam("a", Types.INT),
+ new NamedParam("b", Types.BOOL)
+ ),
+ Effects.NONE,
+ Types.STRING
+ );
+
+ var result = functionType.describe();
+
+ assertThat(result, equalTo("fn (.a: Int, .b: Bool) -> String"));
+ }
+
+ @Test
+ public void positionalParamsAndNamedParamsAreCommaSeparated() {
+ var functionType = new FunctionType(
+ List.of(
+ Types.INT
+ ),
+ List.of(
+ new NamedParam("b", Types.BOOL)
+ ),
+ Effects.NONE,
+ Types.STRING
+ );
+
+ var result = functionType.describe();
+
+ assertThat(result, equalTo("fn (Int, .b: Bool) -> String"));
+ }
+
+ @Test
+ public void descriptionIncludesEffect() {
+ var functionType = new FunctionType(
+ List.of(),
+ List.of(),
+ Effects.IO,
+ Types.STRING
+ );
+
+ var result = functionType.describe();
+
+ assertThat(result, equalTo("fn () ! Io -> String"));
+ }
+}