From c87ffcbab5927c660d339d3b47b75acb50deb085 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Oct 2023 23:18:17 +0100 Subject: Check that main function is present --- .../java/org/shedlang/compiler/ShedCompiler.java | 8 +++- .../shedlang/compiler/config/ConfigChecker.java | 25 ++++++++++ .../shedlang/compiler/config/ExampleConfig.java | 11 ++++- .../compiler/config/errors/ConfigError.java | 11 +++++ .../shedlang/compiler/backends/ExampleTests.java | 22 +++++---- .../compiler/config/ConfigCheckerTests.java | 56 ++++++++++++++++++++++ 6 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/config/ConfigChecker.java create mode 100644 src/main/java/org/shedlang/compiler/config/errors/ConfigError.java create mode 100644 src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java index 98583ed1..6e7f187a 100644 --- a/src/main/java/org/shedlang/compiler/ShedCompiler.java +++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java @@ -2,6 +2,7 @@ package org.shedlang.compiler; import org.shedlang.compiler.ast.untyped.UntypedNamespaceNode; import org.shedlang.compiler.backends.Backend; +import org.shedlang.compiler.config.ConfigChecker; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.parser.Parser; import org.shedlang.compiler.sources.Sources; @@ -9,6 +10,7 @@ import org.shedlang.compiler.stackir.StackCompiler; import org.shedlang.compiler.stackir.StackIrNamespace; import org.shedlang.compiler.typechecker.TypeChecker; import org.shedlang.compiler.typechecker.TypeCheckerContext; +import org.shedlang.compiler.typechecker.TypeInfos; import org.shedlang.compiler.types.Effects; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; @@ -61,10 +63,14 @@ public class ShedCompiler { namespaces.add(stackIrNamespace); } + var typeInfos = typeCheckerContext.toTypeInfos(); + + ConfigChecker.configCheck(config, typeInfos); + backend.compile( config, namespaces, - typeCheckerContext.toTypeInfos(), + typeInfos, config.targetDir().resolve(backend.name()) ); } diff --git a/src/main/java/org/shedlang/compiler/config/ConfigChecker.java b/src/main/java/org/shedlang/compiler/config/ConfigChecker.java new file mode 100644 index 00000000..83ba3867 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/config/ConfigChecker.java @@ -0,0 +1,25 @@ +package org.shedlang.compiler.config; + +import org.shedlang.compiler.config.errors.ConfigError; +import org.shedlang.compiler.typechecker.TypeInfos; + +public class ConfigChecker { + private ConfigChecker() { + } + + public static void configCheck(ProjectConfig projectConfig, TypeInfos typeInfos) { + for (var binConfig : projectConfig.bins()) { + configCheckBin(binConfig, typeInfos); + } + } + + static void configCheckBin(BinConfig binConfig, TypeInfos typeInfos) { + var namespaceTypeInfo = typeInfos.namespaceTypeInfo(binConfig.mainNamespace()) + // TODO: better error + .orElseThrow(); + + if (namespaceTypeInfo.fieldType("main").isEmpty()) { + throw new ConfigError("Namespace " + binConfig.mainNamespace() + " has no field main"); + } + } +} diff --git a/src/main/java/org/shedlang/compiler/config/ExampleConfig.java b/src/main/java/org/shedlang/compiler/config/ExampleConfig.java index 46ca462d..7805feff 100644 --- a/src/main/java/org/shedlang/compiler/config/ExampleConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ExampleConfig.java @@ -2,11 +2,18 @@ package org.shedlang.compiler.config; import org.tomlj.TomlTable; -public record ExampleConfig(String bin, String expectedOutput) { +import java.util.Optional; + +public record ExampleConfig( + String bin, + Optional expectedOutput, + Optional expectedError +) { public static ExampleConfig fromToml(TomlTable table) { return new ExampleConfig( table.getString("bin"), - table.getString("expected-output") + Optional.ofNullable(table.getString("expected-output")), + Optional.ofNullable(table.getString("expected-error")) ); } } diff --git a/src/main/java/org/shedlang/compiler/config/errors/ConfigError.java b/src/main/java/org/shedlang/compiler/config/errors/ConfigError.java new file mode 100644 index 00000000..7d847bb3 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/config/errors/ConfigError.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.config.errors; + +import org.shedlang.compiler.errors.SourceError; +import org.shedlang.compiler.sources.Sources; + +public class ConfigError extends SourceError { + public ConfigError(String message) { + // TODO: proper source + super(message, Sources.NULL); + } +} diff --git a/src/test/java/org/shedlang/compiler/backends/ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/ExampleTests.java index 037041f3..adfdf5d8 100644 --- a/src/test/java/org/shedlang/compiler/backends/ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/ExampleTests.java @@ -31,19 +31,23 @@ public abstract class ExampleTests { var backend = backend(); var compiler = new ShedCompiler(); - try { - compiler.compile(exampleDirectory.toPath(), backend); - } catch (CompilerError error) { - System.err.println(error.source().describe()); - throw error; - } - - var config = ProjectConfig.read(exampleDirectory.toPath()); for (var example : config.examples()) { + try { + compiler.compile(exampleDirectory.toPath(), backend); + } catch (CompilerError error) { + if (example.expectedError().isPresent()) { + assertThat(error.getMessage(), equalTo(example.expectedError().get())); + return; + } + + System.err.println(error.source().describe()); + throw error; + } + var binPath = config.targetDir().resolve(backend.name()).resolve(example.bin()).toString(); var result = runExampleBinary(binPath).throwOnError(); - assertThat(result.stdout(), equalTo(example.expectedOutput())); + assertThat(result.stdout(), equalTo(example.expectedOutput().orElseThrow())); } } )); diff --git a/src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java b/src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java new file mode 100644 index 00000000..4483c639 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java @@ -0,0 +1,56 @@ +package org.shedlang.compiler.config; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.config.errors.ConfigError; +import org.shedlang.compiler.typechecker.TypeInfos; +import org.shedlang.compiler.types.*; +import org.shedlang.compiler.util.P; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class ConfigCheckerTests { + @Test + public void whenMainFunctionExistsWithCorrectTypeThenBinConfigIsValid() throws IOException { + var binConfig = new BinConfig("x", NamespaceName.of("Main")); + var typeInfos = TypeInfos.EMPTY + .addNamespaceTypeInfo( + NamespaceName.of("Main"), + NamespaceTypeInfo.fromFields(P.orderedMap( + Map.entry( + "main", + new FunctionType(List.of(), List.of(), Effects.IO, Types.UNIT) + ) + )) + ); + + ConfigChecker.configCheckBin(binConfig, typeInfos); + } + + @Test + public void whenMainFunctionIsMissingThenBinConfigIsInvalid() throws IOException { + var binConfig = new BinConfig("x", NamespaceName.of("Main")); + var typeInfos = TypeInfos.EMPTY + .addNamespaceTypeInfo( + NamespaceName.of("Main"), + NamespaceTypeInfo.fromFields(P.orderedMap( + Map.entry( + "main2", + new FunctionType(List.of(), List.of(), Effects.IO, Types.UNIT) + ) + )) + ); + + var error = assertThrows( + ConfigError.class, + () -> ConfigChecker.configCheckBin(binConfig, typeInfos) + ); + + assertThat(error.getMessage(), equalTo("Namespace Main has no field main")); + } +} -- cgit v1.2.3