aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-10-05 18:15:10 +0100
committerMichael Williamson <mike@zwobble.org>2015-10-05 18:15:10 +0100
commitbd717883dd37d4e3578c1e46c3dba484b6ad4f79 (patch)
tree9c67f3c6e6a607ffaba5ae830588366a9f3bc0b9 /src/main
parent27ffd2d94482ccdcd10acbb1111ce0a0ddae6c01 (diff)
Support String::length in Python backend
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java37
1 files changed, 28 insertions, 9 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 690375d..023d257 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
@@ -156,20 +156,39 @@ public class PythonCodeGenerator {
@Override
public PythonExpressionNode visit(MethodCallNode methodCall) {
- val arguments = methodCall.getArguments().stream()
- .map(PythonCodeGenerator::generateExpression)
- .collect(Collectors.toList());
- return pythonCall(
- pythonAttributeAccess(
- generateExpression(methodCall.getReceiver()),
- methodCall.getMethodName()),
- arguments);
+ if (isPrimitive(methodCall.getReceiver())) {
+ val isStringLength = methodCall.getReceiver().getType().equals(StringValue.REF) &&
+ methodCall.getMethodName() == "length";
+ if (isStringLength) {
+ return pythonCall(
+ pythonVariableReference("len"),
+ asList(generateExpression(methodCall.getReceiver())));
+ } else {
+ throw new UnsupportedOperationException();
+ }
+ } else {
+ val arguments = methodCall.getArguments().stream()
+ .map(PythonCodeGenerator::generateExpression)
+ .collect(Collectors.toList());
+ return pythonCall(
+ pythonAttributeAccess(
+ generateExpression(methodCall.getReceiver()),
+ methodCall.getMethodName()),
+ arguments);
+ }
}
@Override
public PythonExpressionNode visit(StaticMethodCallNode staticMethodCall) {
throw new UnsupportedOperationException();
}
-
+ }
+
+ private static boolean isPrimitive(ExpressionNode value) {
+ val type = value.getType();
+ return type.equals(IntegerValue.REF) ||
+ type.equals(StringValue.REF) ||
+ type.equals(BooleanValue.REF) ||
+ type.equals(UnitValue.REF);
}
}