diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-10 17:52:18 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-10 17:52:18 +0000 |
| commit | 8711de89810f1cd6782d98e0e4745c30532a77ac (patch) | |
| tree | fd99c496f52eeaaa2dee10cfb71ea2dc9580bae8 /src/main/java/org/zwobble/couscous/backends/python | |
| parent | 7dab9cb11a7181094144e4319716c4d91262a289 (diff) | |
Support type literals in the backends
Diffstat (limited to 'src/main/java/org/zwobble/couscous/backends/python')
| -rw-r--r-- | src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java | 14 | ||||
| -rw-r--r-- | src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java | 20 |
2 files changed, 22 insertions, 12 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 index de36ed6..731faeb 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java @@ -1,20 +1,17 @@ package org.zwobble.couscous.backends.python; +import com.google.common.collect.ImmutableList; +import com.google.common.io.Resources; +import org.zwobble.couscous.Backend; +import org.zwobble.couscous.ast.ClassNode; + 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 java.util.stream.Stream; - -import com.google.common.collect.ImmutableList; -import org.zwobble.couscous.Backend; -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; import static org.zwobble.couscous.util.ExtraLists.list; @@ -24,6 +21,7 @@ public class PythonBackend implements Backend { "java.lang.Object", "java.lang.Boolean", "java.lang.Integer", + "java.lang.String", "_couscous"); private final Path root; diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java index 311d065..414d04b 100644 --- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java +++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java @@ -105,6 +105,15 @@ public class PythonCodeGenerator { return Stream.empty(); } } + + @Override + public Stream<TypeName> visit(LiteralNode literal) { + if (literal.getValue() instanceof TypeValue) { + return Stream.of(((TypeValue)literal.getValue()).getValue()); + } else { + return Stream.empty(); + } + } })) .collect(Collectors.toSet()); } @@ -134,8 +143,7 @@ public class PythonCodeGenerator { @Override public PythonExpressionNode visit(TypeValue value) { - // TODO: - throw new UnsupportedOperationException(); + return typeReference(value.getValue()); } }); } @@ -270,7 +278,7 @@ public class PythonCodeGenerator { @Override public PythonExpressionNode visit(ConstructorCallNode call) { final org.zwobble.couscous.ast.TypeName className = call.getType(); - final org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode classReference = pythonVariableReference(className.getSimpleName()); + final org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode classReference = typeReference(className); final java.util.List<org.zwobble.couscous.backends.python.ast.PythonExpressionNode> arguments = generateExpressions(call.getArguments()); return pythonCall(classReference, arguments); } @@ -305,7 +313,7 @@ public class PythonCodeGenerator { @Override public PythonExpressionNode visit(TypeName receiver) { - return pythonVariableReference(receiver.getSimpleName()); + return typeReference(receiver); } }); } @@ -322,6 +330,10 @@ public class PythonCodeGenerator { return type.equals(IntegerValue.REF) || type.equals(ObjectValues.BOXED_INT); } + private static PythonVariableReferenceNode typeReference(TypeName className) { + return pythonVariableReference(className.getSimpleName()); + } + public static String toName(MethodSignature signature) { return Joiner.on("__").join(Iterables.concat( list(signature.getName()), |
