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 ++++++++++ 4 files changed, 52 insertions(+), 3 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 (limited to 'src/main/java') 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); + } +} -- cgit v1.2.3