diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
| commit | 20dc1e124d606cf4298d5112b6d3c4f6c74e9b68 (patch) | |
| tree | 47914e8b0356b2f40e3723eae650d3bb6f100dd4 /src/main/java/org/zwobble/couscous/backends/csharp | |
| parent | 8ac6cb16b727b5e3d28ab89fee1d169a4f74d9a1 (diff) | |
Introduce Type interface
Diffstat (limited to 'src/main/java/org/zwobble/couscous/backends/csharp')
4 files changed, 41 insertions, 25 deletions
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<TypeNode> classes) throws IOException { Files.write( directoryPath.resolve("Program.cs"), - Iterables.concat( + Iterables.<String>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<TypeName, TypeName> PRIMITIVES = ImmutableMap.<TypeName, TypeName>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<ScalarType, ScalarType> PRIMITIVES = ImmutableMap.<ScalarType, ScalarType>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<Optional<ExpressionNode>>() { @Override public Optional<ExpressionNode> 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<ExpressionNode> visit(TypeName receiver) { + public Optional<ExpressionNode> 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<Type>() { + @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<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(BooleanValue.REF, BOOLEAN_METHODS) .put(StringValue.REF, STRING_METHODS) .put(ObjectValues.BOXED_INT, BOXED_INT_METHODS) @@ -78,19 +78,19 @@ public class CsharpPrimitiveMethods { ImmutableMap.<String, PrimitiveStaticMethodGenerator>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<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(ObjectValues.BOXED_INT, STATIC_INT_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))); } @@ -100,7 +100,7 @@ public class CsharpPrimitiveMethods { ExpressionNode generate(ExpressionNode receiver, List<ExpressionNode> 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))); } 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<Void>() { + @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(); } } |
