From 6f4afccfe077fa305d5cabe24c1a133bc8c20fcc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Oct 2023 22:31:24 +0100 Subject: Add proper description of function type --- .../org/shedlang/compiler/types/FunctionType.java | 39 +++++++++- .../shedlang/compiler/types/FunctionTypeTests.java | 90 ++++++++++++++++++++++ 2 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/types/FunctionTypeTests.java (limited to 'src') 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 positionalParams, List 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")); + } +} -- cgit v1.2.3