1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
package org.shedlang.compiler;
import org.shedlang.compiler.ast.untyped.UntypedNamespaceNode;
import org.shedlang.compiler.backends.Backend;
import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.config.ConfigChecker;
import org.shedlang.compiler.config.ProfileConfig;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.parser.Parser;
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.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.parser.Tokeniser.tokenise;
import static org.shedlang.compiler.typechecker.NamespaceDependencyLineariser.orderNamespaces;
public class ShedCompiler {
public void compile(
Path projectRoot,
Backend backend,
Optional<String> profileName
) throws IOException, InterruptedException {
var config = ProjectConfig.read(projectRoot);
var projectRoots = findProjectRoots(config);
var untypedNamespaceNodes = projectRoots.stream()
.flatMap(root -> {
var sourceRoot = root.resolve("src");
try {
var sourceFilePaths = collectSourceFilePaths(sourceRoot);
return parseSource(sourceRoot, sourceFilePaths).stream();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
})
.toList();
var namespaces = new ArrayList<ShidNamespace>();
var typeCheckerContext = TypeCheckerContext.EMPTY
.addNamespaceTypeInfo(
ShedBuiltins.NAMESPACE_NAME,
new NamespaceTypeInfo(FieldsInfo.of(
FieldsInfo.freeField(ShedBuiltins.PRINT_NAME, ShedBuiltins.PRINT_TYPE)
))
)
.enterGlobal(P.map(
Map.entry("Bool", Types.metaType(Types.BOOL)),
Map.entry("Int", Types.metaType(Types.INT)),
Map.entry("Io", Types.effectType(Effects.IO)),
Map.entry("String", Types.metaType(Types.STRING)),
Map.entry("Unit", Types.metaType(Types.UNIT)),
Map.entry(ShedBuiltins.PRINT_NAME, ShedBuiltins.PRINT_TYPE)
));
for (var untypedNamespaceNode : orderNamespaces(untypedNamespaceNodes)) {
var typeCheckNamespaceResult = TypeChecker.typeCheckNamespace(untypedNamespaceNode, typeCheckerContext);
typeCheckerContext = typeCheckNamespaceResult.context();
var shidNamespace = ShidCodeGen.compileNamespace(typeCheckNamespaceResult.value());
namespaces.add(shidNamespace);
}
var typeInfos = typeCheckerContext.toTypeInfos();
ConfigChecker.configCheck(config, typeInfos);
var profileConfig = profileName.map(config::profile)
.orElse(ProfileConfig.DEFAULT);
backend.compile(
config,
profileConfig,
namespaces,
typeInfos,
new TargetDir(
config.targetDir()
.resolve(backend.targetName())
.resolve(profileConfig.name())
)
);
}
/**
* Find the roots of any projects that require compilation, including
* transitive dependencies.
*/
private LinkedHashSet<Path> findProjectRoots(ProjectConfig config) throws IOException {
var projectRoots = new LinkedHashSet<Path>();
var pendingProjects = new ArrayDeque<Path>();
pendingProjects.add(config.root());
// TODO: detect cycles
while (!pendingProjects.isEmpty()) {
var projectRoot = pendingProjects.removeFirst();
if (projectRoots.add(projectRoot)) {
var projectConfig = ProjectConfig.read(projectRoot);
for (var dependency : projectConfig.dependencies()) {
pendingProjects.add(dependency.path());
}
}
}
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
) 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);
var tokens = tokenise(sourceText);
var untypedNode = Parser.parseNamespace(tokens, namespaceName);
untypedNamespaceNodes.add(untypedNode);
}
return untypedNamespaceNodes;
}
}
|