aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/python
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-03-09 18:11:17 +0000
committerMichael Williamson <mike@zwobble.org>2016-03-09 18:11:17 +0000
commit20dc1e124d606cf4298d5112b6d3c4f6c74e9b68 (patch)
tree47914e8b0356b2f40e3723eae650d3bb6f100dd4 /src/main/java/org/zwobble/couscous/backends/python
parent8ac6cb16b727b5e3d28ab89fee1d169a4f74d9a1 (diff)
Introduce Type interface
Diffstat (limited to 'src/main/java/org/zwobble/couscous/backends/python')
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java35
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java14
2 files changed, 26 insertions, 23 deletions
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 7d134ef..49e7b80 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
@@ -6,6 +6,8 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import org.zwobble.couscous.ast.*;
import org.zwobble.couscous.ast.structure.NodeStructure;
+import org.zwobble.couscous.ast.types.ScalarType;
+import org.zwobble.couscous.ast.types.Type;
import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper;
import org.zwobble.couscous.ast.visitors.NodeMapperWithDefault;
import org.zwobble.couscous.ast.visitors.StatementNodeMapper;
@@ -20,6 +22,7 @@ import java.util.stream.Stream;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Iterators.singletonIterator;
+import static org.zwobble.couscous.ast.types.Types.erasure;
import static org.zwobble.couscous.backends.python.ast.PythonAssignmentNode.pythonAssignment;
import static org.zwobble.couscous.backends.python.ast.PythonAttributeAccessNode.pythonAttributeAccess;
import static org.zwobble.couscous.backends.python.ast.PythonBooleanLiteralNode.pythonBooleanLiteral;
@@ -72,7 +75,7 @@ public class PythonCodeGenerator {
}
private static Stream<PythonImportNode> generateImports(TypeNode classNode) {
- Set<TypeName> classes = findReferencedClasses(classNode);
+ Set<ScalarType> classes = findReferencedClasses(classNode);
return classes.stream()
.filter(name -> !name.equals(InternalCouscousValue.REF))
.map(name -> pythonImport(importPathToRoot(classNode) + name.getQualifiedName(), list(pythonImportAlias(name.getSimpleName()))));
@@ -93,21 +96,21 @@ public class PythonCodeGenerator {
return depth;
}
- private static Set<TypeName> findReferencedClasses(TypeNode classNode) {
+ private static Set<ScalarType> findReferencedClasses(TypeNode classNode) {
return NodeStructure.descendantNodes(classNode)
- .flatMap(node -> node.accept(new NodeMapperWithDefault<Stream<TypeName>>(Stream.empty()) {
+ .flatMap(node -> node.accept(new NodeMapperWithDefault<Stream<ScalarType>>(Stream.empty()) {
@Override
- public Stream<TypeName> visit(StaticReceiver receiver) {
+ public Stream<ScalarType> visit(StaticReceiver receiver) {
return Stream.of(receiver.getType());
}
@Override
- public Stream<TypeName> visit(ConstructorCallNode call) {
- return Stream.of(call.getType());
+ public Stream<ScalarType> visit(ConstructorCallNode call) {
+ return Stream.of(erasure(call.getType()));
}
@Override
- public Stream<TypeName> visit(TypeCoercionNode typeCoercion) {
+ public Stream<ScalarType> visit(TypeCoercionNode typeCoercion) {
if (isIntegerBox(typeCoercion)) {
return Stream.of(ObjectValues.BOXED_INT);
} else {
@@ -116,7 +119,7 @@ public class PythonCodeGenerator {
}
@Override
- public Stream<TypeName> visit(LiteralNode literal) {
+ public Stream<ScalarType> visit(LiteralNode literal) {
if (literal.getValue() instanceof TypeValue) {
return Stream.of(((TypeValue)literal.getValue()).getValue());
} else {
@@ -151,7 +154,7 @@ public class PythonCodeGenerator {
}
@Override
- public PythonExpressionNode visitType(TypeName value) {
+ public PythonExpressionNode visitType(ScalarType value) {
return typeReference(value);
}
});
@@ -272,12 +275,12 @@ public class PythonCodeGenerator {
return methodCall.getReceiver().accept(new Receiver.Mapper<Optional<PythonExpressionNode>>() {
@Override
public Optional<PythonExpressionNode> visit(ExpressionNode receiver) {
- return PythonPrimitiveMethods.getPrimitiveMethod(receiver.getType(), methodCall.getMethodName())
+ return PythonPrimitiveMethods.getPrimitiveMethod(erasure(receiver.getType()), methodCall.getMethodName())
.map(generator -> generator.generate(pythonReceiver, pythonArguments));
}
@Override
- public Optional<PythonExpressionNode> visit(TypeName receiver) {
+ public Optional<PythonExpressionNode> visit(ScalarType receiver) {
return PythonPrimitiveMethods.getPrimitiveStaticMethod(receiver, methodCall.getMethodName())
.map(generator -> generator.generate(pythonArguments));
}
@@ -286,7 +289,7 @@ public class PythonCodeGenerator {
@Override
public PythonExpressionNode visit(ConstructorCallNode call) {
- final org.zwobble.couscous.ast.TypeName className = call.getType();
+ final Type className = call.getType();
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);
@@ -334,7 +337,7 @@ public class PythonCodeGenerator {
}
@Override
- public PythonExpressionNode visit(TypeName receiver) {
+ public PythonExpressionNode visit(ScalarType receiver) {
return typeReference(receiver);
}
});
@@ -348,11 +351,11 @@ public class PythonCodeGenerator {
return !isInteger(typeCoercion.getExpression().getType()) && isInteger(typeCoercion.getType());
}
- private static boolean isInteger(TypeName type) {
+ private static boolean isInteger(Type type) {
return type.equals(IntegerValue.REF) || type.equals(ObjectValues.BOXED_INT);
}
- private static PythonVariableReferenceNode typeReference(TypeName className) {
- return pythonVariableReference(className.getSimpleName());
+ private static PythonVariableReferenceNode typeReference(Type className) {
+ return pythonVariableReference(erasure(className).getSimpleName());
}
} \ No newline at end of file
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java
index 87fc0ac..49d9abf 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonPrimitiveMethods.java
@@ -3,7 +3,7 @@ package org.zwobble.couscous.backends.python;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import org.zwobble.couscous.ast.Operator;
-import org.zwobble.couscous.ast.TypeName;
+import org.zwobble.couscous.ast.types.ScalarType;
import org.zwobble.couscous.backends.python.ast.PythonBinaryOperation;
import org.zwobble.couscous.backends.python.ast.PythonExpressionNode;
import org.zwobble.couscous.backends.python.ast.PythonNotNode;
@@ -110,8 +110,8 @@ public class PythonPrimitiveMethods {
build.apply(receiver, arguments.get(0)));
}
- private static final Map<TypeName, Map<String, PrimitiveMethodGenerator>> METHODS =
- ImmutableMap.<TypeName, Map<String, PrimitiveMethodGenerator>>builder()
+ private static final Map<ScalarType, Map<String, PrimitiveMethodGenerator>> METHODS =
+ ImmutableMap.<ScalarType, Map<String, PrimitiveMethodGenerator>>builder()
.put(StringValue.REF, STRING_METHODS)
.put(BooleanValue.REF, BOOLEAN_METHODS)
.put(IntegerValue.REF, INT_METHODS)
@@ -132,13 +132,13 @@ public class PythonPrimitiveMethods {
.build();
- private static final Map<TypeName, Map<String, PrimitiveStaticMethodGenerator>> STATIC_METHODS =
- ImmutableMap.<TypeName, Map<String, PrimitiveStaticMethodGenerator>>builder()
+ private static final Map<ScalarType, Map<String, PrimitiveStaticMethodGenerator>> STATIC_METHODS =
+ ImmutableMap.<ScalarType, Map<String, PrimitiveStaticMethodGenerator>>builder()
.put(InternalCouscousValue.REF, INTERNAL_METHODS)
.put(ObjectValues.BOXED_INT, BOXED_INT_STATIC_METHODS)
.build();
- public static Optional<PrimitiveMethodGenerator> getPrimitiveMethod(TypeName type, String methodName) {
+ public static Optional<PrimitiveMethodGenerator> getPrimitiveMethod(ScalarType type, String methodName) {
return Optional.ofNullable(METHODS.get(type))
.flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName)));
}
@@ -148,7 +148,7 @@ public class PythonPrimitiveMethods {
PythonExpressionNode generate(PythonExpressionNode receiver, List<PythonExpressionNode> arguments);
}
- public static Optional<PrimitiveStaticMethodGenerator> getPrimitiveStaticMethod(TypeName type, String methodName) {
+ public static Optional<PrimitiveStaticMethodGenerator> getPrimitiveStaticMethod(ScalarType type, String methodName) {
return Optional.ofNullable(STATIC_METHODS.get(type))
.flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName)));
}