From d8cfe057ba5683dd4c9fe3871aa5af28f977919d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 7 Nov 2015 23:12:08 +0000 Subject: Rename PythonCompiler to PythonBackend --- .../couscous/backends/python/PythonBackend.java | 74 ++++++++++++++++++++++ .../couscous/backends/python/PythonCompiler.java | 74 ---------------------- 2 files changed, 74 insertions(+), 74 deletions(-) create mode 100644 src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java delete mode 100644 src/main/java/org/zwobble/couscous/backends/python/PythonCompiler.java (limited to 'src/main/java/org/zwobble/couscous/backends/python') 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 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 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 diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonCompiler.java b/src/main/java/org/zwobble/couscous/backends/python/PythonCompiler.java deleted file mode 100644 index d077736..0000000 --- a/src/main/java/org/zwobble/couscous/backends/python/PythonCompiler.java +++ /dev/null @@ -1,74 +0,0 @@ -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 PythonCompiler { - private static final List RUNTIME_FILES = asList( - "java.lang.Object", - "java.lang.Integer", - "_couscous"); - - private final Path root; - private final String packageName; - - public PythonCompiler(Path root, String packageName) { - this.root = root; - this.packageName = packageName; - } - - public void compile(List 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 -- cgit v1.2.3