summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/01-struct/hobgoblin.json51
-rw-r--r--examples/01-struct/src/point.hob4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java28
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/filesystem/HobgoblinCompilerFiles.java13
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java9
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenIterator.java2
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ExampleSet.java38
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java22
8 files changed, 116 insertions, 1 deletions
diff --git a/examples/01-struct/hobgoblin.json5 b/examples/01-struct/hobgoblin.json5
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/examples/01-struct/hobgoblin.json5
@@ -0,0 +1 @@
+{}
diff --git a/examples/01-struct/src/point.hob b/examples/01-struct/src/point.hob
new file mode 100644
index 0000000..82b55cc
--- /dev/null
+++ b/examples/01-struct/src/point.hob
@@ -0,0 +1,4 @@
+struct Point {
+ field x: Int32;
+ field y: Int32;
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java
new file mode 100644
index 0000000..9d95dae
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java
@@ -0,0 +1,28 @@
+package org.zwobble.hobgoblin.compiler;
+
+import org.zwobble.hobgoblin.compiler.parser.Parser;
+import org.zwobble.hobgoblin.compiler.types.NamespaceName;
+import org.zwobble.sourcetext.SourceText;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class HobgoblinCompiler {
+ private HobgoblinCompiler() {
+ }
+
+ public static void compile(Path projectRoot) throws IOException {
+ var sourceDirectory = projectRoot.resolve("src");
+ var sourceFiles = sourceDirectory.toFile()
+ .listFiles(file -> file.toPath().toString().endsWith(".hob"));
+ for (var sourceFile : sourceFiles) {
+ // TODO: derive namespace name from source path
+ var namespaceName = NamespaceName.of();
+ var sourceContents = Files.readString(sourceFile.toPath(), StandardCharsets.UTF_8);
+ var sourceText = SourceText.fromString(sourceFile.toPath().toString(), sourceContents);
+ var namespace = Parser.parseNamespace(sourceText, namespaceName);
+ }
+ }
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/filesystem/HobgoblinCompilerFiles.java b/src/main/java/org/zwobble/hobgoblin/compiler/filesystem/HobgoblinCompilerFiles.java
new file mode 100644
index 0000000..5609313
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/filesystem/HobgoblinCompilerFiles.java
@@ -0,0 +1,13 @@
+package org.zwobble.hobgoblin.compiler.filesystem;
+
+import java.nio.file.Path;
+
+public class HobgoblinCompilerFiles {
+ private HobgoblinCompilerFiles() {
+ }
+
+ public static Path getRoot() {
+ // TODO: don't assume we're in the project root
+ return Path.of(".");
+ }
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
index 7a50bf5..fbf38bb 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
@@ -5,6 +5,7 @@ import org.zwobble.hobgoblin.compiler.sources.FileFragmentSource;
import org.zwobble.hobgoblin.compiler.sources.Source;
import org.zwobble.hobgoblin.compiler.types.NamespaceName;
import org.zwobble.sourcetext.SourcePosition;
+import org.zwobble.sourcetext.SourceText;
import java.util.ArrayList;
import java.util.List;
@@ -16,6 +17,14 @@ public class Parser {
}
public static UntypedNamespaceNode parseNamespace(
+ SourceText sourceText,
+ NamespaceName namespaceName
+ ) {
+ var tokens = Tokenizer.tokenize(sourceText);
+ return parseNamespace(tokens.iterator(), namespaceName);
+ }
+
+ static UntypedNamespaceNode parseNamespace(
TokenIterator tokens,
NamespaceName namespaceName
) {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenIterator.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenIterator.java
index a66ec35..0ae3d28 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenIterator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenIterator.java
@@ -4,7 +4,7 @@ import org.zwobble.sourcetext.SourcePosition;
import java.util.List;
-public class TokenIterator {
+class TokenIterator {
private final List<Token> tokens;
private int tokenIndex;
private final Token tokenEnd;
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ExampleSet.java b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleSet.java
new file mode 100644
index 0000000..5a29e90
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleSet.java
@@ -0,0 +1,38 @@
+package org.zwobble.hobgoblin.compiler;
+
+import org.zwobble.hobgoblin.compiler.filesystem.HobgoblinCompilerFiles;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.stream.Stream;
+
+public class ExampleSet {
+ public static Stream<ExampleSet> exampleSets() {
+ return exampleSetDirectories()
+ .map(exampleDirectory -> new ExampleSet(exampleDirectory.toPath()));
+ }
+
+ /// The directory of each example set.
+ private static Stream<File> exampleSetDirectories() {
+ var examplesRoot = HobgoblinCompilerFiles.getRoot().resolve("examples");
+
+ return Arrays.stream(examplesRoot.toFile()
+ .listFiles(file -> file.isDirectory()));
+ }
+
+ private final Path path;
+
+ private ExampleSet(Path path) {
+ this.path = path;
+ }
+
+ public String name() {
+ return this.path.getFileName().toString();
+ }
+
+ public Path path() {
+ return this.path;
+ }
+}
+
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java
new file mode 100644
index 0000000..354cb08
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ExampleTests.java
@@ -0,0 +1,22 @@
+package org.zwobble.hobgoblin.compiler;
+
+import org.junit.jupiter.api.DynamicTest;
+import org.junit.jupiter.api.TestFactory;
+
+import java.io.IOException;
+import java.util.stream.Stream;
+
+public class ExampleTests {
+ @TestFactory
+ public Stream<DynamicTest> tests() {
+ return ExampleSet.exampleSets()
+ .map(exampleSet -> DynamicTest.dynamicTest(
+ exampleSet.name(),
+ () -> runTest(exampleSet)
+ ));
+ }
+
+ private void runTest(ExampleSet exampleSet) throws IOException {
+ HobgoblinCompiler.compile(exampleSet.path());
+ }
+}