diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-09 18:11:17 +0000 |
| commit | 20dc1e124d606cf4298d5112b6d3c4f6c74e9b68 (patch) | |
| tree | 47914e8b0356b2f40e3723eae650d3bb6f100dd4 /src/main/java/org/zwobble/couscous/ast | |
| parent | 8ac6cb16b727b5e3d28ab89fee1d169a4f74d9a1 (diff) | |
Introduce Type interface
Diffstat (limited to 'src/main/java/org/zwobble/couscous/ast')
30 files changed, 196 insertions, 128 deletions
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<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<FieldDeclarationNode> fields, List<StatementNode> 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<FormalTypeParameterNode> typeParameters; - private final Set<TypeName> superTypes; + private final Set<Type> superTypes; private final List<FieldDeclarationNode> fields; private final List<StatementNode> staticConstructor; private final ConstructorNode constructor; private final List<MethodNode> methods; public ClassNode( - TypeName name, + ScalarType name, List<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<FieldDeclarationNode> fields, List<StatementNode> 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<TypeName> getSuperTypes() { + public Set<Type> 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<TypeName> superTypes; + private final ScalarType name; + private final ImmutableSet.Builder<Type> superTypes; private final ImmutableList.Builder<FormalTypeParameterNode> typeParameters; private final ImmutableList.Builder<FieldDeclarationNode> fields; private List<StatementNode> staticConstructor; private Optional<ConstructorNode> constructor; private final ImmutableList.Builder<MethodNode> 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<MethodBuilder<?>, T> build; private final ImmutableList.Builder<AnnotationNode> annotations; private final ImmutableList.Builder<FormalArgumentNode> arguments; - private TypeName returnType = UnitValue.REF; + private ScalarType returnType = UnitValue.REF; private final ImmutableList.Builder<StatementNode> statements; public MethodBuilder(Function<MethodBuilder<?>, T> build) { @@ -142,7 +144,7 @@ public class ClassNodeBuilder { this.statements = ImmutableList.builder(); } - public MethodBuilder<T> annotation(TypeName type) { + public MethodBuilder<T> annotation(ScalarType type) { annotations.add(AnnotationNode.annotation(type)); return this; } @@ -151,12 +153,12 @@ public class ClassNodeBuilder { return ThisReferenceNode.thisReference(name); } - public MethodBuilder<T> argument(Identifier id, String name, TypeName type) { + public MethodBuilder<T> argument(Identifier id, String name, ScalarType type) { arguments.add(formalArg(var(id, name, type))); return this; } - public MethodBuilder<T> returns(TypeName returnType) { + public MethodBuilder<T> 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<? extends ExpressionNode> arguments) { + Type type, + List<? extends ExpressionNode> arguments) + { return new ConstructorCallNode(type, arguments); } - private final TypeName type; + private final Type type; private final List<? extends ExpressionNode> arguments; public ConstructorCallNode( - TypeName type, - List<? extends ExpressionNode> arguments) { + Type type, + List<? extends ExpressionNode> 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> T accept(ExpressionNodeMapper<T> 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<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<MethodNode> methods) { return new InterfaceNode(name, typeParameters, superTypes, methods); } - private final TypeName name; + private final ScalarType name; private final List<FormalTypeParameterNode> typeParameters; - private final Set<TypeName> superTypes; + private final Set<Type> superTypes; private final List<MethodNode> methods; private InterfaceNode( - TypeName name, + ScalarType name, List<FormalTypeParameterNode> typeParameters, - Set<TypeName> superTypes, + Set<Type> superTypes, List<MethodNode> 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<TypeName> getSuperTypes() { + public Set<Type> 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<ExpressionNode> 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<ExpressionNode> 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<ExpressionNode> 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<ExpressionNode> 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<ExpressionNode> arguments; - private final TypeName type; + private final Type type; private MethodCallNode( Receiver receiver, String methodName, List<ExpressionNode> 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<FormalArgumentNode> arguments = ImmutableList.builder(); - private TypeName returnType = UnitValue.REF; + private Type returnType = UnitValue.REF; private final ImmutableList.Builder<StatementNode> 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<FormalArgumentNode> arguments, - TypeName returnType, + Type returnType, Optional<List<StatementNode>> 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<FormalArgumentNode> arguments; - private final TypeName returnType; + private final Type returnType; private final Optional<List<StatementNode>> body; private MethodNode( @@ -109,7 +110,7 @@ public class MethodNode implements Node { boolean isStatic, String name, List<FormalArgumentNode> arguments, - TypeName returnType, + Type returnType, Optional<List<StatementNode>> 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<TypeName> arguments; - private final TypeName returnType; + private final List<Type> arguments; + private final Type returnType; - public MethodSignature(String name, List<TypeName> arguments, TypeName returnType) { + public MethodSignature(String name, List<Type> arguments, Type returnType) { this.name = name; this.arguments = arguments; this.returnType = returnType; @@ -17,11 +19,11 @@ public class MethodSignature { return name; } - public List<TypeName> getArguments() { + public List<Type> 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<ExpressionNode> arguments, TypeName type) { + public static OperationNode operation(Operator operator, List<ExpressionNode> arguments, Type type) { return new OperationNode(operator, arguments, type); } private final Operator operator; private final List<ExpressionNode> arguments; - private TypeName type; + private Type type; - OperationNode(Operator operator, List<ExpressionNode> arguments, TypeName type) { + OperationNode(Operator operator, List<ExpressionNode> 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> { 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/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<FormalTypeParameterNode> getTypeParameters(); - Set<TypeName> getSuperTypes(); + Set<Type> getSuperTypes(); List<MethodNode> 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<TypeName> superTypes; + private final Set<Type> superTypes; private final List<FieldDeclarationNode> fields; private final List<MethodNode> methods; public AnonymousClass( - Set<TypeName> superTypes, + Set<Type> superTypes, List<FieldDeclarationNode> fields, List<MethodNode> methods) { @@ -23,7 +23,7 @@ public class AnonymousClass { this.methods = methods; } - public Set<TypeName> getSuperTypes() { + public Set<Type> getSuperTypes() { return superTypes; } diff --git a/src/main/java/org/zwobble/couscous/ast/TypeName.java b/src/main/java/org/zwobble/couscous/ast/types/ScalarType.java index e2c32d0..69b9f97 100644 --- a/src/main/java/org/zwobble/couscous/ast/TypeName.java +++ b/src/main/java/org/zwobble/couscous/ast/types/ScalarType.java @@ -1,20 +1,20 @@ -package org.zwobble.couscous.ast; +package org.zwobble.couscous.ast.types; import com.google.common.base.Strings; import java.util.Optional; -public class TypeName { - public static TypeName of(String qualifiedName) { +public class ScalarType implements Type { + public static ScalarType of(String qualifiedName) { if (Strings.isNullOrEmpty(qualifiedName)) { throw new IllegalArgumentException("qualifiedName cannot be blank"); } - return new TypeName(qualifiedName); + return new ScalarType(qualifiedName); } private final String qualifiedName; - private TypeName(String qualifiedName) { + private ScalarType(String qualifiedName) { this.qualifiedName = qualifiedName; } @@ -37,7 +37,7 @@ public class TypeName { @Override public String toString() { - return "TypeName(qualifiedName=" + qualifiedName + ")"; + return "ScalarType(qualifiedName=" + qualifiedName + ")"; } @Override @@ -57,7 +57,7 @@ public class TypeName { return false; if (getClass() != obj.getClass()) return false; - TypeName other = (TypeName) obj; + ScalarType other = (ScalarType) obj; if (qualifiedName == null) { if (other.qualifiedName != null) return false; @@ -65,4 +65,9 @@ public class TypeName { return false; return true; } + + @Override + public <T> T accept(Visitor<T> 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> T accept(Visitor<T> visitor); + + interface Visitor<T> { + 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<ScalarType>() { + @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<ExpressionNode, Optional<ExpressionNode>> transformExpression = expression -> Optional.empty(); - private Function<TypeName, TypeName> transformType = type -> type; + private Function<Type, Type> transformType = type -> type; private Function<MethodSignature, String> transformMethodName = MethodSignature::getName; public Builder transformExpression( @@ -165,7 +167,7 @@ public class NodeTransformer { } public Builder transformType( - Function<TypeName, TypeName> transformType) + Function<Type, Type> transformType) { this.transformType = transformType; return this; @@ -182,12 +184,12 @@ public class NodeTransformer { } private final Function<ExpressionNode, Optional<ExpressionNode>> transformExpression; - private final Function<TypeName, TypeName> transformType; + private final Function<Type, Type> transformType; private final Function<MethodSignature, String> transformMethodName; private NodeTransformer( Function<ExpressionNode, Optional<ExpressionNode>> transformExpression, - Function<TypeName, TypeName> transformType, + Function<Type, Type> transformType, Function<MethodSignature, String> 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<TypeName> transformTypes(Set<TypeName> superTypes) { + public Set<Type> transformTypes(Set<Type> superTypes) { return transformSet(superTypes, this::transform); } |
