From 20dc1e124d606cf4298d5112b6d3c4f6c74e9b68 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 9 Mar 2016 18:11:17 +0000 Subject: Introduce Type interface --- .../couscous/backends/csharp/CsharpBackend.java | 2 +- .../backends/csharp/CsharpCodeGenerator.java | 30 ++++++++++++++-------- .../backends/csharp/CsharpPrimitiveMethods.java | 18 ++++++------- .../couscous/backends/csharp/CsharpSerializer.java | 16 +++++++++--- 4 files changed, 41 insertions(+), 25 deletions(-) (limited to 'src/main/java/org/zwobble/couscous/backends/csharp') diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java index e98a526..3c58dae 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpBackend.java @@ -33,7 +33,7 @@ public class CsharpBackend implements Backend { public void compile(List classes) throws IOException { Files.write( directoryPath.resolve("Program.cs"), - Iterables.concat( + Iterables.concat( transform( classes, classNode -> compileClass(classNode)), 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 5a90dc7..8388421 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java @@ -2,6 +2,8 @@ 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.visitors.NodeTransformer; import org.zwobble.couscous.backends.Names; import org.zwobble.couscous.values.*; @@ -9,15 +11,16 @@ import org.zwobble.couscous.values.*; import java.util.Map; import java.util.Optional; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.util.Casts.tryCast; public class CsharpCodeGenerator { - private final static Map PRIMITIVES = ImmutableMap.builder() - .put(IntegerValue.REF, TypeName.of("int")) - .put(StringValue.REF, TypeName.of("string")) - .put(BooleanValue.REF, TypeName.of("bool")) - .put(ObjectValues.CLASS, TypeName.of("System.Type")) - .put(UnitValue.REF, TypeName.of("void")) + private final static Map PRIMITIVES = ImmutableMap.builder() + .put(IntegerValue.REF, ScalarType.of("int")) + .put(StringValue.REF, ScalarType.of("string")) + .put(BooleanValue.REF, ScalarType.of("bool")) + .put(ObjectValues.CLASS, ScalarType.of("System.Type")) + .put(UnitValue.REF, ScalarType.of("void")) .build(); private final String namespace; @@ -40,7 +43,7 @@ public class CsharpCodeGenerator { return nodeTransformer.transform(node); } - private TypeName transformType(TypeName type) { + private Type transformType(Type type) { if (PRIMITIVES.containsKey(type)) { return PRIMITIVES.get(type); } else { @@ -61,21 +64,26 @@ public class CsharpCodeGenerator { return call.getReceiver().accept(new Receiver.Mapper>() { @Override public Optional visit(ExpressionNode receiver) { - return CsharpPrimitiveMethods.getPrimitiveMethod(receiver.getType(), call.getMethodName()) + return CsharpPrimitiveMethods.getPrimitiveMethod(erasure(receiver.getType()), call.getMethodName()) .map(generator -> generator.generate( nodeTransformer.transformExpression(receiver), nodeTransformer.transformExpressions(call.getArguments()))); } @Override - public Optional visit(TypeName receiver) { + public Optional visit(ScalarType receiver) { return CsharpPrimitiveMethods.getPrimitiveStaticMethod(receiver, call.getMethodName()) .map(generator -> generator.generate(nodeTransformer.transformExpressions(call.getArguments()))); } }); } - private TypeName addPrefix(TypeName name, String namespace) { - return TypeName.of(namespace + "." + name.getQualifiedName()); + private Type addPrefix(Type type, String namespace) { + return type.accept(new Type.Visitor() { + @Override + public Type visit(ScalarType type) { + return ScalarType.of(namespace + "." + type.getQualifiedName()); + } + }); } } diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java index 72aa528..ced0a82 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpPrimitiveMethods.java @@ -2,7 +2,7 @@ package org.zwobble.couscous.backends.csharp; import com.google.common.collect.ImmutableMap; import org.zwobble.couscous.ast.ExpressionNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; @@ -67,8 +67,8 @@ public class CsharpPrimitiveMethods { STRING_METHODS = methods.build(); } - private static final Map> METHODS = - ImmutableMap.>builder() + private static final Map> METHODS = + ImmutableMap.>builder() .put(BooleanValue.REF, BOOLEAN_METHODS) .put(StringValue.REF, STRING_METHODS) .put(ObjectValues.BOXED_INT, BOXED_INT_METHODS) @@ -78,19 +78,19 @@ public class CsharpPrimitiveMethods { ImmutableMap.builder() .put("parseInt", arguments -> staticMethodCall( - TypeName.of("int"), + ScalarType.of("int"), "Parse", list(arguments.get(0)), - TypeName.of("int"))) + ScalarType.of("int"))) .build(); - private static final Map> STATIC_METHODS = - ImmutableMap.>builder() + private static final Map> STATIC_METHODS = + ImmutableMap.>builder() .put(ObjectValues.BOXED_INT, STATIC_INT_METHODS) .build(); - public static Optional getPrimitiveMethod(TypeName type, String methodName) { + public static Optional getPrimitiveMethod(ScalarType type, String methodName) { return Optional.ofNullable(METHODS.get(type)) .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); } @@ -100,7 +100,7 @@ public class CsharpPrimitiveMethods { ExpressionNode generate(ExpressionNode receiver, List arguments); } - public static Optional getPrimitiveStaticMethod(TypeName type, String methodName) { + public static Optional getPrimitiveStaticMethod(ScalarType type, String methodName) { return Optional.ofNullable(STATIC_METHODS.get(type)) .flatMap(methodsForType -> Optional.ofNullable(methodsForType.get(methodName))); } 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 39cd443..c898525 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java @@ -1,6 +1,8 @@ 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.visitors.NodeVisitor; import org.zwobble.couscous.backends.SourceCodeWriter; import org.zwobble.couscous.util.Action; @@ -66,7 +68,7 @@ public class CsharpSerializer implements NodeVisitor { } @Override - public Void visitType(TypeName value) { + public Void visitType(ScalarType value) { writer.writeKeyword("typeof"); writer.writeSymbol("("); writeTypeReference(value); @@ -413,11 +415,17 @@ public class CsharpSerializer implements NodeVisitor { } } - private void writeTypeReference(TypeName value) { - writer.writeIdentifier(typeReference(value)); + private void writeTypeReference(Type value) { + value.accept(new Type.Visitor() { + @Override + public Void visit(ScalarType type) { + writer.writeIdentifier(typeReference(type)); + return null; + } + }); } - private String typeReference(TypeName value) { + private String typeReference(ScalarType value) { return value.getQualifiedName(); } } -- cgit v1.2.3