aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/backends/csharp
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-03-09 22:44:29 +0000
committerMichael Williamson <mike@zwobble.org>2016-03-10 16:43:05 +0000
commit2a182825a88b6ee957e205862f5cf3a505b3a173 (patch)
tree2054c8a0c4252bd4b3f161de0b34482158ee7728 /src/main/java/org/zwobble/couscous/backends/csharp
parent20dc1e124d606cf4298d5112b6d3c4f6c74e9b68 (diff)
Initial implementation of generics for C#
Diffstat (limited to 'src/main/java/org/zwobble/couscous/backends/csharp')
-rw-r--r--src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java63
-rw-r--r--src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java31
2 files changed, 84 insertions, 10 deletions
diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java
index 8388421..469705b 100644
--- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java
+++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java
@@ -2,8 +2,7 @@ package org.zwobble.couscous.backends.csharp;
import com.google.common.collect.ImmutableMap;
import org.zwobble.couscous.ast.*;
-import org.zwobble.couscous.ast.types.ScalarType;
-import org.zwobble.couscous.ast.types.Type;
+import org.zwobble.couscous.ast.types.*;
import org.zwobble.couscous.ast.visitors.NodeTransformer;
import org.zwobble.couscous.backends.Names;
import org.zwobble.couscous.values.*;
@@ -13,6 +12,7 @@ import java.util.Optional;
import static org.zwobble.couscous.ast.types.Types.erasure;
import static org.zwobble.couscous.util.Casts.tryCast;
+import static org.zwobble.couscous.util.ExtraLists.eagerMap;
public class CsharpCodeGenerator {
private final static Map<ScalarType, ScalarType> PRIMITIVES = ImmutableMap.<ScalarType, ScalarType>builder()
@@ -44,11 +44,39 @@ public class CsharpCodeGenerator {
}
private Type transformType(Type type) {
- if (PRIMITIVES.containsKey(type)) {
- return PRIMITIVES.get(type);
- } else {
- return addPrefix(type, namespace);
- }
+ return type.accept(new Type.Visitor<Type>() {
+ @Override
+ public Type visit(ScalarType type) {
+ if (PRIMITIVES.containsKey(type)) {
+ return PRIMITIVES.get(type);
+ } else {
+ return addPrefix(type, namespace);
+ }
+ }
+
+ @Override
+ public TypeParameter visit(TypeParameter parameter) {
+ return parameter;
+ }
+
+ @Override
+ public Type visit(ParameterizedType type) {
+ if (type.getRawType().equals(ObjectValues.CLASS)) {
+ return ScalarType.of("System.Type");
+ } else {
+ return new ParameterizedType(
+ (ScalarType) transformType(type.getRawType()),
+ eagerMap(type.getParameters(), parameter -> transformType(parameter)));
+ }
+ }
+
+ @Override
+ public Type visit(BoundTypeParameter type) {
+ return new BoundTypeParameter(
+ visit(type.getParameter()),
+ transformType(type.getValue()));
+ }
+ });
}
private String transformMethodName(MethodSignature signature) {
@@ -81,9 +109,28 @@ public class CsharpCodeGenerator {
private Type addPrefix(Type type, String namespace) {
return type.accept(new Type.Visitor<Type>() {
@Override
- public Type visit(ScalarType type) {
+ public ScalarType visit(ScalarType type) {
return ScalarType.of(namespace + "." + type.getQualifiedName());
}
+
+ @Override
+ public TypeParameter visit(TypeParameter parameter) {
+ return parameter;
+ }
+
+ @Override
+ public Type visit(ParameterizedType type) {
+ return new ParameterizedType(
+ visit(type.getRawType()),
+ eagerMap(type.getParameters(), parameter -> addPrefix(parameter, namespace)));
+ }
+
+ @Override
+ public Type visit(BoundTypeParameter type) {
+ return new BoundTypeParameter(
+ visit(type.getParameter()),
+ addPrefix(type.getValue(), namespace));
+ }
});
}
}
diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java
index c898525..0d38a88 100644
--- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java
+++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java
@@ -1,8 +1,7 @@
package org.zwobble.couscous.backends.csharp;
import org.zwobble.couscous.ast.*;
-import org.zwobble.couscous.ast.types.ScalarType;
-import org.zwobble.couscous.ast.types.Type;
+import org.zwobble.couscous.ast.types.*;
import org.zwobble.couscous.ast.visitors.NodeVisitor;
import org.zwobble.couscous.backends.SourceCodeWriter;
import org.zwobble.couscous.util.Action;
@@ -422,6 +421,34 @@ public class CsharpSerializer implements NodeVisitor {
writer.writeIdentifier(typeReference(type));
return null;
}
+
+ @Override
+ public Void visit(TypeParameter parameter) {
+ writer.writeIdentifier(parameter.getName());
+ return null;
+ }
+
+ @Override
+ public Void visit(ParameterizedType type) {
+ writer.writeIdentifier(typeReference(type.getRawType()));
+ writer.writeSymbol("<");
+ writer.writeWithSeparator(
+ type.getParameters(),
+ parameter -> writeTypeReference(parameter),
+ () -> {
+ writer.writeSymbol(",");
+ writer.writeSpace();
+ }
+ );
+ writer.writeSymbol(">");
+ return null;
+ }
+
+ @Override
+ public Void visit(BoundTypeParameter type) {
+ writeTypeReference(type.getValue());
+ return null;
+ }
});
}