From 9e08a99b327c7d704f533e3535b77a1bbdc04919 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 18 Jun 2023 12:15:46 +0100 Subject: Run examples automatically --- examples/hello-world/shed.toml | 7 +++ examples/true/shed.toml | 9 +++- .../compiler/backends/llvm/LlvmBackend.java | 28 ++++++------ .../org/shedlang/compiler/config/BinConfig.java | 11 +++++ .../shedlang/compiler/config/ExampleConfig.java | 12 +++++ .../shedlang/compiler/config/ProjectConfig.java | 18 ++++++++ .../compiler/filesystem/ShedCompilerFiles.java | 13 ++++++ .../compiler/backends/llvm/LlvmExampleTests.java | 52 ++++++++++++++++++++++ 8 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/config/BinConfig.java create mode 100644 src/main/java/org/shedlang/compiler/config/ExampleConfig.java create mode 100644 src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java create mode 100644 src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java diff --git a/examples/hello-world/shed.toml b/examples/hello-world/shed.toml index 6c6fb5cb..156fa7c6 100644 --- a/examples/hello-world/shed.toml +++ b/examples/hello-world/shed.toml @@ -1,2 +1,9 @@ [shed] main = "HelloWorld" + +[[shed.bins]] +name = "hello-world" + +[[shed.examples]] +bin = "hello-world" +expected-output = "hello, world" diff --git a/examples/true/shed.toml b/examples/true/shed.toml index 6c6fb5cb..e508461b 100644 --- a/examples/true/shed.toml +++ b/examples/true/shed.toml @@ -1,2 +1,9 @@ [shed] -main = "HelloWorld" +main = "True" + +[[shed.bins]] +name = "true" + +[[shed.examples]] +bin = "true" +expected-output = "" diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java index c2cf941d..c1ff0c95 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java @@ -4,6 +4,7 @@ import org.shedlang.compiler.backends.Backend; import org.shedlang.compiler.backends.llvm.ir.LlvmIrSerializer; import org.shedlang.compiler.backends.llvm.ir.ast.LlvmModule; import org.shedlang.compiler.config.ProjectConfig; +import org.shedlang.compiler.filesystem.ShedCompilerFiles; import org.shedlang.compiler.stackir.instructions.Instruction; import org.shedlang.compiler.typechecker.TypeInfo; import org.shedlang.compiler.types.NamespaceName; @@ -36,24 +37,25 @@ public class LlvmBackend implements Backend { objectFilePaths.add(objectPath); } - var mainModule = LlvmCompiler.compileMain(config.main(), typeInfo); - var objectPath = compileLlvmModule(mainModule, config.targetDir(), "main"); - objectFilePaths.add(objectPath); + for (var bin : config.bins()) { + var mainModule = LlvmCompiler.compileMain(config.main(), typeInfo); + var objectPath = compileLlvmModule(mainModule, config.targetDir().resolve("bins"), bin.name()); + objectFilePaths.add(objectPath); - var gccArgs = new ArrayList(); - gccArgs.add("gcc"); + var gccArgs = new ArrayList(); + gccArgs.add("gcc"); - gccArgs.add("-o"); - gccArgs.add(config.targetDir().resolve("main").toString()); + gccArgs.add("-o"); + gccArgs.add(config.targetDir().resolve(bin.name()).toString()); - for (var objectFilePath : objectFilePaths) { - gccArgs.add(objectFilePath.toString()); - } + for (var objectFilePath : objectFilePaths) { + gccArgs.add(objectFilePath.toString()); + } - // TODO: don't assume we're in the project root - gccArgs.add("runtime/build/libshed.a"); + gccArgs.add(ShedCompilerFiles.getRoot().resolve("runtime/build/libshed.a").toString()); - run(gccArgs); + run(gccArgs); + } } private static Path compileLlvmModule( diff --git a/src/main/java/org/shedlang/compiler/config/BinConfig.java b/src/main/java/org/shedlang/compiler/config/BinConfig.java new file mode 100644 index 00000000..c3e70f2d --- /dev/null +++ b/src/main/java/org/shedlang/compiler/config/BinConfig.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.config; + +import org.tomlj.TomlTable; + +public record BinConfig(String name) { + public static BinConfig fromToml(TomlTable table) { + var name = table.getString("name"); + + return new BinConfig(name); + } +} diff --git a/src/main/java/org/shedlang/compiler/config/ExampleConfig.java b/src/main/java/org/shedlang/compiler/config/ExampleConfig.java new file mode 100644 index 00000000..46ca462d --- /dev/null +++ b/src/main/java/org/shedlang/compiler/config/ExampleConfig.java @@ -0,0 +1,12 @@ +package org.shedlang.compiler.config; + +import org.tomlj.TomlTable; + +public record ExampleConfig(String bin, String expectedOutput) { + public static ExampleConfig fromToml(TomlTable table) { + return new ExampleConfig( + table.getString("bin"), + table.getString("expected-output") + ); + } +} diff --git a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java index 1db4b87d..1d6a9a9f 100644 --- a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java @@ -7,6 +7,8 @@ import org.tomlj.TomlTable; import java.io.IOException; import java.nio.file.Path; import java.util.Arrays; +import java.util.List; +import java.util.stream.IntStream; public class ProjectConfig { public static ProjectConfig read(Path projectPath) throws IOException { @@ -36,4 +38,20 @@ public class ProjectConfig { public Path targetDir() { return root.resolve("target"); } + + public List bins() { + var binsArray = config.getArray(List.of("shed", "bins")); + + return IntStream.range(0, binsArray.size()) + .mapToObj(binIndex -> BinConfig.fromToml(binsArray.getTable(binIndex))) + .toList(); + } + + public List examples() { + var examplesArray = config.getArray(List.of("shed", "examples")); + + return IntStream.range(0, examplesArray.size()) + .mapToObj(exampleIndex -> ExampleConfig.fromToml(examplesArray.getTable(exampleIndex))) + .toList(); + } } diff --git a/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java b/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java new file mode 100644 index 00000000..62e61567 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.filesystem; + +import java.nio.file.Path; + +public class ShedCompilerFiles { + private ShedCompilerFiles() { + } + + public static Path getRoot() { + // TODO: don't assume we're in the project root + return Path.of("."); + } +} diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java new file mode 100644 index 00000000..6e57665f --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java @@ -0,0 +1,52 @@ +package org.shedlang.compiler.backends.llvm; + +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.shedlang.compiler.ShedCompiler; +import org.shedlang.compiler.config.ProjectConfig; +import org.shedlang.compiler.filesystem.ShedCompilerFiles; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class LlvmExampleTests { + @TestFactory + public Stream tests() { + var examplesRoot = ShedCompilerFiles.getRoot().resolve("examples"); + return Arrays.stream(examplesRoot.toFile() + .listFiles(file -> file.isDirectory())) + .map(exampleDirectory -> DynamicTest.dynamicTest( + exampleDirectory.getName(), + () -> { + var compiler = new ShedCompiler(); + compiler.compile(exampleDirectory.toPath(), new LlvmBackend()); + + var config = ProjectConfig.read(exampleDirectory.toPath()); + for (var example : config.examples()) { + var process = new ProcessBuilder() + .command(List.of(config.targetDir().resolve(example.bin()).toString())) + .start(); + + var exitCode = process.waitFor(); + + try ( + var stdoutReader = process.inputReader(StandardCharsets.UTF_8); + var stderrReader = process.errorReader(StandardCharsets.UTF_8) + ) { + var stdout = stdoutReader.lines().collect(Collectors.joining()); + var stderr = stderrReader.lines().collect(Collectors.joining()); + + assertThat(stderr, equalTo("")); + assertThat(stdout, equalTo(example.expectedOutput())); + } + } + } + )); + } +} -- cgit v1.2.3