blob: 8787d8199a4143f5be710f3591516efff7e2aaa4 (
plain)
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
|
package org.zwobble.couscous.backends.python;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.ast.ClassNode;
import static java.util.Arrays.asList;
import static org.zwobble.couscous.backends.python.PythonCodeGenerator.generateCode;
import static org.zwobble.couscous.backends.python.PythonSerializer.serialize;
import lombok.SneakyThrows;
import lombok.val;
public class PythonCompiler {
private final Path root;
private final String packageName;
public PythonCompiler(Path root, String packageName) {
this.root = root;
this.packageName = packageName;
}
public void compile(List<ClassNode> classes) {
for (val classNode : classes) {
compileClass(classNode);
}
writeClass(TypeName.of("java.lang.Integer"),
"class Integer(object):\n" +
" def parseInt(value):\n" +
" return int(value)"
);
}
private Path pathForClass(TypeName className) {
return root
.resolve(packageName)
.resolve(className.getQualifiedName().replace(".", File.separator) + ".py");
}
private void compileClass(ClassNode classNode) {
writeClass(classNode.getName(), serialize(generateCode(classNode)));
}
@SneakyThrows
private void writeClass(TypeName name, String contents) {
val path = pathForClass(name);
Files.createDirectories(path.getParent());
createPythonPackages(path.getParent());
Files.write(path, asList(contents));
}
private void createPythonPackages(Path packagePath) throws IOException {
while (packagePath.startsWith(root)) {
val packageFile = packagePath.resolve("__init__.py");
if (!packageFile.toFile().exists()) {
Files.createFile(packageFile);
}
packagePath = packagePath.getParent();
}
}
}
|