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 ++++-- 31 files changed, 257 insertions(+), 189 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/org/zwobble/couscous/ast') 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); } -- cgit v1.2.3