diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-07 20:39:24 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-07 20:39:24 +0100 |
| commit | d73f24563db38a46318acf0d4b0378ac787fb593 (patch) | |
| tree | f9f9e2764a243dcedc6f7d63ede7a6290de26e08 /src/main/java/org/shedlang/compiler/ShedCompiler.java | |
| parent | e4790b25e567aeaef20f29d1a6174413bc8ea0f0 (diff) | |
Add CLI
Diffstat (limited to 'src/main/java/org/shedlang/compiler/ShedCompiler.java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/ShedCompiler.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java new file mode 100644 index 00000000..a7949d53 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java @@ -0,0 +1,63 @@ +package org.shedlang.compiler; + +import org.shedlang.compiler.parser.Parser; +import org.shedlang.compiler.sources.Sources; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileVisitResult; +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 static org.shedlang.compiler.parser.Tokeniser.tokenise; + +public class ShedCompiler { + public void compile(Path projectRoot) throws IOException { + var sourceRoot = projectRoot.resolve("src"); + + var sourceFilePaths = collectSourceFilePaths(sourceRoot); + + for (var sourceFilePath : sourceFilePaths) { + System.out.println(sourceFilePath); + + var sourceContents = Files.readString(sourceFilePath, StandardCharsets.UTF_8); + var source = Sources.file(sourceFilePath.toString(), sourceContents); + var tokens = tokenise(source); + var node = Parser.parseNamespace(tokens); + System.out.println(node); + + System.out.println(); + } + } + + private List<Path> collectSourceFilePaths(Path sourceRoot) throws IOException { + var paths = new ArrayList<Path>(); + Files.walkFileTree(sourceRoot, new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile( + Path file, + BasicFileAttributes attrs + ) { + if (file.getFileName().toString().endsWith(".shed")) { + paths.add(file); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory( + Path dir, + IOException exc + ) { + return FileVisitResult.CONTINUE; + } + }); + paths.sort(Comparator.naturalOrder()); + return paths; + } +} |
