diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-22 23:18:17 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-22 23:18:17 +0100 |
| commit | c87ffcbab5927c660d339d3b47b75acb50deb085 (patch) | |
| tree | d1bcebfdc82565179fd8718bea37c1dae6bca3b8 /src | |
| parent | 9c35257a360c8bea20e766b44da7eb4897a223b3 (diff) | |
Check that main function is present
Diffstat (limited to 'src')
6 files changed, 121 insertions, 12 deletions
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<String> expectedOutput, + Optional<String> 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")); + } +} |
