From 975e56cb795a42e576a3e44aee155ff3a81b601b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 11 Jun 2023 20:03:13 +0100 Subject: Add stubbed LLVM backend --- .../java/org/shedlang/compiler/ShedCompiler.java | 34 ++++++++++++---------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'src/main/java/org/shedlang/compiler/ShedCompiler.java') diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java index 24455816..8cc102d8 100644 --- a/src/main/java/org/shedlang/compiler/ShedCompiler.java +++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java @@ -1,14 +1,16 @@ package org.shedlang.compiler; +import org.shedlang.compiler.backends.Backend; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.parser.Parser; import org.shedlang.compiler.sources.Sources; -import org.shedlang.compiler.stackir.InstructionSerializer; import org.shedlang.compiler.stackir.StackCompiler; +import org.shedlang.compiler.stackir.instructions.Instruction; import org.shedlang.compiler.typechecker.TypeChecker; import org.shedlang.compiler.typechecker.TypeCheckerContext; import org.shedlang.compiler.types.Effects; import org.shedlang.compiler.types.FunctionType; +import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; import org.shedlang.compiler.util.P; @@ -19,25 +21,29 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.stream.StreamSupport; import static org.shedlang.compiler.parser.Tokeniser.tokenise; public class ShedCompiler { - public void compile(Path projectRoot) throws IOException { + public void compile(Path projectRoot, Backend backend) throws IOException { var config = ProjectConfig.read(projectRoot); - System.out.println("main: " + config.main()); - var sourceRoot = projectRoot.resolve("src"); var sourceFilePaths = collectSourceFilePaths(sourceRoot); + var namespaces = new LinkedHashMap>(); + for (var sourceFilePath : sourceFilePaths) { - System.out.println(sourceFilePath); + var namespaceNameParts = StreamSupport.stream(sourceRoot.relativize(sourceFilePath).spliterator(), false) + .map(element -> { + var string = element.toString(); + return string.substring(0, string.lastIndexOf('.')); + }) + .toList(); + var namespaceName = new NamespaceName(namespaceNameParts); var sourceContents = Files.readString(sourceFilePath, StandardCharsets.UTF_8); var source = Sources.file(sourceFilePath.toString(), sourceContents); @@ -60,14 +66,10 @@ public class ShedCompiler { var typedNode = TypeChecker.typeCheckNamespace(untypedNode, context); var instructions = StackCompiler.compileNamespace(typedNode); - - var builder = new StringBuilder(); - InstructionSerializer.serializeInstructions(instructions, builder); - var instructionsString = builder.toString(); - - System.out.println(instructionsString); - System.out.println(); + namespaces.put(namespaceName, instructions); } + + backend.compile(config, namespaces); } private List collectSourceFilePaths(Path sourceRoot) throws IOException { -- cgit v1.2.3