diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-04-25 10:25:24 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-04-25 10:25:24 +0100 |
| commit | 2a37bf78f778c2955a80f0bbf0ac7d4c14f5c55f (patch) | |
| tree | 815b68061e1516c78f096e846bf1636ff3ceb6a3 /src/main/java/org | |
| parent | 7e0999200b57bfd01b04a8dd96cd8eed997ad2e2 (diff) | |
Add test for source file namespace
Diffstat (limited to 'src/main/java/org')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java index 7945944..6daa31b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/HobgoblinCompiler.java @@ -10,18 +10,20 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; public class HobgoblinCompiler { + public static final String FILE_NAME_EXTENSION = ".hob"; + 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")); + .listFiles(file -> file.toPath().toString().endsWith(FILE_NAME_EXTENSION)); for (var sourceFile : sourceFiles) { - // TODO: derive namespace name from source path - var namespaceName = NamespaceName.of(); + var namespaceName = sourceFileNamespaceName(sourceDirectory, sourceFile.toPath()); var sourceContents = Files.readString(sourceFile.toPath(), StandardCharsets.UTF_8); var sourceText = SourceText.fromString(sourceFile.toPath().toString(), sourceContents); var untypedNamespaceNode = Parser.parseNamespace(sourceText, namespaceName); @@ -30,4 +32,19 @@ public class HobgoblinCompiler { var typedNamespaceNode = TypeChecker.typeCheckNamespace(untypedNamespaceNode, typeCheckerContext); } } + + static NamespaceName sourceFileNamespaceName(Path sourceDirectory, Path sourceFilePath) { + var relativePath = sourceDirectory.relativize(sourceFilePath); + + var parts = new ArrayList<String>(); + for (var i = 0; i < relativePath.getNameCount() - 1; i++) { + parts.add(relativePath.getName(i).toString()); + } + + var fileName = relativePath.getName(relativePath.getNameCount() - 1).toString(); + var lastPart = fileName.substring(0, fileName.length() - FILE_NAME_EXTENSION.length()); + parts.add(lastPart); + + return new NamespaceName(parts); + } } |
