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 --- .../org/zwobble/couscous/ast/AnnotationNode.java | 9 +-- .../org/zwobble/couscous/ast/AssignmentNode.java | 3 +- .../java/org/zwobble/couscous/ast/CastNode.java | 9 +-- .../java/org/zwobble/couscous/ast/ClassNode.java | 20 +++--- .../org/zwobble/couscous/ast/ClassNodeBuilder.java | 24 +++---- .../zwobble/couscous/ast/ConstructorCallNode.java | 15 +++-- .../org/zwobble/couscous/ast/ExpressionNode.java | 3 +- .../org/zwobble/couscous/ast/FieldAccessNode.java | 16 ++--- .../zwobble/couscous/ast/FieldDeclarationNode.java | 16 ++--- .../zwobble/couscous/ast/FormalArgumentNode.java | 3 +- .../org/zwobble/couscous/ast/InterfaceNode.java | 18 +++--- .../java/org/zwobble/couscous/ast/LiteralNode.java | 11 ++-- .../couscous/ast/LocalVariableDeclarationNode.java | 5 +- .../org/zwobble/couscous/ast/MethodCallNode.java | 20 +++--- .../java/org/zwobble/couscous/ast/MethodNode.java | 13 ++-- .../org/zwobble/couscous/ast/MethodSignature.java | 12 ++-- .../org/zwobble/couscous/ast/OperationNode.java | 9 +-- .../java/org/zwobble/couscous/ast/Receiver.java | 3 +- .../org/zwobble/couscous/ast/StaticReceiver.java | 9 +-- .../couscous/ast/TernaryConditionalNode.java | 3 +- .../zwobble/couscous/ast/ThisReferenceNode.java | 9 +-- .../org/zwobble/couscous/ast/TypeCoercionNode.java | 10 +-- .../java/org/zwobble/couscous/ast/TypeName.java | 68 -------------------- .../java/org/zwobble/couscous/ast/TypeNode.java | 6 +- .../zwobble/couscous/ast/VariableDeclaration.java | 9 +-- .../couscous/ast/VariableReferenceNode.java | 3 +- .../zwobble/couscous/ast/sugar/AnonymousClass.java | 8 +-- .../org/zwobble/couscous/ast/types/ScalarType.java | 73 ++++++++++++++++++++++ .../java/org/zwobble/couscous/ast/types/Type.java | 9 +++ .../java/org/zwobble/couscous/ast/types/Types.java | 12 ++++ .../couscous/ast/visitors/NodeTransformer.java | 18 ++++-- .../java/org/zwobble/couscous/backends/Names.java | 7 ++- .../couscous/backends/csharp/CsharpBackend.java | 2 +- .../backends/csharp/CsharpCodeGenerator.java | 30 +++++---- .../backends/csharp/CsharpPrimitiveMethods.java | 18 +++--- .../couscous/backends/csharp/CsharpSerializer.java | 16 +++-- .../backends/python/PythonCodeGenerator.java | 35 ++++++----- .../backends/python/PythonPrimitiveMethods.java | 14 ++--- .../couscous/frontends/java/GeneratedClosure.java | 5 +- .../java/JavaExpressionMethodReferenceReader.java | 10 ++- .../frontends/java/JavaExpressionReader.java | 30 ++++----- .../frontends/java/JavaLambdaExpressionReader.java | 6 +- .../couscous/frontends/java/JavaReader.java | 51 ++++++++------- .../frontends/java/JavaStatementReader.java | 11 ++-- .../zwobble/couscous/frontends/java/JavaTypes.java | 30 ++++----- .../org/zwobble/couscous/frontends/java/Scope.java | 18 +++--- .../zwobble/couscous/interpreter/Environment.java | 17 ++--- .../zwobble/couscous/interpreter/Evaluator.java | 9 ++- .../zwobble/couscous/interpreter/Interpreter.java | 7 ++- .../couscous/interpreter/InterpreterTypes.java | 18 +++--- .../couscous/interpreter/MapBackedProject.java | 14 +++-- .../org/zwobble/couscous/interpreter/Project.java | 4 +- .../couscous/interpreter/errors/InvalidCast.java | 12 ++-- .../interpreter/errors/UnexpectedValueType.java | 12 ++-- .../interpreter/types/InterpreterType.java | 6 +- .../types/IntrinsicInterpreterType.java | 33 +++++----- .../types/UserDefinedInterpreterType.java | 12 ++-- .../values/BoxedIntegerInterpreterValue.java | 2 +- .../couscous/interpreter/values/Callable.java | 5 +- .../interpreter/values/InterpreterValues.java | 4 +- .../couscous/interpreter/values/MethodValue.java | 8 +-- .../interpreter/values/StaticMethodValue.java | 8 +-- .../interpreter/values/TypeInterpreterValue.java | 8 +-- .../org/zwobble/couscous/values/BooleanValue.java | 6 +- .../org/zwobble/couscous/values/IntegerValue.java | 6 +- .../couscous/values/InternalCouscousValue.java | 4 +- .../org/zwobble/couscous/values/ObjectValues.java | 10 +-- .../zwobble/couscous/values/PrimitiveValue.java | 4 +- .../couscous/values/PrimitiveValueVisitor.java | 4 +- .../zwobble/couscous/values/PrimitiveValues.java | 4 +- .../org/zwobble/couscous/values/StringValue.java | 6 +- .../org/zwobble/couscous/values/TypeValue.java | 10 +-- .../org/zwobble/couscous/values/UnitValue.java | 6 +- 73 files changed, 540 insertions(+), 428 deletions(-) delete mode 100644 src/main/java/org/zwobble/couscous/ast/TypeName.java create mode 100644 src/main/java/org/zwobble/couscous/ast/types/ScalarType.java create mode 100644 src/main/java/org/zwobble/couscous/ast/types/Type.java create mode 100644 src/main/java/org/zwobble/couscous/ast/types/Types.java (limited to 'src/main/java') diff --git a/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java b/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java index e102af7..b70ed12 100644 --- a/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/AnnotationNode.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class AnnotationNode implements Node { - public static AnnotationNode annotation(TypeName type) { + public static AnnotationNode annotation(Type type) { return new AnnotationNode(type); } - private final TypeName type; + private final Type type; - private AnnotationNode(TypeName type) { + private AnnotationNode(Type type) { this.type = type; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java b/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java index ec781b3..64fb973 100644 --- a/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java +++ b/src/main/java/org/zwobble/couscous/ast/AssignmentNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -52,7 +53,7 @@ public class AssignmentNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return value.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/CastNode.java b/src/main/java/org/zwobble/couscous/ast/CastNode.java index 20ae9c6..75201e7 100644 --- a/src/main/java/org/zwobble/couscous/ast/CastNode.java +++ b/src/main/java/org/zwobble/couscous/ast/CastNode.java @@ -1,17 +1,18 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class CastNode implements ExpressionNode { - public static ExpressionNode cast(ExpressionNode expression, TypeName type) { + public static ExpressionNode cast(ExpressionNode expression, Type type) { return new CastNode(expression, type); } private final ExpressionNode expression; - private final TypeName type; + private final Type type; - private CastNode(ExpressionNode expression, TypeName type) { + private CastNode(ExpressionNode expression, Type type) { this.expression = expression; this.type = type; } @@ -21,7 +22,7 @@ public class CastNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/ClassNode.java b/src/main/java/org/zwobble/couscous/ast/ClassNode.java index 2342315..d50051c 100644 --- a/src/main/java/org/zwobble/couscous/ast/ClassNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ClassNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -7,7 +9,7 @@ import java.util.List; import java.util.Set; public class ClassNode implements TypeNode { - public static ClassNodeBuilder builder(TypeName name) { + public static ClassNodeBuilder builder(ScalarType name) { return new ClassNodeBuilder(name); } public static ClassNodeBuilder builder(String name) { @@ -15,9 +17,9 @@ public class ClassNode implements TypeNode { } public static ClassNode declareClass( - TypeName name, + ScalarType name, List typeParameters, - Set superTypes, + Set superTypes, List fields, List staticConstructor, ConstructorNode constructor, @@ -26,18 +28,18 @@ public class ClassNode implements TypeNode { return new ClassNode(name, typeParameters, superTypes, fields, staticConstructor, constructor, methods); } - private final TypeName name; + private final ScalarType name; private final List typeParameters; - private final Set superTypes; + private final Set superTypes; private final List fields; private final List staticConstructor; private final ConstructorNode constructor; private final List methods; public ClassNode( - TypeName name, + ScalarType name, List typeParameters, - Set superTypes, + Set superTypes, List fields, List staticConstructor, ConstructorNode constructor, @@ -53,7 +55,7 @@ public class ClassNode implements TypeNode { } @Override - public TypeName getName() { + public ScalarType getName() { return name; } @@ -63,7 +65,7 @@ public class ClassNode implements TypeNode { } @Override - public Set getSuperTypes() { + public Set getSuperTypes() { return superTypes; } diff --git a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java index 282df33..9dc5098 100644 --- a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java +++ b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java @@ -3,6 +3,8 @@ package org.zwobble.couscous.ast; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.values.UnitValue; import java.util.List; @@ -15,15 +17,15 @@ import static org.zwobble.couscous.ast.VariableDeclaration.var; import static org.zwobble.couscous.util.ExtraLists.list; public class ClassNodeBuilder { - private final TypeName name; - private final ImmutableSet.Builder superTypes; + private final ScalarType name; + private final ImmutableSet.Builder superTypes; private final ImmutableList.Builder typeParameters; private final ImmutableList.Builder fields; private List staticConstructor; private Optional constructor; private final ImmutableList.Builder methods; - public ClassNodeBuilder(TypeName name) { + public ClassNodeBuilder(ScalarType name) { this.name = name; this.typeParameters = ImmutableList.builder(); this.superTypes = ImmutableSet.builder(); @@ -34,7 +36,7 @@ public class ClassNodeBuilder { } public ClassNodeBuilder(String name) { - this(TypeName.of(name)); + this(ScalarType.of(name)); } public ClassNodeBuilder addTypeParameter(String name) { @@ -43,15 +45,15 @@ public class ClassNodeBuilder { } public ClassNodeBuilder addSuperType(String type) { - this.superTypes.add(TypeName.of(type)); + this.superTypes.add(ScalarType.of(type)); return this; } - public ClassNodeBuilder staticField(String name, TypeName type) { + public ClassNodeBuilder staticField(String name, ScalarType type) { return field(FieldDeclarationNode.staticField(name, type)); } - public ClassNodeBuilder field(String name, TypeName type) { + public ClassNodeBuilder field(String name, ScalarType type) { return field(FieldDeclarationNode.field(name, type)); } @@ -132,7 +134,7 @@ public class ClassNodeBuilder { private final Function, T> build; private final ImmutableList.Builder annotations; private final ImmutableList.Builder arguments; - private TypeName returnType = UnitValue.REF; + private ScalarType returnType = UnitValue.REF; private final ImmutableList.Builder statements; public MethodBuilder(Function, T> build) { @@ -142,7 +144,7 @@ public class ClassNodeBuilder { this.statements = ImmutableList.builder(); } - public MethodBuilder annotation(TypeName type) { + public MethodBuilder annotation(ScalarType type) { annotations.add(AnnotationNode.annotation(type)); return this; } @@ -151,12 +153,12 @@ public class ClassNodeBuilder { return ThisReferenceNode.thisReference(name); } - public MethodBuilder argument(Identifier id, String name, TypeName type) { + public MethodBuilder argument(Identifier id, String name, ScalarType type) { arguments.add(formalArg(var(id, name, type))); return this; } - public MethodBuilder returns(TypeName returnType) { + public MethodBuilder returns(ScalarType returnType) { this.returnType = returnType; return this; } diff --git a/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java b/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java index b5439bf..5a597d1 100644 --- a/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ConstructorCallNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -7,23 +8,25 @@ import java.util.List; public class ConstructorCallNode implements ExpressionNode { public static ConstructorCallNode constructorCall( - TypeName type, - List arguments) { + Type type, + List arguments) + { return new ConstructorCallNode(type, arguments); } - private final TypeName type; + private final Type type; private final List arguments; public ConstructorCallNode( - TypeName type, - List arguments) { + Type type, + List arguments) + { this.type = type; this.arguments = arguments; } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java b/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java index a7842d3..2452183 100644 --- a/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ExpressionNode.java @@ -1,11 +1,12 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public interface ExpressionNode extends Node { - TypeName getType(); + Type getType(); T accept(ExpressionNodeMapper visitor); @Override diff --git a/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java b/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java index a949630..e417638 100644 --- a/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java +++ b/src/main/java/org/zwobble/couscous/ast/FieldAccessNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.AssignableExpressionNodeVisitor; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -11,15 +13,15 @@ public class FieldAccessNode implements AssignableExpressionNode { public static FieldAccessNode fieldAccess( ExpressionNode left, String fieldName, - TypeName type) + Type type) { return fieldAccess(instanceReceiver(left), fieldName, type); } public static FieldAccessNode fieldAccess( - TypeName left, + ScalarType left, String fieldName, - TypeName type) + Type type) { return fieldAccess(staticReceiver(left), fieldName, type); } @@ -27,19 +29,19 @@ public class FieldAccessNode implements AssignableExpressionNode { public static FieldAccessNode fieldAccess( Receiver left, String fieldName, - TypeName type) + Type type) { return new FieldAccessNode(left, fieldName, type); } private final Receiver left; private final String fieldName; - private final TypeName type; + private final Type type; public FieldAccessNode( Receiver left, String fieldName, - TypeName type) + Type type) { this.left = left; this.fieldName = fieldName; @@ -54,7 +56,7 @@ public class FieldAccessNode implements AssignableExpressionNode { return fieldName; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java b/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java index a9f9b76..a207b82 100644 --- a/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/FieldDeclarationNode.java @@ -1,26 +1,27 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class FieldDeclarationNode implements Node { - public static FieldDeclarationNode field(String name, TypeName type) { + public static FieldDeclarationNode field(String name, Type type) { return field(false, name, type); } - public static FieldDeclarationNode staticField(String name, TypeName type) { + public static FieldDeclarationNode staticField(String name, Type type) { return field(true, name, type); } - public static FieldDeclarationNode field(boolean isStatic, String name, TypeName type) { + public static FieldDeclarationNode field(boolean isStatic, String name, Type type) { return new FieldDeclarationNode(isStatic, name, type); } private final boolean isStatic; private final String name; - private final TypeName type; + private final Type type; - private FieldDeclarationNode(boolean isStatic, String name, TypeName type) { + private FieldDeclarationNode(boolean isStatic, String name, Type type) { this.isStatic = isStatic; this.name = name; this.type = type; @@ -34,7 +35,7 @@ public class FieldDeclarationNode implements Node { return name; } - public TypeName getType() { + public Type getType() { return type; } @@ -45,7 +46,8 @@ public class FieldDeclarationNode implements Node { public FieldDeclarationNode transform(NodeTransformer transformer) { return new FieldDeclarationNode( - isStatic, name, + isStatic, + name, transformer.transform(type)); } diff --git a/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java b/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java index f7d7f42..7df3ead 100644 --- a/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java +++ b/src/main/java/org/zwobble/couscous/ast/FormalArgumentNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -18,7 +19,7 @@ public class FormalArgumentNode implements VariableNode, Node { return declaration; } - public TypeName getType() { + public Type getType() { return declaration.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java b/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java index e5b876d..97faae4 100644 --- a/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java +++ b/src/main/java/org/zwobble/couscous/ast/InterfaceNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -8,23 +10,23 @@ import java.util.Set; public class InterfaceNode implements TypeNode { public static InterfaceNode declareInterface( - TypeName name, + ScalarType name, List typeParameters, - Set superTypes, + Set superTypes, List methods) { return new InterfaceNode(name, typeParameters, superTypes, methods); } - private final TypeName name; + private final ScalarType name; private final List typeParameters; - private final Set superTypes; + private final Set superTypes; private final List methods; private InterfaceNode( - TypeName name, + ScalarType name, List typeParameters, - Set superTypes, + Set superTypes, List methodNodes) { this.name = name; @@ -34,7 +36,7 @@ public class InterfaceNode implements TypeNode { } @Override - public TypeName getName() { + public ScalarType getName() { return name; } @@ -44,7 +46,7 @@ public class InterfaceNode implements TypeNode { } @Override - public Set getSuperTypes() { + public Set getSuperTypes() { return superTypes; } diff --git a/src/main/java/org/zwobble/couscous/ast/LiteralNode.java b/src/main/java/org/zwobble/couscous/ast/LiteralNode.java index a76ca80..bb38562 100644 --- a/src/main/java/org/zwobble/couscous/ast/LiteralNode.java +++ b/src/main/java/org/zwobble/couscous/ast/LiteralNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.values.*; @@ -25,23 +26,23 @@ public class LiteralNode implements ExpressionNode { return new LiteralNode(value(value), BooleanValue.REF); } - public static LiteralNode literal(TypeName type) { + public static LiteralNode literal(ScalarType type) { return new LiteralNode(value(type), ObjectValues.CLASS); } - public static LiteralNode of(PrimitiveValue value, TypeName type) { + public static LiteralNode of(PrimitiveValue value, ScalarType type) { return new LiteralNode(value, type); } private final PrimitiveValue value; - private final TypeName type; + private final ScalarType type; - private LiteralNode(PrimitiveValue value, TypeName type) { + private LiteralNode(PrimitiveValue value, ScalarType type) { this.value = value; this.type = type; } - public TypeName getType() { + public ScalarType getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java b/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java index 03e4226..5b571d0 100644 --- a/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/LocalVariableDeclarationNode.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.ast; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.ast.visitors.StatementNodeMapper; @@ -10,7 +11,7 @@ public class LocalVariableDeclarationNode implements VariableNode, StatementNode public static LocalVariableDeclarationNode localVariableDeclaration( Identifier id, String name, - TypeName type, + Type type, ExpressionNode initialValue) { return localVariableDeclaration(var(id, name, type), initialValue); } @@ -43,7 +44,7 @@ public class LocalVariableDeclarationNode implements VariableNode, StatementNode return declaration.getName(); } - public TypeName getType() { + public Type getType() { return declaration.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java index c4e0321..43993b8 100644 --- a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java +++ b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java @@ -1,5 +1,7 @@ package org.zwobble.couscous.ast; +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.NodeTransformer; @@ -14,15 +16,15 @@ public class MethodCallNode implements ExpressionNode { String className, String methodName, List arguments, - TypeName type) { - return staticMethodCall(TypeName.of(className), methodName, arguments, type); + Type type) { + return staticMethodCall(ScalarType.of(className), methodName, arguments, type); } public static ExpressionNode staticMethodCall( - TypeName className, + ScalarType className, String methodName, List arguments, - TypeName type) { + Type type) { return MethodCallNode.methodCall(staticReceiver(className), methodName, arguments, type); } @@ -30,7 +32,7 @@ public class MethodCallNode implements ExpressionNode { ExpressionNode receiver, String methodName, List arguments, - TypeName type) + Type type) { return methodCall(instanceReceiver(receiver), methodName, arguments, type); } @@ -39,7 +41,7 @@ public class MethodCallNode implements ExpressionNode { Receiver receiver, String methodName, List arguments, - TypeName type) + Type type) { return new MethodCallNode(receiver, methodName, arguments, type); } @@ -47,13 +49,13 @@ public class MethodCallNode implements ExpressionNode { private final Receiver receiver; private final String methodName; private final List arguments; - private final TypeName type; + private final Type type; private MethodCallNode( Receiver receiver, String methodName, List arguments, - TypeName type) + Type type) { this.receiver = receiver; this.methodName = methodName; @@ -73,7 +75,7 @@ public class MethodCallNode implements ExpressionNode { return arguments; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/MethodNode.java b/src/main/java/org/zwobble/couscous/ast/MethodNode.java index 4909093..8589eb1 100644 --- a/src/main/java/org/zwobble/couscous/ast/MethodNode.java +++ b/src/main/java/org/zwobble/couscous/ast/MethodNode.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.ast; import com.google.common.collect.ImmutableList; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; import org.zwobble.couscous.values.UnitValue; @@ -27,7 +28,7 @@ public class MethodNode implements Node { private final String name; private final ImmutableList.Builder arguments = ImmutableList.builder(); - private TypeName returnType = UnitValue.REF; + private Type returnType = UnitValue.REF; private final ImmutableList.Builder body = ImmutableList.builder(); @@ -60,7 +61,7 @@ public class MethodNode implements Node { return this; } - public Builder returns(TypeName returnType) { + public Builder returns(Type returnType) { this.returnType = returnType; return this; } @@ -91,7 +92,7 @@ public class MethodNode implements Node { boolean isStatic, String name, List arguments, - TypeName returnType, + Type returnType, Optional> body) { return new MethodNode(annotations, isStatic, name, arguments, returnType, body); @@ -101,7 +102,7 @@ public class MethodNode implements Node { private final boolean isStatic; private final String name; private final List arguments; - private final TypeName returnType; + private final Type returnType; private final Optional> body; private MethodNode( @@ -109,7 +110,7 @@ public class MethodNode implements Node { boolean isStatic, String name, List arguments, - TypeName returnType, + Type returnType, Optional> body) { this.annotations = annotations; @@ -136,7 +137,7 @@ public class MethodNode implements Node { return arguments; } - public TypeName getReturnType() { + public Type getReturnType() { return returnType; } diff --git a/src/main/java/org/zwobble/couscous/ast/MethodSignature.java b/src/main/java/org/zwobble/couscous/ast/MethodSignature.java index 908e673..93d4f58 100644 --- a/src/main/java/org/zwobble/couscous/ast/MethodSignature.java +++ b/src/main/java/org/zwobble/couscous/ast/MethodSignature.java @@ -1,13 +1,15 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; + import java.util.List; public class MethodSignature { private final String name; - private final List arguments; - private final TypeName returnType; + private final List arguments; + private final Type returnType; - public MethodSignature(String name, List arguments, TypeName returnType) { + public MethodSignature(String name, List arguments, Type returnType) { this.name = name; this.arguments = arguments; this.returnType = returnType; @@ -17,11 +19,11 @@ public class MethodSignature { return name; } - public List getArguments() { + public List getArguments() { return arguments; } - public TypeName getReturnType() { + public Type getReturnType() { return returnType; } diff --git a/src/main/java/org/zwobble/couscous/ast/OperationNode.java b/src/main/java/org/zwobble/couscous/ast/OperationNode.java index 4348c33..d6453e3 100644 --- a/src/main/java/org/zwobble/couscous/ast/OperationNode.java +++ b/src/main/java/org/zwobble/couscous/ast/OperationNode.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; import java.util.List; public class OperationNode implements ExpressionNode { - public static OperationNode operation(Operator operator, List arguments, TypeName type) { + public static OperationNode operation(Operator operator, List arguments, Type type) { return new OperationNode(operator, arguments, type); } private final Operator operator; private final List arguments; - private TypeName type; + private Type type; - OperationNode(Operator operator, List arguments, TypeName type) { + OperationNode(Operator operator, List arguments, Type type) { this.operator = operator; this.arguments = arguments; this.type = type; @@ -41,7 +42,7 @@ public class OperationNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/Receiver.java b/src/main/java/org/zwobble/couscous/ast/Receiver.java index 1afb9b7..90395d2 100644 --- a/src/main/java/org/zwobble/couscous/ast/Receiver.java +++ b/src/main/java/org/zwobble/couscous/ast/Receiver.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.NodeTransformer; public interface Receiver extends Node { @@ -8,6 +9,6 @@ public interface Receiver extends Node { interface Mapper { T visit(ExpressionNode receiver); - T visit(TypeName receiver); + T visit(ScalarType receiver); } } diff --git a/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java b/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java index 84cf266..3c6e32c 100644 --- a/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java +++ b/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.NodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class StaticReceiver implements Receiver { - public static Receiver staticReceiver(TypeName type) { + public static Receiver staticReceiver(ScalarType type) { return new StaticReceiver(type); } - private final TypeName type; + private final ScalarType type; - public StaticReceiver(TypeName type) { + public StaticReceiver(ScalarType type) { this.type = type; } - public TypeName getType() { + public ScalarType getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java b/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java index 3caec58..2d46473 100644 --- a/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java +++ b/src/main/java/org/zwobble/couscous/ast/TernaryConditionalNode.java @@ -1,5 +1,6 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -50,7 +51,7 @@ public class TernaryConditionalNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return ifTrue.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java b/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java index ebc2d8d..cdf995b 100644 --- a/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ThisReferenceNode.java @@ -1,20 +1,21 @@ package org.zwobble.couscous.ast; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; public class ThisReferenceNode implements ExpressionNode, ReferenceNode { - public static ThisReferenceNode thisReference(TypeName type) { + public static ThisReferenceNode thisReference(Type type) { return new ThisReferenceNode(type); } - private final TypeName type; + private final Type type; - private ThisReferenceNode(TypeName type) { + private ThisReferenceNode(Type type) { this.type = type; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java b/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java index 680ecea..c8c2a33 100644 --- a/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java +++ b/src/main/java/org/zwobble/couscous/ast/TypeCoercionNode.java @@ -1,17 +1,19 @@ package org.zwobble.couscous.ast; +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.NodeTransformer; public class TypeCoercionNode implements ExpressionNode { - public static TypeCoercionNode typeCoercion(ExpressionNode expression, TypeName type) { + public static TypeCoercionNode typeCoercion(ExpressionNode expression, Type type) { return new TypeCoercionNode(expression, type); } private final ExpressionNode expression; - private final TypeName type; + private final Type type; - private TypeCoercionNode(ExpressionNode expression, TypeName type) { + private TypeCoercionNode(ExpressionNode expression, Type type) { this.expression = expression; this.type = type; } @@ -21,7 +23,7 @@ public class TypeCoercionNode implements ExpressionNode { } @Override - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/TypeName.java b/src/main/java/org/zwobble/couscous/ast/TypeName.java deleted file mode 100644 index e2c32d0..0000000 --- a/src/main/java/org/zwobble/couscous/ast/TypeName.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.zwobble.couscous.ast; - -import com.google.common.base.Strings; - -import java.util.Optional; - -public class TypeName { - public static TypeName of(String qualifiedName) { - if (Strings.isNullOrEmpty(qualifiedName)) { - throw new IllegalArgumentException("qualifiedName cannot be blank"); - } - return new TypeName(qualifiedName); - } - - private final String qualifiedName; - - private TypeName(String qualifiedName) { - this.qualifiedName = qualifiedName; - } - - public String getQualifiedName() { - return qualifiedName; - } - - public String getSimpleName() { - return qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1); - } - - public Optional getPackage() { - int lastDot = qualifiedName.lastIndexOf("."); - if (lastDot == -1) { - return Optional.empty(); - } else { - return Optional.of(qualifiedName.substring(0, lastDot)); - } - } - - @Override - public String toString() { - return "TypeName(qualifiedName=" + qualifiedName + ")"; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result - + ((qualifiedName == null) ? 0 : qualifiedName.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - TypeName other = (TypeName) obj; - if (qualifiedName == null) { - if (other.qualifiedName != null) - return false; - } else if (!qualifiedName.equals(other.qualifiedName)) - return false; - return true; - } -} diff --git a/src/main/java/org/zwobble/couscous/ast/TypeNode.java b/src/main/java/org/zwobble/couscous/ast/TypeNode.java index e3a78af..af4ab81 100644 --- a/src/main/java/org/zwobble/couscous/ast/TypeNode.java +++ b/src/main/java/org/zwobble/couscous/ast/TypeNode.java @@ -1,14 +1,16 @@ package 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 java.util.List; import java.util.Set; public interface TypeNode extends Node { - TypeName getName(); + ScalarType getName(); List getTypeParameters(); - Set getSuperTypes(); + Set getSuperTypes(); List getMethods(); TypeNode transform(NodeTransformer transformer); } diff --git a/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java b/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java index 5932371..55d4efb 100644 --- a/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java +++ b/src/main/java/org/zwobble/couscous/ast/VariableDeclaration.java @@ -1,17 +1,18 @@ package org.zwobble.couscous.ast; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; public class VariableDeclaration { - public static VariableDeclaration var(Identifier id, String name, TypeName type) { + public static VariableDeclaration var(Identifier id, String name, Type type) { return new VariableDeclaration(id, name, type); } private final Identifier id; private final String name; - private final TypeName type; + private final Type type; - private VariableDeclaration(Identifier id, String name, TypeName type) { + private VariableDeclaration(Identifier id, String name, Type type) { this.id = id; this.name = name; this.type = type; @@ -25,7 +26,7 @@ public class VariableDeclaration { return name; } - public TypeName getType() { + public Type getType() { return type; } diff --git a/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java b/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java index f9800a5..2b81fbb 100644 --- a/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java +++ b/src/main/java/org/zwobble/couscous/ast/VariableReferenceNode.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.ast; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.ast.visitors.AssignableExpressionNodeVisitor; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.ast.visitors.NodeTransformer; @@ -34,7 +35,7 @@ public class VariableReferenceNode implements AssignableExpressionNode, Referenc } @Override - public TypeName getType() { + public Type getType() { return referent.getType(); } diff --git a/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java b/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java index fea72be..f8a8c48 100644 --- a/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java +++ b/src/main/java/org/zwobble/couscous/ast/sugar/AnonymousClass.java @@ -2,18 +2,18 @@ package org.zwobble.couscous.ast.sugar; import org.zwobble.couscous.ast.FieldDeclarationNode; import org.zwobble.couscous.ast.MethodNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import java.util.List; import java.util.Set; public class AnonymousClass { - private final Set superTypes; + private final Set superTypes; private final List fields; private final List methods; public AnonymousClass( - Set superTypes, + Set superTypes, List fields, List methods) { @@ -23,7 +23,7 @@ public class AnonymousClass { this.methods = methods; } - public Set getSuperTypes() { + public Set getSuperTypes() { return superTypes; } diff --git a/src/main/java/org/zwobble/couscous/ast/types/ScalarType.java b/src/main/java/org/zwobble/couscous/ast/types/ScalarType.java new file mode 100644 index 0000000..69b9f97 --- /dev/null +++ b/src/main/java/org/zwobble/couscous/ast/types/ScalarType.java @@ -0,0 +1,73 @@ +package org.zwobble.couscous.ast.types; + +import com.google.common.base.Strings; + +import java.util.Optional; + +public class ScalarType implements Type { + public static ScalarType of(String qualifiedName) { + if (Strings.isNullOrEmpty(qualifiedName)) { + throw new IllegalArgumentException("qualifiedName cannot be blank"); + } + return new ScalarType(qualifiedName); + } + + private final String qualifiedName; + + private ScalarType(String qualifiedName) { + this.qualifiedName = qualifiedName; + } + + public String getQualifiedName() { + return qualifiedName; + } + + public String getSimpleName() { + return qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1); + } + + public Optional getPackage() { + int lastDot = qualifiedName.lastIndexOf("."); + if (lastDot == -1) { + return Optional.empty(); + } else { + return Optional.of(qualifiedName.substring(0, lastDot)); + } + } + + @Override + public String toString() { + return "ScalarType(qualifiedName=" + qualifiedName + ")"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((qualifiedName == null) ? 0 : qualifiedName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ScalarType other = (ScalarType) obj; + if (qualifiedName == null) { + if (other.qualifiedName != null) + return false; + } else if (!qualifiedName.equals(other.qualifiedName)) + return false; + return true; + } + + @Override + public T accept(Visitor visitor) { + return visitor.visit(this); + } +} diff --git a/src/main/java/org/zwobble/couscous/ast/types/Type.java b/src/main/java/org/zwobble/couscous/ast/types/Type.java new file mode 100644 index 0000000..e936279 --- /dev/null +++ b/src/main/java/org/zwobble/couscous/ast/types/Type.java @@ -0,0 +1,9 @@ +package org.zwobble.couscous.ast.types; + +public interface Type { + T accept(Visitor visitor); + + interface Visitor { + T visit(ScalarType type); + } +} diff --git a/src/main/java/org/zwobble/couscous/ast/types/Types.java b/src/main/java/org/zwobble/couscous/ast/types/Types.java new file mode 100644 index 0000000..c222c54 --- /dev/null +++ b/src/main/java/org/zwobble/couscous/ast/types/Types.java @@ -0,0 +1,12 @@ +package org.zwobble.couscous.ast.types; + +public class Types { + public static ScalarType erasure(Type type) { + return type.accept(new Type.Visitor() { + @Override + public ScalarType visit(ScalarType type) { + return type; + } + }); + } +} diff --git a/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java b/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java index 5d0539a..38c5709 100644 --- a/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java +++ b/src/main/java/org/zwobble/couscous/ast/visitors/NodeTransformer.java @@ -5,6 +5,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import java.util.List; import java.util.Map; @@ -154,7 +156,7 @@ public class NodeTransformer { public static class Builder { private Function> transformExpression = expression -> Optional.empty(); - private Function transformType = type -> type; + private Function transformType = type -> type; private Function transformMethodName = MethodSignature::getName; public Builder transformExpression( @@ -165,7 +167,7 @@ public class NodeTransformer { } public Builder transformType( - Function transformType) + Function transformType) { this.transformType = transformType; return this; @@ -182,12 +184,12 @@ public class NodeTransformer { } private final Function> transformExpression; - private final Function transformType; + private final Function transformType; private final Function transformMethodName; private NodeTransformer( Function> transformExpression, - Function transformType, + Function transformType, Function transformMethodName) { this.transformExpression = transformExpression; @@ -195,10 +197,14 @@ public class NodeTransformer { this.transformMethodName = transformMethodName; } - public TypeName transform(TypeName type) { + public Type transform(Type type) { return transformType.apply(type); } + public ScalarType transform(ScalarType type) { + return (ScalarType)transformType.apply(type); + } + public String transformMethodName(MethodSignature signature) { return transformMethodName.apply(signature); } @@ -256,7 +262,7 @@ public class NodeTransformer { return transformList(annotations, this::transformAnnotation); } - public Set transformTypes(Set superTypes) { + public Set transformTypes(Set superTypes) { return transformSet(superTypes, this::transform); } diff --git a/src/main/java/org/zwobble/couscous/backends/Names.java b/src/main/java/org/zwobble/couscous/backends/Names.java index d467296..094de02 100644 --- a/src/main/java/org/zwobble/couscous/backends/Names.java +++ b/src/main/java/org/zwobble/couscous/backends/Names.java @@ -3,9 +3,10 @@ package org.zwobble.couscous.backends; import com.google.common.base.Joiner; import com.google.common.collect.Iterables; import org.zwobble.couscous.ast.MethodSignature; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import static com.google.common.collect.Iterables.transform; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.util.ExtraLists.list; public class Names { @@ -16,7 +17,7 @@ public class Names { list(typeToString(signature.getReturnType())))); } - private static String typeToString(TypeName argument) { - return argument.getQualifiedName().replace('.', '_'); + private static String typeToString(Type argument) { + return erasure(argument).getQualifiedName().replace('.', '_'); } } 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(); } } 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 generateImports(TypeNode classNode) { - Set classes = findReferencedClasses(classNode); + Set 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 findReferencedClasses(TypeNode classNode) { + private static Set findReferencedClasses(TypeNode classNode) { return NodeStructure.descendantNodes(classNode) - .flatMap(node -> node.accept(new NodeMapperWithDefault>(Stream.empty()) { + .flatMap(node -> node.accept(new NodeMapperWithDefault>(Stream.empty()) { @Override - public Stream visit(StaticReceiver receiver) { + public Stream visit(StaticReceiver receiver) { return Stream.of(receiver.getType()); } @Override - public Stream visit(ConstructorCallNode call) { - return Stream.of(call.getType()); + public Stream visit(ConstructorCallNode call) { + return Stream.of(erasure(call.getType())); } @Override - public Stream visit(TypeCoercionNode typeCoercion) { + public Stream visit(TypeCoercionNode typeCoercion) { if (isIntegerBox(typeCoercion)) { return Stream.of(ObjectValues.BOXED_INT); } else { @@ -116,7 +119,7 @@ public class PythonCodeGenerator { } @Override - public Stream visit(LiteralNode literal) { + public Stream 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>() { @Override public Optional 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 visit(TypeName receiver) { + public Optional 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 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> METHODS = - ImmutableMap.>builder() + private static final Map> METHODS = + ImmutableMap.>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> STATIC_METHODS = - ImmutableMap.>builder() + private static final Map> STATIC_METHODS = + ImmutableMap.>builder() .put(InternalCouscousValue.REF, INTERNAL_METHODS) .put(ObjectValues.BOXED_INT, BOXED_INT_STATIC_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))); } @@ -148,7 +148,7 @@ public class PythonPrimitiveMethods { PythonExpressionNode generate(PythonExpressionNode 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/frontends/java/GeneratedClosure.java b/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java index 9386ff6..18f43be 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/GeneratedClosure.java @@ -2,8 +2,7 @@ package org.zwobble.couscous.frontends.java; import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.ReferenceNode; -import org.zwobble.couscous.ast.TypeName; -import org.zwobble.couscous.ast.VariableDeclaration; +import org.zwobble.couscous.ast.types.ScalarType; import java.util.List; @@ -20,7 +19,7 @@ public class GeneratedClosure { return classNode; } - public TypeName getType() { + public ScalarType getType() { return classNode.getName(); } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java index 545b1c0..b08bd0c 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java @@ -5,8 +5,12 @@ import org.eclipse.jdt.core.dom.ExpressionMethodReference; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.Modifier; -import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.ExpressionNode; +import org.zwobble.couscous.ast.FormalArgumentNode; +import org.zwobble.couscous.ast.VariableReferenceNode; import org.zwobble.couscous.ast.sugar.Lambda; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import java.util.List; @@ -52,11 +56,11 @@ public class JavaExpressionMethodReferenceReader { IMethodBinding methodBinding = expression.resolveMethodBinding(); String methodName = expression.getName().getIdentifier(); List arguments = eagerMap(formalArguments, VariableReferenceNode::reference); - TypeName type = typeOf(methodBinding.getReturnType()); + Type type = typeOf(methodBinding.getReturnType()); if (Modifier.isStatic(methodBinding.getModifiers())) { return staticMethodCall( - typeOf(methodBinding.getDeclaringClass()), + Types.erasure(typeOf(methodBinding.getDeclaringClass())), methodName, arguments, type); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java index 3d8e1c0..98f2686 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.IntegerValue; import org.zwobble.couscous.values.ObjectValues; @@ -31,12 +33,12 @@ public class JavaExpressionReader { this.javaReader = javaReader; } - ExpressionNode readExpression(TypeName targetType, Expression expression) { + ExpressionNode readExpression(Type targetType, Expression expression) { ExpressionNode couscousExpression = readExpressionWithoutBoxing(expression); return coerceExpression(targetType, couscousExpression); } - static ExpressionNode coerceExpression(TypeName targetType, ExpressionNode couscousExpression) { + static ExpressionNode coerceExpression(Type targetType, ExpressionNode couscousExpression) { if (targetType.equals(couscousExpression.getType())) { return couscousExpression; } else { @@ -44,7 +46,7 @@ public class JavaExpressionReader { } } - private static TypeName unboxedType(TypeName type) { + private static Type unboxedType(Type type) { if (type.equals(ObjectValues.BOXED_INT)) { return IntegerValue.REF; } else if (type.equals(ObjectValues.BOXED_BOOLEAN)) { @@ -131,7 +133,7 @@ public class JavaExpressionReader { } private ExpressionNode readTypeLiteral(TypeLiteral expression) { - return literal(typeOf(expression.getType())); + return literal(Types.erasure(typeOf(expression.getType()))); } private ExpressionNode readSimpleName(SimpleName expression) { @@ -148,9 +150,9 @@ public class JavaExpressionReader { return scope.reference(binding.getKey()); } else { boolean isStatic = Modifier.isStatic(binding.getModifiers()); - TypeName classType = typeOf(binding.getDeclaringClass()); + Type classType = typeOf(binding.getDeclaringClass()); Receiver receiver = isStatic - ? new StaticReceiver(classType) + ? new StaticReceiver(Types.erasure(classType)) : new InstanceReceiver(ThisReferenceNode.thisReference(classType)); return FieldAccessNode.fieldAccess( receiver, @@ -167,7 +169,7 @@ public class JavaExpressionReader { IVariableBinding fieldBinding = expression.resolveFieldBinding(); boolean isStatic = Modifier.isStatic(fieldBinding.getModifiers()); Receiver receiver = isStatic - ? new StaticReceiver(typeOf(fieldBinding.getDeclaringClass())) + ? new StaticReceiver(Types.erasure(typeOf(fieldBinding.getDeclaringClass()))) : new InstanceReceiver(readExpressionWithoutBoxing(expression.getExpression())); return FieldAccessNode.fieldAccess( @@ -182,13 +184,13 @@ public class JavaExpressionReader { List arguments = readArguments( expression.resolveMethodBinding(), expression.arguments()); - final TypeName type = typeOf(expression); + final Type type = typeOf(expression); IMethodBinding methodBinding = expression.resolveMethodBinding(); - TypeName receiverType = typeOf(methodBinding.getDeclaringClass()); + Type receiverType = typeOf(methodBinding.getDeclaringClass()); if ((Modifier.isStatic(methodBinding.getModifiers()))) { return staticMethodCall( - receiverType, + Types.erasure(receiverType), methodName, arguments, type); @@ -268,15 +270,15 @@ public class JavaExpressionReader { } private static boolean isPrimitiveOperation(InfixExpression expression) { - TypeName leftOperandType = typeOf(expression.getLeftOperand()); - TypeName rightOperandType = typeOf(expression.getRightOperand()); + Type leftOperandType = typeOf(expression.getLeftOperand()); + Type rightOperandType = typeOf(expression.getRightOperand()); return isPrimitive(leftOperandType) || isPrimitive(rightOperandType) || (expression.getOperator() != InfixExpression.Operator.NOT_EQUALS && expression.getOperator() != InfixExpression.Operator.EQUALS); } - private static boolean isPrimitive(TypeName type) { + private static boolean isPrimitive(Type type) { return type.equals(BooleanValue.REF) || type.equals(IntegerValue.REF); } @@ -297,7 +299,7 @@ public class JavaExpressionReader { } private ExpressionNode readConditionalExpression(ConditionalExpression expression) { - TypeName type = typeOf(expression); + Type type = typeOf(expression); return TernaryConditionalNode.ternaryConditional( readExpression(BooleanValue.REF, expression.getExpression()), readExpression(type, expression.getThenExpression()), diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java index ee4fc06..30dc2f8 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java @@ -3,13 +3,13 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.FormalArgumentNode; import org.zwobble.couscous.ast.StatementNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.*; import org.zwobble.couscous.ast.sugar.Lambda; +import org.zwobble.couscous.ast.types.Type; import java.util.List; import java.util.Optional; -import static java.util.Arrays.asList; import static org.zwobble.couscous.ast.ReturnNode.returns; import static org.zwobble.couscous.ast.sugar.Lambda.lambda; import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf; @@ -44,7 +44,7 @@ public class JavaLambdaExpressionReader { } private List readLambdaExpressionBody(Scope scope, LambdaExpression expression) { - TypeName returnType = typeOf(expression.resolveTypeBinding().getFunctionalInterfaceMethod().getReturnType()); + Type returnType = typeOf(expression.resolveTypeBinding().getFunctionalInterfaceMethod().getReturnType()); if (expression.getBody() instanceof Block) { @SuppressWarnings("unchecked") List statements = ((Block) expression.getBody()).statements(); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java index 73cf749..950e678 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -8,6 +8,8 @@ import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.sugar.AnonymousClass; import org.zwobble.couscous.ast.sugar.Lambda; import org.zwobble.couscous.ast.sugar.TypeDeclarationBody; +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.util.ExtraLists; @@ -30,6 +32,7 @@ import static org.zwobble.couscous.ast.InstanceReceiver.instanceReceiver; import static org.zwobble.couscous.ast.StaticReceiver.staticReceiver; import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference; import static org.zwobble.couscous.ast.VariableReferenceNode.reference; +import static org.zwobble.couscous.ast.types.Types.erasure; import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables; import static org.zwobble.couscous.frontends.java.JavaTypes.*; import static org.zwobble.couscous.util.Casts.tryCast; @@ -62,7 +65,7 @@ public class JavaReader { private final ImmutableList.Builder classes; private int anonymousClassCount = 0; private final Scope topScope = Scope.create(); - private final Map> nestedClasses = new HashMap<>(); + private final Map> nestedClasses = new HashMap<>(); private JavaReader() { classes = ImmutableList.builder(); @@ -74,18 +77,18 @@ public class JavaReader { NodeTransformer transformer = NodeTransformer.builder() .transformExpression(expression -> tryCast(ConstructorCallNode.class, expression) - .flatMap(call -> lookup(nestedClasses, call.getType()).map(captures -> + .flatMap(call -> lookup(nestedClasses, erasure(call.getType())).map(captures -> new ConstructorCallNode(call.getType(), ExtraLists.concat(captures, call.getArguments()))))) .build(); return eagerMap(classes, classNode -> classNode.transform(transformer)); } private TypeNode readTypeDeclaration(TypeDeclaration type) { - TypeName name = typeOf(type.resolveBinding()); + ScalarType name = erasure(typeOf(type.resolveBinding())); Scope scope = topScope.enterClass(name); List typeParameters = readTypeParameters(type); TypeDeclarationBody body = readTypeDeclarationBody(scope, name, type.bodyDeclarations()); - Set superTypes = superTypes(type); + Set superTypes = superTypes(type); if (type.isInterface()) { // TODO: throw exception if other parts of body are declared return InterfaceNode.declareInterface(name, typeParameters, superTypes, body.getMethods()); @@ -122,7 +125,7 @@ public class JavaReader { } private GeneratedClosure readLambda(Scope outerScope, Expression expression, Function lambda) { - TypeName name = generateAnonymousName(expression); + ScalarType name = generateAnonymousName(expression); Scope scope = outerScope.enterClass(name); IMethodBinding functionalInterfaceMethod = expression.resolveTypeBinding().getFunctionalInterfaceMethod(); return generateClosure(scope, name, toAnonymousClass(functionalInterfaceMethod, lambda.apply(scope))); @@ -144,7 +147,7 @@ public class JavaReader { } private List replaceCaptureReferences( - TypeName className, + ScalarType className, List body, List freeVariables) { @@ -157,7 +160,7 @@ public class JavaReader { private ConstructorNode buildConstructor( Scope outerScope, - TypeName type, + ScalarType type, List freeVariables, ConstructorNode existing) { @@ -178,13 +181,13 @@ public class JavaReader { return new ConstructorNode(arguments.build(), body.build()); } - private FieldAccessNode captureAccess(TypeName type, CapturedVariable freeVariable) { + private FieldAccessNode captureAccess(ScalarType type, CapturedVariable freeVariable) { FieldDeclarationNode field = freeVariable.field; return fieldAccess(thisReference(type), field.getName(), field.getType()); } GeneratedClosure readAnonymousClass(Scope outerScope, AnonymousClassDeclaration declaration) { - TypeName className = generateAnonymousName(declaration); + ScalarType className = generateAnonymousName(declaration); Scope scope = outerScope.enterClass(className); TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(scope, className, declaration.bodyDeclarations()); AnonymousClass anonymousClass = new AnonymousClass( @@ -194,7 +197,7 @@ public class JavaReader { return generateClosure(scope, className, anonymousClass); } - GeneratedClosure generateClosure(Scope scope, TypeName className, AnonymousClass anonymousClass) { + GeneratedClosure generateClosure(Scope scope, ScalarType className, AnonymousClass anonymousClass) { GeneratedClosure closure = classWithCapture(scope, className, anonymousClass); classes.add(closure.getClassNode()); return closure; @@ -245,7 +248,7 @@ public class JavaReader { return new GeneratedClosure(generatedClass, freeVariables); } - private boolean isThisReference(TypeName name, ReferenceNode reference) { + private boolean isThisReference(ScalarType name, ReferenceNode reference) { return tryCast(ThisReferenceNode.class, reference) .map(node -> node.getType().equals(name)) .orElse(false); @@ -253,7 +256,7 @@ public class JavaReader { private GeneratedClosure classWithCapture( Scope scope, - TypeName className, + ScalarType className, AnonymousClass anonymousClass ) { return classWithCapture(scope, ClassNode.declareClass( @@ -275,19 +278,19 @@ public class JavaReader { @Override public FieldDeclarationNode visit(ThisReferenceNode thisReference) { - TypeName type = thisReference.getType(); - String name = "this_" + type.getQualifiedName().replace(".", "__"); + Type type = thisReference.getType(); + String name = "this_" + erasure(type).getQualifiedName().replace(".", "__"); return field(name, type); } }); } - private TypeName generateAnonymousName(ASTNode node) { + private ScalarType generateAnonymousName(ASTNode node) { ITypeBinding type = findDeclaringClass(node); while (type.isAnonymous()) { type = type.getDeclaringClass(); } - return TypeName.of(type.getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); + return ScalarType.of(type.getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); } private ITypeBinding findDeclaringClass(ASTNode node) { @@ -297,7 +300,7 @@ public class JavaReader { return ((AbstractTypeDeclaration)node).resolveBinding(); } - private TypeDeclarationBody readTypeDeclarationBody(Scope scope, TypeName type, List bodyDeclarations) { + private TypeDeclarationBody readTypeDeclarationBody(Scope scope, ScalarType type, List bodyDeclarations) { TypeDeclarationBody.Builder body = TypeDeclarationBody.builder(); for (Object declaration : bodyDeclarations) { @@ -318,7 +321,7 @@ public class JavaReader { return body.build(); } - private TypeNode readNestedTypeDeclaration(TypeName outerType, TypeDeclaration typeDeclaration) { + private TypeNode readNestedTypeDeclaration(ScalarType outerType, TypeDeclaration typeDeclaration) { TypeNode typeNode = readTypeDeclaration(typeDeclaration); // TODO: can we remove duplication of scope creation with readTypeDeclaration()? Scope scope = topScope.enterClass(typeNode.getName()); @@ -332,10 +335,10 @@ public class JavaReader { .orElse(typeNode); } - private void readField(TypeDeclarationBody.Builder builder, Scope scope, TypeName declaringType, FieldDeclaration field) { + private void readField(TypeDeclarationBody.Builder builder, Scope scope, ScalarType declaringType, FieldDeclaration field) { @SuppressWarnings("unchecked") List fragments = field.fragments(); - TypeName type = typeOf(field.getType()); + Type type = typeOf(field.getType()); fragments.forEach(fragment -> { builder.field(field(Modifier.isStatic(field.getModifiers()), fragment.getName().getIdentifier(), type)); if (fragment.getInitializer() != null) { @@ -358,7 +361,7 @@ public class JavaReader { List formalArguments = readFormalArguments(scope, method); List annotations = readAnnotations(method); - Optional returnType = Optional.ofNullable(method.getReturnType2()) + Optional returnType = Optional.ofNullable(method.getReturnType2()) .map(JavaTypes::typeOf); Optional> body = readBody(scope, method, returnType); @@ -391,7 +394,7 @@ public class JavaReader { return eagerMap(parameters, parameter -> JavaVariableDeclarationReader.read(scope, parameter)); } - private Optional> readBody(Scope scope, MethodDeclaration method, Optional returnType) { + private Optional> readBody(Scope scope, MethodDeclaration method, Optional returnType) { if (method.getBody() == null) { return Optional.empty(); } else { @@ -406,12 +409,12 @@ public class JavaReader { return statementReader.readStatement(statement); } - List readStatements(Scope scope, List body, Optional returnType) { + List readStatements(Scope scope, List body, Optional returnType) { JavaStatementReader statementReader = new JavaStatementReader(scope, expressionReader(scope), returnType); return eagerFlatMap(body, statementReader::readStatement); } - ExpressionNode readExpression(Scope scope, TypeName targetType, Expression body) { + ExpressionNode readExpression(Scope scope, org.zwobble.couscous.ast.types.Type targetType, Expression body) { return expressionReader(scope).readExpression(targetType, body); } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java index fb40ed3..ac039fb 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java @@ -3,6 +3,7 @@ package org.zwobble.couscous.frontends.java; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.sugar.SwitchCaseNode; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.values.BooleanValue; import org.zwobble.couscous.values.ObjectValues; @@ -29,12 +30,12 @@ import static org.zwobble.couscous.util.UpToAndIncludingIterable.upToAndIncludin class JavaStatementReader { private final Scope scope; private final JavaExpressionReader expressionReader; - private final Optional returnType; + private final Optional returnType; JavaStatementReader( Scope scope, JavaExpressionReader expressionReader, - Optional returnType + Optional returnType ) { this.scope = scope; this.expressionReader = expressionReader; @@ -183,13 +184,13 @@ class JavaStatementReader { private List readVariableDeclarationStatement(VariableDeclarationStatement statement) { @SuppressWarnings("unchecked") List fragments = statement.fragments(); - TypeName type = typeOf(statement.getType()); + Type type = typeOf(statement.getType()); return readDeclarationFragments(fragments, type); } private List readDeclarationFragments( List fragments, - TypeName type + Type type ) { return eagerMap(fragments, fragment -> scope.localVariable( fragment.resolveBinding().getKey(), @@ -202,7 +203,7 @@ class JavaStatementReader { return expressionReader.readExpressionWithoutBoxing(expression); } - private ExpressionNode readExpression(TypeName targetType, Expression expression) { + private ExpressionNode readExpression(Type targetType, Expression expression) { return expressionReader.readExpression(targetType, expression); } } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java index 117f3ca..fd59e8c 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java @@ -2,7 +2,9 @@ package org.zwobble.couscous.frontends.java; import com.google.common.collect.ImmutableSet; import org.eclipse.jdt.core.dom.*; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; +import org.zwobble.couscous.ast.types.Types; import org.zwobble.couscous.values.ObjectValues; import java.util.Optional; @@ -12,44 +14,44 @@ import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; class JavaTypes { - static TypeName typeOf(Expression expression) { + static Type typeOf(Expression expression) { return typeOf(expression.resolveTypeBinding()); } - static TypeName typeOf(IVariableBinding variableBinding) { + static Type typeOf(IVariableBinding variableBinding) { return typeOf(variableBinding.getType()); } - static TypeName typeOf(Type type) { + static Type typeOf(org.eclipse.jdt.core.dom.Type type) { return typeOf(type.resolveBinding()); } - static TypeName typeOf(ITypeBinding typeBinding) { + static Type typeOf(ITypeBinding typeBinding) { ITypeBinding outerClass = typeBinding.getDeclaringClass(); if (outerClass == null) { - return TypeName.of(typeBinding.getErasure().getQualifiedName()); + return ScalarType.of(typeBinding.getErasure().getQualifiedName()); } else { - return TypeName.of(typeOf(outerClass).getQualifiedName() + "__" + typeBinding.getName()); + return ScalarType.of(Types.erasure(typeOf(outerClass)).getQualifiedName() + "__" + typeBinding.getName()); } } - static Set superTypes(TypeDeclaration declaration) { + static Set superTypes(TypeDeclaration declaration) { return superTypes(declaration.resolveBinding()); } - static Set superTypes(AnonymousClassDeclaration declaration) { + static Set superTypes(AnonymousClassDeclaration declaration) { return superTypes(declaration.resolveBinding()); } - static Set superTypesAndSelf(ITypeBinding typeBinding) { - ImmutableSet.Builder superTypes = ImmutableSet.builder(); + static Set superTypesAndSelf(ITypeBinding typeBinding) { + ImmutableSet.Builder superTypes = ImmutableSet.builder(); superTypes.addAll(superTypes(typeBinding)); superTypes.add(typeOf(typeBinding)); return superTypes.build(); } - private static Set superTypes(ITypeBinding typeBinding) { - ImmutableSet.Builder superTypes = ImmutableSet.builder(); + private static Set superTypes(ITypeBinding typeBinding) { + ImmutableSet.Builder superTypes = ImmutableSet.builder(); superClass(typeBinding).ifPresent(superTypes::add); superTypes.addAll(transform( asList(typeBinding.getInterfaces()), @@ -57,7 +59,7 @@ class JavaTypes { return superTypes.build(); } - private static Optional superClass(ITypeBinding typeBinding) { + private static Optional superClass(ITypeBinding typeBinding) { return Optional.ofNullable(typeBinding.getSuperclass()) .map(JavaTypes::typeOf) .filter(type -> !type.equals(ObjectValues.OBJECT)); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java index 7ddc8ca..b02601a 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java @@ -2,6 +2,8 @@ package org.zwobble.couscous.frontends.java; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.util.NaturalNumbers; import java.util.*; @@ -28,7 +30,7 @@ public class Scope { this.temporaryCounter = temporaryCounter; } - public Scope enterClass(TypeName className) { + public Scope enterClass(ScalarType className) { return enter("class#" + className.getQualifiedName()); } @@ -45,12 +47,12 @@ public class Scope { return new Scope(variablesByKey, identifiers, identifier.extend(name), temporaryCounter); } - public FormalArgumentNode formalArgument(String name, TypeName type) { + public FormalArgumentNode formalArgument(String name, Type type) { VariableDeclaration declaration = generateVariable(name, type); return FormalArgumentNode.formalArg(declaration); } - public FormalArgumentNode formalArgument(String key, String name, TypeName type) { + public FormalArgumentNode formalArgument(String key, String name, Type type) { VariableDeclaration declaration = generateVariable(key, name, type); return FormalArgumentNode.formalArg(declaration); } @@ -58,7 +60,7 @@ public class Scope { public LocalVariableDeclarationNode localVariable( String key, String name, - TypeName type, + Type type, ExpressionNode initialValue ) { VariableDeclaration declaration = generateVariable(key, name, type); @@ -67,7 +69,7 @@ public class Scope { public LocalVariableDeclarationNode localVariable( String name, - TypeName type, + Type type, ExpressionNode initialValue ) { VariableDeclaration declaration = generateVariable(name, type); @@ -78,7 +80,7 @@ public class Scope { return temporaryVariable(initialValue.getType(), initialValue); } - public LocalVariableDeclarationNode temporaryVariable(TypeName type, ExpressionNode initialValue) { + public LocalVariableDeclarationNode temporaryVariable(Type type, ExpressionNode initialValue) { return localVariable("_couscous_tmp_" + temporaryCounter.next(), type, initialValue); } @@ -90,7 +92,7 @@ public class Scope { return VariableReferenceNode.reference(variable); } - private VariableDeclaration generateVariable(String key, String name, TypeName type) { + private VariableDeclaration generateVariable(String key, String name, Type type) { if (variablesByKey.containsKey(key)) { throw new IllegalArgumentException(key + " is already mapped"); } @@ -99,7 +101,7 @@ public class Scope { return variable; } - public VariableDeclaration generateVariable(String name, TypeName type) { + public VariableDeclaration generateVariable(String name, Type type) { Identifier identifier = generateIdentifier(name); return VariableDeclaration.var(identifier, name, type); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/Environment.java b/src/main/java/org/zwobble/couscous/interpreter/Environment.java index daee3ed..fa5958b 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Environment.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Environment.java @@ -1,9 +1,10 @@ package org.zwobble.couscous.interpreter; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.VariableDeclaration; import org.zwobble.couscous.ast.VariableNode; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.errors.UnboundVariable; import org.zwobble.couscous.interpreter.errors.VariableNotInScope; import org.zwobble.couscous.interpreter.values.InterpreterValue; @@ -16,7 +17,7 @@ import java.util.Optional; import static java.util.stream.Collectors.toMap; public class Environment { - private final Map types; + private final Map types; private final Optional thisValue; private final Map stackFrame; private final Project project; @@ -51,7 +52,7 @@ public class Environment { put(variable.getDeclaration().getId(), value); } - public StaticReceiverValue findClass(TypeName className) { + public StaticReceiverValue findClass(ScalarType className) { if (!types.containsKey(className)) { StaticReceiverValue value = new StaticReceiverValue(project.findClass(className)); types.put(className, value); @@ -71,24 +72,24 @@ public class Environment { } private void checkVariableType(Identifier variableId, InterpreterValue value) { - TypeName variableType = stackFrame.get(variableId).getType(); + Type variableType = stackFrame.get(variableId).getType(); InterpreterTypes.checkIsInstance(variableType, value); } private static class VariableEntry { - private final TypeName type; + private final Type type; private Optional value; - private VariableEntry(final TypeName type, final Optional value) { + private VariableEntry(final Type type, final Optional value) { this.type = type; this.value = value; } - public static VariableEntry of(final TypeName type, final Optional value) { + public static VariableEntry of(final Type type, final Optional value) { return new VariableEntry(type, value); } - public TypeName getType() { + public Type getType() { return this.type; } diff --git a/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java b/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java index 1d88c6b..37038ab 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Evaluator.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.interpreter; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.visitors.AssignableExpressionNodeVisitor; import org.zwobble.couscous.ast.visitors.ExpressionNodeMapper; import org.zwobble.couscous.interpreter.errors.ConditionMustBeBoolean; @@ -12,6 +13,8 @@ import org.zwobble.couscous.values.IntegerValue; import java.util.List; import java.util.stream.Collectors; +import static org.zwobble.couscous.ast.types.Types.erasure; + public class Evaluator implements ExpressionNodeMapper { public static InterpreterValue eval(Environment environment, ExpressionNode expression) { return new Evaluator(environment).eval(expression); @@ -93,7 +96,7 @@ public class Evaluator implements ExpressionNodeMapper { @Override public InterpreterValue visit(ConstructorCallNode call) { - StaticReceiverValue clazz = environment.findClass(call.getType()); + StaticReceiverValue clazz = environment.findClass(erasure(call.getType())); List arguments = evalArguments(call.getArguments()); return clazz.callConstructor(environment, arguments); } @@ -127,7 +130,7 @@ public class Evaluator implements ExpressionNodeMapper { public InterpreterValue visit(CastNode cast) { InterpreterValue value = eval(cast.getExpression()); if (!InterpreterTypes.isSubType(cast.getType(), value.getType())) { - throw new InvalidCast(cast.getType(), value.getType().getName()); + throw new InvalidCast(cast.getType(), value.getType().getType()); } return value; } @@ -166,7 +169,7 @@ public class Evaluator implements ExpressionNodeMapper { } @Override - public ReceiverValue visit(TypeName receiver) { + public ReceiverValue visit(ScalarType receiver) { return environment.findClass(receiver); } }); diff --git a/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java b/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java index 8ec461a..fcf20d9 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Interpreter.java @@ -2,7 +2,8 @@ package org.zwobble.couscous.interpreter; import com.google.common.collect.ImmutableMap; import org.zwobble.couscous.ast.MethodSignature; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.values.InterpreterValue; import org.zwobble.couscous.interpreter.values.StaticReceiverValue; @@ -18,7 +19,7 @@ public class Interpreter { this.project = project; } - public InterpreterValue run(TypeName className, String methodName, List arguments, TypeName returnType) { + public InterpreterValue run(ScalarType className, String methodName, List arguments, Type returnType) { final Environment environment = new Environment( project, Optional.empty(), @@ -27,7 +28,7 @@ public class Interpreter { StaticReceiverValue clazz = environment.findClass(className); MethodSignature signature = new MethodSignature( methodName, - eagerMap(arguments, argument -> argument.getType().getName()), + eagerMap(arguments, argument -> argument.getType().getType()), returnType); return clazz.callMethod(environment, signature, arguments); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java b/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java index b9221de..0ec63b0 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java +++ b/src/main/java/org/zwobble/couscous/interpreter/InterpreterTypes.java @@ -1,26 +1,26 @@ package org.zwobble.couscous.interpreter; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.errors.UnexpectedValueType; import org.zwobble.couscous.interpreter.types.InterpreterType; import org.zwobble.couscous.interpreter.values.InterpreterValue; import org.zwobble.couscous.values.ObjectValues; public class InterpreterTypes { - public static void checkIsInstance(TypeName type, InterpreterValue value) { + public static void checkIsInstance(Type type, InterpreterValue value) { checkIsSubType(type, value.getType()); } - private static void checkIsSubType(TypeName superTypeName, InterpreterType subType) { - if (!isSubType(superTypeName, subType)) { - throw new UnexpectedValueType(superTypeName, subType.getName()); + private static void checkIsSubType(Type superType, InterpreterType subType) { + if (!isSubType(superType, subType)) { + throw new UnexpectedValueType(superType, subType.getType()); } } - public static boolean isSubType(TypeName superTypeName, InterpreterType subType) { + public static boolean isSubType(Type superType, InterpreterType subType) { return - superTypeName.equals(subType.getName()) || - superTypeName.equals(ObjectValues.OBJECT) || - subType.getSuperTypes().contains(superTypeName); + superType.equals(subType.getType()) || + superType.equals(ObjectValues.OBJECT) || + subType.getSuperTypes().contains(superType); } } diff --git a/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java b/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java index eca6ba9..ebc31ae 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java +++ b/src/main/java/org/zwobble/couscous/interpreter/MapBackedProject.java @@ -1,25 +1,27 @@ package org.zwobble.couscous.interpreter; import com.google.common.collect.ImmutableMap; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.interpreter.types.InterpreterType; import java.util.Map; +import static org.zwobble.couscous.ast.types.Types.erasure; + public class MapBackedProject implements Project { public static Builder builder() { return new Builder(); } public static class Builder { - private final ImmutableMap.Builder classes; + private final ImmutableMap.Builder classes; private Builder() { classes = ImmutableMap.builder(); } public Builder addClass(InterpreterType clazz) { - classes.put(clazz.getName(), clazz); + classes.put(erasure(clazz.getType()), clazz); return this; } @@ -35,14 +37,14 @@ public class MapBackedProject implements Project { } } - private Map classes; + private Map classes; - public MapBackedProject(Map classes) { + public MapBackedProject(Map classes) { this.classes = classes; } @Override - public InterpreterType findClass(TypeName name) { + public InterpreterType findClass(ScalarType name) { if (classes.containsKey(name)) { return classes.get(name); } else { diff --git a/src/main/java/org/zwobble/couscous/interpreter/Project.java b/src/main/java/org/zwobble/couscous/interpreter/Project.java index 7e749b4..edfbaba 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/Project.java +++ b/src/main/java/org/zwobble/couscous/interpreter/Project.java @@ -1,8 +1,8 @@ package org.zwobble.couscous.interpreter; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.interpreter.types.InterpreterType; public interface Project { - InterpreterType findClass(TypeName name); + InterpreterType findClass(ScalarType name); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java b/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java index 2aef47b..a13612b 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java +++ b/src/main/java/org/zwobble/couscous/interpreter/errors/InvalidCast.java @@ -1,22 +1,22 @@ package org.zwobble.couscous.interpreter.errors; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; public class InvalidCast extends InterpreterException { - private final TypeName expected; - private final TypeName actual; + private final Type expected; + private final Type actual; - public InvalidCast(TypeName expected, TypeName actual) { + public InvalidCast(Type expected, Type actual) { this.expected = expected; this.actual = actual; } - public TypeName getExpected() { + public Type getExpected() { return expected; } - public TypeName getActual() { + public Type getActual() { return actual; } } diff --git a/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java b/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java index ca40c7c..459d9d7 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/errors/UnexpectedValueType.java @@ -1,22 +1,22 @@ package org.zwobble.couscous.interpreter.errors; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; public final class UnexpectedValueType extends InterpreterException { private static final long serialVersionUID = 1L; - private final TypeName expected; - private final TypeName actual; + private final Type expected; + private final Type actual; - public UnexpectedValueType(final TypeName expected, final TypeName actual) { + public UnexpectedValueType(final Type expected, final Type actual) { this.expected = expected; this.actual = actual; } - public TypeName getExpected() { + public Type getExpected() { return this.expected; } - public TypeName getActual() { + public Type getActual() { return this.actual; } diff --git a/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java b/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java index f5316ae..5ee1ae0 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/InterpreterType.java @@ -3,7 +3,7 @@ package org.zwobble.couscous.interpreter.types; import org.zwobble.couscous.ast.FieldDeclarationNode; import org.zwobble.couscous.ast.MethodSignature; import org.zwobble.couscous.ast.StatementNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.values.InterpreterValue; @@ -12,8 +12,8 @@ import java.util.Optional; import java.util.Set; public interface InterpreterType { - TypeName getName(); - Set getSuperTypes(); + Type getType(); + Set getSuperTypes(); Optional getField(String fieldName); List getStaticConstructor(); InterpreterValue callConstructor(Environment environment, List arguments); diff --git a/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java b/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java index 5bfc221..96973cb 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java @@ -3,8 +3,9 @@ package org.zwobble.couscous.interpreter.types; import com.google.common.collect.ImmutableMap; import org.zwobble.couscous.ast.FieldDeclarationNode; import org.zwobble.couscous.ast.MethodSignature; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.ast.StatementNode; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.PositionalArguments; import org.zwobble.couscous.interpreter.errors.NoSuchMethod; @@ -21,12 +22,12 @@ import static org.zwobble.couscous.util.ExtraLists.list; import static org.zwobble.couscous.util.ExtraSets.set; public class IntrinsicInterpreterType implements InterpreterType { - public static Builder builder(Class interpreterValueType, TypeName reference) { + public static Builder builder(Class interpreterValueType, ScalarType reference) { return new Builder<>(interpreterValueType, reference); } public static Builder builder(Class interpreterValueType, String name) { - return builder(interpreterValueType, TypeName.of(name)); + return builder(interpreterValueType, ScalarType.of(name)); } public static Builder classBuilder(String name) { @@ -39,34 +40,34 @@ public class IntrinsicInterpreterType implements InterpreterType { private final ImmutableMap.Builder methods = ImmutableMap.builder(); private final ImmutableMap.Builder staticMethods = ImmutableMap.builder(); private final Class interpreterValueType; - private final TypeName name; + private final ScalarType name; - public Builder(Class interpreterValueType, TypeName name) { + public Builder(Class interpreterValueType, ScalarType name) { this.interpreterValueType = interpreterValueType; this.name = name; } - public Builder field(String name, TypeName type) { + public Builder field(String name, ScalarType type) { fields.put(name, FieldDeclarationNode.field(name, type)); return this; } - public Builder constructor(List argumentsTypes, BiFunction, InterpreterValue> method) { + public Builder constructor(List argumentsTypes, BiFunction, InterpreterValue> method) { constructor = toMethodValue(argumentsTypes, method); return this; } public Builder method( String name, - List argumentsTypes, - TypeName returnType, + List argumentsTypes, + Type returnType, BiFunction, InterpreterValue> method) { methods.put(new MethodSignature(name, argumentsTypes, returnType), toMethodValue(argumentsTypes, method)); return this; } - private MethodValue toMethodValue(List argumentsTypes, BiFunction, InterpreterValue> method) { + private MethodValue toMethodValue(List argumentsTypes, BiFunction, InterpreterValue> method) { return new MethodValue( argumentsTypes, (environment, arguments) -> @@ -77,8 +78,8 @@ public class IntrinsicInterpreterType implements InterpreterType { public Builder staticMethod( String name, - List argumentsTypes, - TypeName returnType, + List argumentsTypes, + Type returnType, BiFunction method) { staticMethods.put(new MethodSignature(name, argumentsTypes, returnType), new StaticMethodValue(argumentsTypes, method)); @@ -95,14 +96,14 @@ public class IntrinsicInterpreterType implements InterpreterType { } } - private final TypeName name; + private final ScalarType name; private final Map fields; private final MethodValue constructor; private final Map methods; private final Map staticMethods; private IntrinsicInterpreterType( - TypeName name, + ScalarType name, Map fields, MethodValue constructor, Map methods, @@ -116,12 +117,12 @@ public class IntrinsicInterpreterType implements InterpreterType { } @Override - public TypeName getName() { + public ScalarType getType() { return name; } @Override - public Set getSuperTypes() { + public Set getSuperTypes() { return set(); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java b/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java index a253180..6d596ee 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java @@ -1,6 +1,8 @@ package org.zwobble.couscous.interpreter.types; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.Executor; import org.zwobble.couscous.interpreter.InterpreterTypes; @@ -38,12 +40,12 @@ public class UserDefinedInterpreterType implements InterpreterType { } @Override - public TypeName getName() { + public ScalarType getType() { return type.getName(); } @Override - public Set getSuperTypes() { + public Set getSuperTypes() { return type.getSuperTypes(); } @@ -65,7 +67,7 @@ public class UserDefinedInterpreterType implements InterpreterType { // TODO: add test for this case .orElseThrow(() -> new RuntimeException("Cannot instantiate non-class types")) .getConstructor(); - List formalArgumentTypes = eagerMap( + List formalArgumentTypes = eagerMap( constructor.getArguments(), FormalArgumentNode::getType); checkMethodArguments(formalArgumentTypes, arguments); @@ -78,12 +80,12 @@ public class UserDefinedInterpreterType implements InterpreterType { return object; } - private static void checkMethodArguments(final List argumentTypes, List arguments) { + private static void checkMethodArguments(final List argumentTypes, List arguments) { if (argumentTypes.size() != arguments.size()) { throw new WrongNumberOfArguments(argumentTypes.size(), arguments.size()); } for (int index = 0; index < arguments.size(); index++) { - TypeName formalArgumentType = argumentTypes.get(index); + Type formalArgumentType = argumentTypes.get(index); InterpreterTypes.checkIsInstance(formalArgumentType, arguments.get(index)); } } diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java index 5c477af..18d0c34 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java @@ -38,7 +38,7 @@ public class BoxedIntegerInterpreterValue { .method("equals", list(ObjectValues.OBJECT), BooleanValue.REF, (environment, arguments) -> { InterpreterValue right = arguments.getPositionalArguments().get(0); - if (right.getType().getName().equals(ObjectValues.BOXED_INT)) { + if (right.getType().getType().equals(ObjectValues.BOXED_INT)) { IntegerInterpreterValue leftValue = (IntegerInterpreterValue)arguments.getReceiver().getField("value"); IntegerInterpreterValue rightValue = (IntegerInterpreterValue)right.getField("value"); return BooleanInterpreterValue.of(leftValue.getValue() == rightValue.getValue()); diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java b/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java index f96ba9a..c296797 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/Callable.java @@ -2,8 +2,9 @@ package org.zwobble.couscous.interpreter.values; import java.util.List; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; +import org.zwobble.couscous.ast.types.Type; public interface Callable { - List getArgumentTypes(); + List getArgumentTypes(); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java b/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java index 4e8f1e6..3841d7c 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/InterpreterValues.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.values.PrimitiveValue; import org.zwobble.couscous.values.PrimitiveValueVisitor; @@ -40,7 +40,7 @@ public class InterpreterValues { } @Override - public InterpreterValue visitType(TypeName value) { + public InterpreterValue visitType(ScalarType value) { return TypeInterpreterValue.of(value); } }); diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java index 01b2f35..eeda6f4 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/MethodValue.java @@ -1,25 +1,25 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import java.util.List; import java.util.function.BiFunction; public final class MethodValue implements Callable { - private final List argumentTypes; + private final List argumentTypes; private final BiFunction, InterpreterValue> apply; public InterpreterValue apply(Environment environment, MethodCallArguments arguments) { return apply.apply(environment, arguments); } - public MethodValue(final List argumentTypes, final BiFunction, InterpreterValue> apply) { + public MethodValue(final List argumentTypes, final BiFunction, InterpreterValue> apply) { this.argumentTypes = argumentTypes; this.apply = apply; } - public List getArgumentTypes() { + public List getArgumentTypes() { return this.argumentTypes; } } \ No newline at end of file diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java index fabf1ad..e8223ac 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/StaticMethodValue.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.Type; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.PositionalArguments; @@ -8,19 +8,19 @@ import java.util.List; import java.util.function.BiFunction; public final class StaticMethodValue implements Callable { - private final List argumentTypes; + private final List argumentTypes; private final BiFunction apply; public InterpreterValue apply(Environment environment, PositionalArguments arguments) { return apply.apply(environment, arguments); } - public StaticMethodValue(final List argumentTypes, final BiFunction apply) { + public StaticMethodValue(final List argumentTypes, final BiFunction apply) { this.argumentTypes = argumentTypes; this.apply = apply; } - public List getArgumentTypes() { + public List getArgumentTypes() { return this.argumentTypes; } } \ No newline at end of file diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java index f221ea0..a4efddb 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java +++ b/src/main/java/org/zwobble/couscous/interpreter/values/TypeInterpreterValue.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.interpreter.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; import org.zwobble.couscous.interpreter.errors.NoSuchField; import org.zwobble.couscous.interpreter.types.InterpreterType; import org.zwobble.couscous.interpreter.types.IntrinsicInterpreterType; @@ -15,13 +15,13 @@ public class TypeInterpreterValue implements InterpreterValue { .builder(TypeInterpreterValue.class, ObjectValues.CLASS) .build(); - public static InterpreterValue of(TypeName type) { + public static InterpreterValue of(ScalarType type) { return new TypeInterpreterValue(type); } - private final TypeName type; + private final ScalarType type; - public TypeInterpreterValue(TypeName type) { + public TypeInterpreterValue(ScalarType type) { this.type = type; } diff --git a/src/main/java/org/zwobble/couscous/values/BooleanValue.java b/src/main/java/org/zwobble/couscous/values/BooleanValue.java index 887cddf..fd387e0 100644 --- a/src/main/java/org/zwobble/couscous/values/BooleanValue.java +++ b/src/main/java/org/zwobble/couscous/values/BooleanValue.java @@ -1,9 +1,9 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public final class BooleanValue implements PrimitiveValue { - public static final TypeName REF = TypeName.of("boolean"); + public static final ScalarType REF = ScalarType.of("boolean"); private final boolean value; @@ -21,7 +21,7 @@ public final class BooleanValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } diff --git a/src/main/java/org/zwobble/couscous/values/IntegerValue.java b/src/main/java/org/zwobble/couscous/values/IntegerValue.java index ed0040a..22a233b 100644 --- a/src/main/java/org/zwobble/couscous/values/IntegerValue.java +++ b/src/main/java/org/zwobble/couscous/values/IntegerValue.java @@ -1,9 +1,9 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public final class IntegerValue implements PrimitiveValue { - public static final TypeName REF = TypeName.of("int"); + public static final ScalarType REF = ScalarType.of("int"); private final int value; @Override @@ -12,7 +12,7 @@ public final class IntegerValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } diff --git a/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java b/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java index c08104f..b56abec 100644 --- a/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java +++ b/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java @@ -1,7 +1,7 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class InternalCouscousValue { - public static final TypeName REF = TypeName.of("_couscous"); + public static final ScalarType REF = ScalarType.of("_couscous"); } diff --git a/src/main/java/org/zwobble/couscous/values/ObjectValues.java b/src/main/java/org/zwobble/couscous/values/ObjectValues.java index 2681259..5dd94a7 100644 --- a/src/main/java/org/zwobble/couscous/values/ObjectValues.java +++ b/src/main/java/org/zwobble/couscous/values/ObjectValues.java @@ -1,10 +1,10 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class ObjectValues { - public static final TypeName CLASS = TypeName.of("java.lang.Class"); - public static final TypeName OBJECT = TypeName.of("java.lang.Object"); - public static final TypeName BOXED_INT = TypeName.of("java.lang.Integer"); - public static final TypeName BOXED_BOOLEAN = TypeName.of("java.lang.Boolean"); + public static final ScalarType CLASS = ScalarType.of("java.lang.Class"); + public static final ScalarType OBJECT = ScalarType.of("java.lang.Object"); + public static final ScalarType BOXED_INT = ScalarType.of("java.lang.Integer"); + public static final ScalarType BOXED_BOOLEAN = ScalarType.of("java.lang.Boolean"); } diff --git a/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java b/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java index aefff21..cd6a078 100644 --- a/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java +++ b/src/main/java/org/zwobble/couscous/values/PrimitiveValue.java @@ -1,8 +1,8 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public interface PrimitiveValue { T accept(PrimitiveValueVisitor visitor); - TypeName getType(); + ScalarType getType(); } diff --git a/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java b/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java index 18b6916..302118d 100644 --- a/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java +++ b/src/main/java/org/zwobble/couscous/values/PrimitiveValueVisitor.java @@ -1,11 +1,11 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public interface PrimitiveValueVisitor { T visitInteger(int value); T visitString(String value); T visitBoolean(boolean value); T visitUnit(); - T visitType(TypeName value); + T visitType(ScalarType value); } diff --git a/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java b/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java index 8022a9d..9aac702 100644 --- a/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java +++ b/src/main/java/org/zwobble/couscous/values/PrimitiveValues.java @@ -1,6 +1,6 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class PrimitiveValues { public static final PrimitiveValue UNIT = UnitValue.UNIT; @@ -19,7 +19,7 @@ public class PrimitiveValues { return value ? TRUE : FALSE; } - public static PrimitiveValue value(TypeName type) { + public static PrimitiveValue value(ScalarType type) { return new TypeValue(type); } } diff --git a/src/main/java/org/zwobble/couscous/values/StringValue.java b/src/main/java/org/zwobble/couscous/values/StringValue.java index d2a0a6a..4d45ae8 100644 --- a/src/main/java/org/zwobble/couscous/values/StringValue.java +++ b/src/main/java/org/zwobble/couscous/values/StringValue.java @@ -1,13 +1,13 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public final class StringValue implements PrimitiveValue { public static StringValue of(String value) { return new StringValue(value); } - public static final TypeName REF = TypeName.of("java.lang.String"); + public static final ScalarType REF = ScalarType.of("java.lang.String"); private final String value; @Override @@ -16,7 +16,7 @@ public final class StringValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } diff --git a/src/main/java/org/zwobble/couscous/values/TypeValue.java b/src/main/java/org/zwobble/couscous/values/TypeValue.java index 663673e..e7f5fe4 100644 --- a/src/main/java/org/zwobble/couscous/values/TypeValue.java +++ b/src/main/java/org/zwobble/couscous/values/TypeValue.java @@ -1,15 +1,15 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class TypeValue implements PrimitiveValue { - private final TypeName value; + private final ScalarType value; - public TypeValue(TypeName value) { + public TypeValue(ScalarType value) { this.value = value; } - public TypeName getValue() { + public ScalarType getValue() { return value; } @@ -19,7 +19,7 @@ public class TypeValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return ObjectValues.CLASS; } diff --git a/src/main/java/org/zwobble/couscous/values/UnitValue.java b/src/main/java/org/zwobble/couscous/values/UnitValue.java index dfa11d4..398f04a 100644 --- a/src/main/java/org/zwobble/couscous/values/UnitValue.java +++ b/src/main/java/org/zwobble/couscous/values/UnitValue.java @@ -1,9 +1,9 @@ package org.zwobble.couscous.values; -import org.zwobble.couscous.ast.TypeName; +import org.zwobble.couscous.ast.types.ScalarType; public class UnitValue implements PrimitiveValue { - public static final TypeName REF = TypeName.of("org.zwobble.couscous.runtime.Unit"); + public static final ScalarType REF = ScalarType.of("org.zwobble.couscous.runtime.Unit"); static final UnitValue UNIT = new UnitValue(); @@ -16,7 +16,7 @@ public class UnitValue implements PrimitiveValue { } @Override - public TypeName getType() { + public ScalarType getType() { return REF; } } -- cgit v1.2.3