From 2a37bf78f778c2955a80f0bbf0ac7d4c14f5c55f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 25 Apr 2026 10:25:24 +0100 Subject: Add test for source file namespace --- .../hobgoblin/compiler/HobgoblinCompiler.java | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/main/java/org/zwobble') 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(); + 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); + } } -- cgit v1.2.3