diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-04-23 17:03:59 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-04-23 17:03:59 +0100 |
| commit | 045969708f21e19f852256f8bf934f4289b96495 (patch) | |
| tree | 91e9f9dda9f5b4b4dd81be5e07332e0d98815b4f /src | |
| parent | 2113d294e5194dd88b5e92142af505b52bd20adb (diff) | |
Add basic example
Diffstat (limited to 'src')
6 files changed, 111 insertions, 1 deletions
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()); + } +} |
