aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/ShedCompiler.java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-01-12 17:29:05 +0000
committerMichael Williamson <mike@zwobble.org>2025-01-12 17:29:05 +0000
commit50730c7784ee742c8c148d4d5b07c4ea312a8e58 (patch)
tree85f1432a0bb6ad137176fc376cbd6cd4770bc7e2 /src/main/java/org/shedlang/compiler/ShedCompiler.java
parent0d3098233012bf20123e778927a2e8473a2e230b (diff)
Improve collection of source files
Diffstat (limited to 'src/main/java/org/shedlang/compiler/ShedCompiler.java')
-rw-r--r--src/main/java/org/shedlang/compiler/ShedCompiler.java65
1 files changed, 12 insertions, 53 deletions
diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java
index 3901da5c..d1bd6101 100644
--- a/src/main/java/org/shedlang/compiler/ShedCompiler.java
+++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java
@@ -11,20 +11,20 @@ import org.shedlang.compiler.shid.ShidCodeGen;
import org.shedlang.compiler.shid.ShidNamespace;
import org.shedlang.compiler.typechecker.TypeChecker;
import org.shedlang.compiler.typechecker.TypeCheckerContext;
-import org.shedlang.compiler.types.*;
+import org.shedlang.compiler.types.Effects;
+import org.shedlang.compiler.types.FieldsInfo;
+import org.shedlang.compiler.types.NamespaceTypeInfo;
+import org.shedlang.compiler.types.Types;
import org.shedlang.compiler.util.P;
import org.zwobble.sourcetext.SourceText;
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.*;
-import java.util.stream.StreamSupport;
+import static org.shedlang.compiler.SourceFiles.collectSourceFiles;
import static org.shedlang.compiler.parser.Tokeniser.tokenise;
import static org.shedlang.compiler.typechecker.NamespaceDependencyLineariser.orderNamespaces;
@@ -42,8 +42,8 @@ public class ShedCompiler {
.flatMap(root -> {
var sourceRoot = root.resolve("src");
try {
- var sourceFilePaths = collectSourceFilePaths(sourceRoot);
- return parseSource(sourceRoot, sourceFilePaths).stream();
+ var sourceFiles = collectSourceFiles(sourceRoot);
+ return parseSource(sourceFiles).stream();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
@@ -120,59 +120,18 @@ public class ShedCompiler {
return projectRoots;
}
- 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;
- }
-
private List<UntypedNamespaceNode> parseSource(
- Path sourceRoot,
- List<Path> sourceFilePaths
+ List<SourceFile> sourceFiles
) throws IOException {
var untypedNamespaceNodes = new ArrayList<UntypedNamespaceNode>();
- for (var sourceFilePath : sourceFilePaths) {
- // TODO: handle path -> namespace name properly and test
- var namespaceNameParts = StreamSupport.stream(sourceRoot.relativize(sourceFilePath).spliterator(), false)
- .map(element -> {
- var string = element.toString();
- var lastDotIndex = string.lastIndexOf('.');
- if (lastDotIndex == -1) {
- return string;
- } else {
- return string.substring(0, lastDotIndex);
- }
- })
- .toList();
- var namespaceName = new NamespaceName(namespaceNameParts);
-
- var sourceContents = Files.readString(sourceFilePath, StandardCharsets.UTF_8);
- var sourceText = SourceText.fromString(sourceFilePath.toString(), sourceContents);
+ for (var sourceFile : sourceFiles) {
+ var sourceContents = Files.readString(sourceFile.path(), StandardCharsets.UTF_8);
+ var sourceText = SourceText.fromString(sourceFile.path().toString(), sourceContents);
var tokens = tokenise(sourceText);
- var untypedNode = Parser.parseNamespace(tokens, namespaceName);
+ var untypedNode = Parser.parseNamespace(tokens, sourceFile.namespaceName());
untypedNamespaceNodes.add(untypedNode);
}