aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-07 20:39:24 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-07 20:39:24 +0100
commitd73f24563db38a46318acf0d4b0378ac787fb593 (patch)
treef9f9e2764a243dcedc6f7d63ede7a6290de26e08 /src/main/java
parente4790b25e567aeaef20f29d1a6174413bc8ea0f0 (diff)
Add CLI
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ShedCompiler.java63
-rw-r--r--src/main/java/org/shedlang/compiler/cli/ShedcCli.java45
-rw-r--r--src/main/java/org/shedlang/compiler/parser/Parser.java2
3 files changed, 109 insertions, 1 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;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/cli/ShedcCli.java b/src/main/java/org/shedlang/compiler/cli/ShedcCli.java
new file mode 100644
index 00000000..82549f97
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/cli/ShedcCli.java
@@ -0,0 +1,45 @@
+package org.shedlang.compiler.cli;
+
+import net.sourceforge.argparse4j.ArgumentParsers;
+import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import net.sourceforge.argparse4j.inf.Namespace;
+import org.shedlang.compiler.ShedCompiler;
+import org.shedlang.compiler.errors.SourceError;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+
+public class ShedcCli {
+ public static void main(String[] rawArgs) throws IOException {
+ var args = parseArgs(rawArgs);
+ try {
+ compile(args);
+ } catch (SourceError error) {
+ System.err.println(error.getSource().describe());
+ System.err.println(error.getMessage());
+ System.exit(2);
+ }
+ }
+
+ private static void compile(Namespace args) throws IOException {
+ var sourcePath = Paths.get(args.getString("source"));
+
+ var compiler = new ShedCompiler();
+ compiler.compile(sourcePath);
+ }
+
+ private static Namespace parseArgs(String[] args) {
+ var parser = ArgumentParsers.newFor("shedc").build()
+ .defaultHelp(true);
+
+ parser.addArgument("source").required(true);
+
+ try {
+ return parser.parseArgs(args);
+ } catch (ArgumentParserException e) {
+ parser.handleError(e);
+ System.exit(1);
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/parser/Parser.java b/src/main/java/org/shedlang/compiler/parser/Parser.java
index 384cd694..5b17e80d 100644
--- a/src/main/java/org/shedlang/compiler/parser/Parser.java
+++ b/src/main/java/org/shedlang/compiler/parser/Parser.java
@@ -240,7 +240,7 @@ public class Parser {
return new UntypedTypeLevelReferenceNode(name, source);
}
- static UntypedNamespaceNode parseNamespace(TokenIterator<TokenType> tokens) {
+ public static UntypedNamespaceNode parseNamespace(TokenIterator<TokenType> tokens) {
var start = tokens.position();
var end = tokens.position();