aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-11-07 23:12:08 +0000
committerMichael Williamson <mike@zwobble.org>2015-11-07 23:12:08 +0000
commitd8cfe057ba5683dd4c9fe3871aa5af28f977919d (patch)
treec91f3e3d447a1b9cc3ed6de92a50e4ac969f5a44 /src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java
parent59599a21e5c332421f866ab0fc469d93784b70f8 (diff)
Rename PythonCompiler to PythonBackend
Diffstat (limited to 'src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java')
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java b/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java
new file mode 100644
index 0000000..5c3d0c5
--- /dev/null
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java
@@ -0,0 +1,74 @@
+package org.zwobble.couscous.backends.python;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import org.zwobble.couscous.ast.ClassNode;
+
+import com.google.common.io.Resources;
+
+import static java.util.Arrays.asList;
+import static org.zwobble.couscous.backends.python.PythonCodeGenerator.generateCode;
+import static org.zwobble.couscous.backends.python.PythonSerializer.serialize;
+
+public class PythonBackend {
+ private static final List<String> RUNTIME_FILES = asList(
+ "java.lang.Object",
+ "java.lang.Integer",
+ "_couscous");
+
+ private final Path root;
+ private final String packageName;
+
+ public PythonBackend(Path root, String packageName) {
+ this.root = root;
+ this.packageName = packageName;
+ }
+
+ public void compile(List<ClassNode> classes) throws IOException {
+ for (ClassNode classNode : classes) {
+ compileClass(classNode);
+ }
+ for (String runtimeFile : RUNTIME_FILES) {
+ String path = relativePathForModule(runtimeFile);
+ writeModule(
+ runtimeFile,
+ Resources.toString(Resources.getResource("python/runtime/" + path), StandardCharsets.UTF_8));
+ }
+ }
+
+ private Path destinationPathForModule(String moduleName) {
+ return root.resolve(packageName).resolve(relativePathForModule(moduleName));
+ }
+
+ private String relativePathForModule(String moduleName) {
+ return moduleName.replace(".", File.separator) + ".py";
+ }
+
+ private void compileClass(ClassNode classNode) throws IOException {
+ writeModule(
+ classNode.getName().getQualifiedName(),
+ serialize(generateCode(classNode)));
+ }
+
+ private void writeModule(String name, String contents) throws IOException {
+ Path path = destinationPathForModule(name);
+ Files.createDirectories(path.getParent());
+ createPythonPackages(path.getParent());
+ Files.write(path, asList(contents));
+ }
+
+ private void createPythonPackages(Path packagePath) throws IOException {
+ while (packagePath.startsWith(root)) {
+ final java.nio.file.Path packageFile = packagePath.resolve("__init__.py");
+ if (!packageFile.toFile().exists()) {
+ Files.createFile(packageFile);
+ }
+ packagePath = packagePath.getParent();
+ }
+ }
+} \ No newline at end of file