diff options
Diffstat (limited to 'src')
25 files changed, 226 insertions, 207 deletions
diff --git a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java index ec17c9b..4d39b01 100644 --- a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java +++ b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java @@ -38,7 +38,7 @@ public class ClassNodeBuilder { } public ClassNodeBuilder(String name) { - this(ScalarType.of(name)); + this(ScalarType.topLevel(name)); } public ClassNodeBuilder addTypeParameter(String name) { @@ -47,7 +47,7 @@ public class ClassNodeBuilder { } public ClassNodeBuilder addSuperType(String type) { - this.superTypes.add(ScalarType.of(type)); + this.superTypes.add(ScalarType.topLevel(type)); return this; } diff --git a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java index 2a6f408..ece3175 100644 --- a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java +++ b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java @@ -19,7 +19,7 @@ public class MethodCallNode implements ExpressionNode { List<ExpressionNode> arguments, Type type) { - return staticMethodCall(ScalarType.of(className), methodName, arguments, type); + return staticMethodCall(ScalarType.topLevel(className), methodName, arguments, type); } public static ExpressionNode staticMethodCall( @@ -29,7 +29,7 @@ public class MethodCallNode implements ExpressionNode { Type returnType, MethodSignature signature) { - return staticMethodCall(ScalarType.of(className), methodName, arguments, returnType, signature); + return staticMethodCall(ScalarType.topLevel(className), methodName, arguments, returnType, signature); } public static ExpressionNode staticMethodCall( diff --git a/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java b/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java index 7ae7ec1..a61a685 100644 --- a/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java +++ b/src/main/java/org/zwobble/couscous/ast/StaticReceiver.java @@ -6,7 +6,7 @@ import org.zwobble.couscous.util.ExtraIterables; public class StaticReceiver implements Receiver { public static Receiver staticReceiver(String type) { - return new StaticReceiver(ScalarType.of(type)); + return new StaticReceiver(ScalarType.topLevel(type)); } public static Receiver staticReceiver(ScalarType type) { 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 260d7ec..68eaa73 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpCodeGenerator.java @@ -21,14 +21,14 @@ public class CsharpCodeGenerator { public static final Naming NAMING = Naming.noMangling(); private final static Map<ScalarType, ScalarType> PRIMITIVES = ImmutableMap.<ScalarType, ScalarType>builder() - .put(Types.INT, ScalarType.of("int")) - .put(Types.BOXED_INT, ScalarType.of("int")) - .put(Types.STRING, ScalarType.of("string")) - .put(Types.CHAR, ScalarType.of("char")) - .put(Types.BOOLEAN, ScalarType.of("bool")) - .put(Types.BOXED_BOOLEAN, ScalarType.of("bool")) - .put(Types.CLASS, ScalarType.of("System.Type")) - .put(Types.VOID, ScalarType.of("void")) + .put(Types.INT, ScalarType.topLevel("int")) + .put(Types.BOXED_INT, ScalarType.topLevel("int")) + .put(Types.STRING, ScalarType.topLevel("string")) + .put(Types.CHAR, ScalarType.topLevel("char")) + .put(Types.BOOLEAN, ScalarType.topLevel("bool")) + .put(Types.BOXED_BOOLEAN, ScalarType.topLevel("bool")) + .put(Types.CLASS, ScalarType.topLevel("System.Type")) + .put(Types.VOID, ScalarType.topLevel("void")) .put(Types.ARRAY, Types.ARRAY) .build(); @@ -81,7 +81,7 @@ public class CsharpCodeGenerator { @Override public Type visit(ParameterizedType type) { if (type.getRawType().equals(Types.CLASS)) { - return ScalarType.of("System.Type"); + return ScalarType.topLevel("System.Type"); } else { return new ParameterizedType( (ScalarType) transformType(type.getRawType()), @@ -102,7 +102,7 @@ public class CsharpCodeGenerator { return type.accept(new Type.Visitor<Type>() { @Override public ScalarType visit(ScalarType type) { - return ScalarType.of(namespace + "." + type.getQualifiedName()); + return ScalarType.topLevel(namespace + "." + type.getQualifiedName()); } @Override 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 c2fa029..4ddeb7c 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/CsharpSerializer.java @@ -596,7 +596,7 @@ public class CsharpSerializer { } private void writeInNamespace(TypeNode typeNode, Action writeBody) { - writeInNamespace(typeNode.getName().getQualifier().get(), writeBody); + writeInNamespace(typeNode.getName().getPackage().get(), writeBody); } private void writeInNamespace(String namespace, Action writeBody) { diff --git a/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java index 6a5c084..3208e17 100644 --- a/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java +++ b/src/main/java/org/zwobble/couscous/backends/csharp/primitives/CsharpPrimitiveMethods.java @@ -135,10 +135,10 @@ public class CsharpPrimitiveMethods { .put("parseInt", new PrimitiveStaticMethod( arguments -> staticMethodCall( - ScalarType.of("int"), + ScalarType.topLevel("int"), "Parse", list(arguments.get(0)), - ScalarType.of("int") + ScalarType.topLevel("int") ), Types.INT )) @@ -155,7 +155,7 @@ public class CsharpPrimitiveMethods { .put("join", new PrimitiveStaticMethod( arguments -> staticMethodCall( - ScalarType.of("java.lang.String"), + ScalarType.topLevel("java.lang.String"), "join", arguments, Types.STRING 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 823310e..747bcac 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java @@ -230,7 +230,7 @@ public class JavaExpressionReader { private Receiver toReceiver(IBinding binding) { if (binding instanceof ITypeBinding) { - ScalarType receiver = ScalarType.of(((ITypeBinding) binding).getQualifiedName()); + ScalarType receiver = ScalarType.topLevel(((ITypeBinding) binding).getQualifiedName()); return staticReceiver(receiver); } else if (binding instanceof IVariableBinding) { return instanceReceiver(readVariableBinding((IVariableBinding) binding)); 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 a047e9e..b0d347b 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -239,7 +239,7 @@ public class JavaReader { while (type.isAnonymous()) { type = type.getDeclaringClass(); } - return ScalarType.of(erasure(typeOf(type)).getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); + return ScalarType.topLevel(erasure(typeOf(type)).getQualifiedName() + "_Anonymous_" + (anonymousClassCount++)); } private ITypeBinding findDeclaringClass(ASTNode node) { 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 5d9eddd..fbd2a3b 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java @@ -47,12 +47,10 @@ public class JavaTypes { Identifier declaringScope = getDeclaringScope(typeBinding); return new TypeParameter(declaringScope, typeBinding.getName()); } - // TODO: tidy-up -// ScalarType rawType = outerClass == null -// ? ScalarType.of(typeBinding.getErasure().getQualifiedName()) -// // TODO: test for erasure of inner type name -// : ScalarType.of(erasure(typeOf(outerClass)).getQualifiedName() + "__" + typeBinding.getErasure().getName()); - ScalarType rawType = ScalarType.of(typeBinding.getErasure().getQualifiedName()); + ScalarType rawType = outerClass == null + ? ScalarType.topLevel(typeBinding.getErasure().getQualifiedName()) + // TODO: test for erasure of inner type name + : ScalarType.innerType(erasure(typeOf(outerClass)), typeBinding.getErasure().getName()); if (typeBinding.isParameterizedType()) { List<Type> typeParameters = eagerMap( asList(typeBinding.getTypeArguments()), @@ -141,10 +139,10 @@ public class JavaTypes { } public static Type iterable(Type elementType) { - return parameterizedType(ScalarType.of("java.lang.Iterable"), list(elementType)); + return parameterizedType(ScalarType.topLevel("java.lang.Iterable"), list(elementType)); } - private static final ScalarType RAW_ITERATOR = ScalarType.of("java.util.Iterator"); + private static final ScalarType RAW_ITERATOR = ScalarType.topLevel("java.util.Iterator"); public static final TypeParameter ITERATOR_TYPE_PARAMETER = typeParameter(Identifier.TOP.type(RAW_ITERATOR.getQualifiedName()), "T"); public static Type iterator(Type elementType) { 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 b53b274..e687938 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/IntrinsicInterpreterType.java @@ -28,7 +28,7 @@ public class IntrinsicInterpreterType implements InterpreterType { } public static <T> Builder<T> builder(Class<T> interpreterValueType, String name) { - return builder(interpreterValueType, ScalarType.of(name)); + return builder(interpreterValueType, ScalarType.topLevel(name)); } public static Builder<ObjectInterpreterValue> classBuilder(String name) { diff --git a/src/main/java/org/zwobble/couscous/transforms/HoistNestedTypes.java b/src/main/java/org/zwobble/couscous/transforms/HoistNestedTypes.java index ae4af14..6c40af9 100644 --- a/src/main/java/org/zwobble/couscous/transforms/HoistNestedTypes.java +++ b/src/main/java/org/zwobble/couscous/transforms/HoistNestedTypes.java @@ -12,7 +12,6 @@ import org.zwobble.couscous.util.ExtraSets; import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.regex.Pattern; import static org.zwobble.couscous.ast.MethodCallNode.methodCall; import static org.zwobble.couscous.ast.ReturnNode.returns; @@ -81,25 +80,22 @@ public class HoistNestedTypes { } } - private static final Pattern INNER_TYPE_QUALIFIER_REGEX = Pattern.compile(".*\\.[A-Z][^.]*$"); - private boolean isInnerType(Type type) { - // TODO: implement separate type classes for top-level and inner types rather than using this heuristic - return tryCast(ScalarType.class, type) - .flatMap(scalarType -> scalarType.getQualifier()) - .map(qualifier -> INNER_TYPE_QUALIFIER_REGEX.matcher(qualifier).matches()) + .map(scalarType -> scalarType.outerType().isPresent()) .orElse(false); } private Type outerType(ScalarType type) { - // TODO: implement separate type classes for top-level and inner types - - return ScalarType.of(type.getQualifier().get()); + return type.outerType().get(); } private static Type hoistedTypeName(ScalarType type) { - return ScalarType.of(type.getQualifier().get() + "__" + type.getSimpleName()); + List<String> typeNames = type.getTypeNames(); + return new ScalarType( + type.getPackage(), + cons(typeNames.get(0) + "__" + typeNames.get(1), typeNames.subList(2, typeNames.size())) + ); } private HoistResult hoistInnerTypes(TypeNode declaration) { diff --git a/src/main/java/org/zwobble/couscous/transforms/SplitStaticsFromInterface.java b/src/main/java/org/zwobble/couscous/transforms/SplitStaticsFromInterface.java index 3af4042..f8623fc 100644 --- a/src/main/java/org/zwobble/couscous/transforms/SplitStaticsFromInterface.java +++ b/src/main/java/org/zwobble/couscous/transforms/SplitStaticsFromInterface.java @@ -57,7 +57,7 @@ public class SplitStaticsFromInterface { } private static ScalarType staticTypeFor(ScalarType name) { - return ScalarType.of(name.getQualifiedName() + "_static"); + return ScalarType.topLevel(name.getQualifiedName() + "_static"); } private static boolean isSplittable(TypeNode typeNode) { diff --git a/src/main/java/org/zwobble/couscous/types/ScalarType.java b/src/main/java/org/zwobble/couscous/types/ScalarType.java index 4f77342..4a1bcc3 100644 --- a/src/main/java/org/zwobble/couscous/types/ScalarType.java +++ b/src/main/java/org/zwobble/couscous/types/ScalarType.java @@ -2,40 +2,61 @@ package org.zwobble.couscous.types; import com.google.common.base.Strings; +import java.util.List; import java.util.Optional; import java.util.function.Function; +import static org.zwobble.couscous.util.ExtraLists.*; + public class ScalarType implements Type { - public static ScalarType of(String qualifiedName) { + public static ScalarType topLevel(String qualifiedName) { if (Strings.isNullOrEmpty(qualifiedName)) { throw new IllegalArgumentException("qualifiedName cannot be blank"); } if (qualifiedName.contains("<")) { throw new IllegalArgumentException(qualifiedName + " contains <"); } - return new ScalarType(qualifiedName); + int lastDot = qualifiedName.lastIndexOf('.'); + String className = qualifiedName.substring(lastDot + 1); + Optional<String> packageName = lastDot == -1 ? Optional.empty() : Optional.of(qualifiedName.substring(0, lastDot)); + return new ScalarType(packageName, list(className)); } - + + public static ScalarType innerType(ScalarType outerType, String name) { + return new ScalarType(outerType.packageName, append(outerType.typeNames, name)); + } + + private final Optional<String> packageName; + private final List<String> typeNames; private final String qualifiedName; - - private ScalarType(String qualifiedName) { - this.qualifiedName = qualifiedName; + + public ScalarType(Optional<String> packageName, List<String> typeNames) { + this.packageName = packageName; + this.typeNames = typeNames; + this.qualifiedName = packageName.map(p -> p + ".").orElse("") + String.join(".", typeNames); + } + + public Optional<String> getPackage() { + return packageName; + } + + public List<String> getTypeNames() { + return typeNames; } public String getQualifiedName() { return qualifiedName; } - + public String getSimpleName() { - return qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1); + return getLast(typeNames); } - public Optional<String> getQualifier() { - int lastDot = qualifiedName.lastIndexOf("."); - if (lastDot == -1) { + public Optional<Type> outerType() { + if (typeNames.size() < 2) { return Optional.empty(); } else { - return Optional.of(qualifiedName.substring(0, lastDot)); + return Optional.of(new ScalarType(packageName, typeNames.subList(0, typeNames.size() - 1))); } } diff --git a/src/main/java/org/zwobble/couscous/types/Types.java b/src/main/java/org/zwobble/couscous/types/Types.java index d0ba3d6..6476828 100644 --- a/src/main/java/org/zwobble/couscous/types/Types.java +++ b/src/main/java/org/zwobble/couscous/types/Types.java @@ -8,16 +8,16 @@ import static org.zwobble.couscous.util.ExtraLists.eagerMap; import static org.zwobble.couscous.util.ExtraLists.list; public class Types { - public static final ScalarType VOID = ScalarType.of("void"); - public static final ScalarType BOOLEAN = ScalarType.of("boolean"); - public static final ScalarType INT = ScalarType.of("int"); - public static final ScalarType CHAR = ScalarType.of("char"); - public static final ScalarType STRING = ScalarType.of("java.lang.String"); - 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"); - public static final ScalarType ARRAY = ScalarType.of("array"); + public static final ScalarType VOID = ScalarType.topLevel("void"); + public static final ScalarType BOOLEAN = ScalarType.topLevel("boolean"); + public static final ScalarType INT = ScalarType.topLevel("int"); + public static final ScalarType CHAR = ScalarType.topLevel("char"); + public static final ScalarType STRING = ScalarType.topLevel("java.lang.String"); + public static final ScalarType CLASS = ScalarType.topLevel("java.lang.Class"); + public static final ScalarType OBJECT = ScalarType.topLevel("java.lang.Object"); + public static final ScalarType BOXED_INT = ScalarType.topLevel("java.lang.Integer"); + public static final ScalarType BOXED_BOOLEAN = ScalarType.topLevel("java.lang.Boolean"); + public static final ScalarType ARRAY = ScalarType.topLevel("array"); public static Type addTypeParameters(ScalarType rawType, List<Type> typeParameters) { if (typeParameters.isEmpty()) { diff --git a/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java b/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java index 0665063..f91f5b5 100644 --- a/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java +++ b/src/main/java/org/zwobble/couscous/values/InternalCouscousValue.java @@ -3,5 +3,5 @@ package org.zwobble.couscous.values; import org.zwobble.couscous.types.ScalarType; public class InternalCouscousValue { - public static final ScalarType REF = ScalarType.of("_couscous"); + public static final ScalarType REF = ScalarType.topLevel("_couscous"); } diff --git a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java index c9c7414..aab7a40 100644 --- a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java +++ b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java @@ -42,8 +42,8 @@ public abstract class BackendEvalTests { assertEquals( value(false), evalExpression(same( - constructorCall(ScalarType.of("java.lang.Object"), Collections.emptyList()), - constructorCall(ScalarType.of("java.lang.Object"), Collections.emptyList())))); + constructorCall(ScalarType.topLevel("java.lang.Object"), Collections.emptyList()), + constructorCall(ScalarType.topLevel("java.lang.Object"), Collections.emptyList())))); } @Test @@ -228,7 +228,7 @@ public abstract class BackendEvalTests { @Test public void staticConstructorIsExecutedOnReference() { - ScalarType type = ScalarType.of("com.example.Example"); + ScalarType type = ScalarType.topLevel("com.example.Example"); ClassNode classNode = ClassNode.builder(type) .staticField("value", Types.INT) .staticConstructor(list( diff --git a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java index 94c6e6a..495f7ed 100644 --- a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java +++ b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java @@ -54,7 +54,7 @@ public abstract class CompilerTests { value(720), execTestProgram( "recursive-factorial", - ScalarType.of("com.example.RecursiveFactorial"), + ScalarType.topLevel("com.example.RecursiveFactorial"), "factorial", list(value(6)), Types.INT)); @@ -66,7 +66,7 @@ public abstract class CompilerTests { value(720), execTestProgram( "while-factorial", - ScalarType.of("com.example.WhileFactorial"), + ScalarType.topLevel("com.example.WhileFactorial"), "factorial", list(value(6)), Types.INT)); @@ -78,7 +78,7 @@ public abstract class CompilerTests { value(720), execTestProgram( "for-factorial", - ScalarType.of("com.example.ForFactorial"), + ScalarType.topLevel("com.example.ForFactorial"), "factorial", list(value(6)), Types.INT)); @@ -90,7 +90,7 @@ public abstract class CompilerTests { value("abc"), execTestProgram( "for-each", - ScalarType.of("com.example.ForEach"), + ScalarType.topLevel("com.example.ForEach"), "value", list(), Types.STRING)); @@ -102,7 +102,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "static-nested-class", - ScalarType.of("com.example.StaticNestedClass"), + ScalarType.topLevel("com.example.StaticNestedClass"), "value", list(), Types.INT)); @@ -114,7 +114,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "inner-class", - ScalarType.of("com.example.InnerClass"), + ScalarType.topLevel("com.example.InnerClass"), "run", list(), Types.INT)); @@ -126,7 +126,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "inner-class-with-constructor-args", - ScalarType.of("com.example.InnerClass"), + ScalarType.topLevel("com.example.InnerClass"), "run", list(), Types.INT)); @@ -138,7 +138,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "implement-user-interface", - ScalarType.of("com.example.ConstantIntSupplier"), + ScalarType.topLevel("com.example.ConstantIntSupplier"), "value", list(), Types.INT)); @@ -150,7 +150,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "anonymous-class", - ScalarType.of("com.example.AnonymousClass"), + ScalarType.topLevel("com.example.AnonymousClass"), "value", list(), Types.INT)); @@ -162,7 +162,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "anonymous-class-this-reference", - ScalarType.of("com.example.AnonymousClass"), + ScalarType.topLevel("com.example.AnonymousClass"), "value", list(), Types.INT)); @@ -174,7 +174,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "anonymous-class-capture", - ScalarType.of("com.example.AnonymousClass"), + ScalarType.topLevel("com.example.AnonymousClass"), "value", list(value(42)), Types.INT)); @@ -186,7 +186,7 @@ public abstract class CompilerTests { value("Hello"), execTestProgram( "anonymous-class-type-capture", - ScalarType.of("com.example.AnonymousClass"), + ScalarType.topLevel("com.example.AnonymousClass"), "value", list(), Types.STRING)); @@ -198,7 +198,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "lambda", - ScalarType.of("com.example.Lambda"), + ScalarType.topLevel("com.example.Lambda"), "value", list(), Types.INT)); @@ -210,7 +210,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "lambda-capture", - ScalarType.of("com.example.Lambda"), + ScalarType.topLevel("com.example.Lambda"), "value", list(value(2)), Types.INT)); @@ -222,7 +222,7 @@ public abstract class CompilerTests { value(84), execTestProgram( "lambda-multiple-capture", - ScalarType.of("com.example.Lambda"), + ScalarType.topLevel("com.example.Lambda"), "value", list(value(2)), Types.INT)); @@ -234,7 +234,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "lambda-this-capture", - ScalarType.of("com.example.Lambda"), + ScalarType.topLevel("com.example.Lambda"), "value", list(value(2)), Types.INT)); @@ -246,7 +246,7 @@ public abstract class CompilerTests { value("two"), execTestProgram( "generic-method", - ScalarType.of("com.example.GenericMethod"), + ScalarType.topLevel("com.example.GenericMethod"), "value", list(), Types.STRING)); @@ -258,7 +258,7 @@ public abstract class CompilerTests { value("Hello"), execTestProgram( "generic-interface-return-type", - ScalarType.of("com.example.GenericInterface"), + ScalarType.topLevel("com.example.GenericInterface"), "value", list(), Types.STRING)); @@ -270,7 +270,7 @@ public abstract class CompilerTests { value("Hello"), execTestProgram( "generic-interface-non-generic-methods", - ScalarType.of("com.example.GenericInterface"), + ScalarType.topLevel("com.example.GenericInterface"), "value", list(), Types.STRING)); @@ -282,7 +282,7 @@ public abstract class CompilerTests { value(42), execTestProgram( "static-method-overloads", - ScalarType.of("com.example.StaticMethodOverloads"), + ScalarType.topLevel("com.example.StaticMethodOverloads"), "value", list(), Types.INT)); @@ -306,7 +306,7 @@ public abstract class CompilerTests { value(expected), execTestProgram( "switch-statements", - ScalarType.of("com.example.SwitchStatements"), + ScalarType.topLevel("com.example.SwitchStatements"), methodName, list(value(input)), Types.INT)); @@ -374,7 +374,7 @@ public abstract class CompilerTests { Files.write(directoryPath.resolve("com/example/Example.java"), list(javaClass)); return execProgram( directoryPath, - ScalarType.of("com.example.Example"), + ScalarType.topLevel("com.example.Example"), "main", list(), returnType); diff --git a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java index dbdd483..86397a5 100644 --- a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java +++ b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java @@ -67,7 +67,6 @@ public class ValueObjectTests { {OperationNode.class}, {ParameterizedType.class}, {ReturnNode.class}, - {ScalarType.class}, {StaticReceiver.class}, {TernaryConditionalNode.class}, {ThisReferenceNode.class}, @@ -75,7 +74,6 @@ public class ValueObjectTests { {TryNode.class}, {TypeCoercionNode.class}, {TypeParameter.class}, - {ScalarType.class}, {VariableDeclaration.class}, {VariableReferenceNode.class}, {WhileNode.class} @@ -166,6 +164,8 @@ public class ValueObjectTests { return generateFirstInstance(VariableReferenceNode.class); } else if (type.equals(Type.class)) { return generateFirstInstance(ScalarType.class); + } else if (type.equals(ScalarType.class)) { + return ScalarType.topLevel("T1"); } else { return generateValue(type, ValueObjectTests::generateFirstInstance).instance; } @@ -196,6 +196,8 @@ public class ValueObjectTests { return generateSecondInstance(VariableReferenceNode.class); } else if (type.equals(Type.class)) { return generateSecondInstance(ScalarType.class); + } else if (type.equals(ScalarType.class)) { + return ScalarType.topLevel("T2"); } else { return generateValue(type, ValueObjectTests::generateSecondInstance).instance; } @@ -226,6 +228,8 @@ public class ValueObjectTests { return generateInstance(VariableReferenceNode.class); } else if (type.equals(Type.class)) { return generateInstance(ScalarType.class); + } else if (type.equals(ScalarType.class)) { + return ScalarType.topLevel("T"); } else { return generateValue(type, ValueObjectTests::generateInstance).instance; } diff --git a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java index 822ee8b..68b053d 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java @@ -75,7 +75,7 @@ public class CsharpMethodRunner implements MethodRunner { String csharpMethodName = CsharpCodeGenerator.NAMING.methodName(signature); String value = CsharpSerializer.serialize( staticMethodCall( - ScalarType.of(NAMESPACE + "." + className.getQualifiedName()), + ScalarType.topLevel(NAMESPACE + "." + className.getQualifiedName()), csharpMethodName, eagerMap(arguments, LiteralNode::literal), returnType)); @@ -200,7 +200,7 @@ public class CsharpMethodRunner implements MethodRunner { return value(Types.STRING); } else { assertThat(value, Matchers.startsWith(NAMESPACE_PREFIX)); - return value(ScalarType.of(value.substring(NAMESPACE_PREFIX.length()))); + return value(ScalarType.topLevel(value.substring(NAMESPACE_PREFIX.length()))); } default: throw new RuntimeException("Unhandled type: " + type); diff --git a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java index 636b214..ca15ba9 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpSerializerTests.java @@ -70,49 +70,49 @@ public class CsharpSerializerTests { @Test public void typeLiteralUsesTypeOfOperator() { - String output = serialize(literal(ScalarType.of("com.example.Example"))); + String output = serialize(literal(ScalarType.topLevel("com.example.Example"))); assertEquals("typeof(com.example.Example)", output); } @Test public void typeLiteralUsesAtSymbolToEscapeReservedIdentifiers() { - String output = serialize(literal(ScalarType.of("internal"))); + String output = serialize(literal(ScalarType.topLevel("internal"))); assertEquals("typeof(@internal)", output); } @Test public void typeLiteralDoesntUseAtSymbolForReservedTypeIdentifiers() { - String output = serialize(literal(ScalarType.of("string"))); + String output = serialize(literal(ScalarType.topLevel("string"))); assertEquals("typeof(string)", output); } @Test public void variableReferenceWritesIdentifier() { - String output = serialize(reference(var(TestIds.ANY_ID, "x", ScalarType.of("X")))); + String output = serialize(reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X")))); assertEquals("x", output); } @Test public void identifierIsEscapedWithAtSymbolIfItsAReservedWord() { - String output = serialize(reference(var(TestIds.ANY_ID, "internal", ScalarType.of("X")))); + String output = serialize(reference(var(TestIds.ANY_ID, "internal", ScalarType.topLevel("X")))); assertEquals("@internal", output); } @Test public void thisReferenceUsesThisKeyword() { - String output = serialize(thisReference(ScalarType.of("X"))); + String output = serialize(thisReference(ScalarType.topLevel("X"))); assertEquals("this", output); } @Test public void emptyArraySpecifiesTypeOfArray() { - String output = serialize(array(ScalarType.of("X"), list())); + String output = serialize(array(ScalarType.topLevel("X"), list())); assertEquals("new X[] {}", output); } @Test public void arrayElementsAreSeparatedByComma() { - String output = serialize(array(ScalarType.of("X"), list(literal(1), literal(2)))); + String output = serialize(array(ScalarType.topLevel("X"), list(literal(1), literal(2)))); assertEquals("new X[] {1, 2}", output); } @@ -133,49 +133,49 @@ public class CsharpSerializerTests { @Test public void staticMethodCallWithNoArgumentsWritesStaticReceiver() { String output = serialize(staticMethodCall( - ScalarType.of("X"), + ScalarType.topLevel("X"), "y", list(), - ScalarType.of("Y"))); + ScalarType.topLevel("Y"))); assertEquals("X.y()", output); } @Test public void methodCallWithNoArgumentsWritesReceiver() { String output = serialize(methodCall( - reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))), + reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))), "y", list(), - ScalarType.of("Y"))); + ScalarType.topLevel("Y"))); assertEquals("x.y()", output); } @Test public void methodCallWithArguments() { String output = serialize(methodCall( - reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))), + reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))), "y", list(literal(1), literal(2)), - ScalarType.of("Y"))); + ScalarType.topLevel("Y"))); assertEquals("x.y(1, 2)", output); } @Test public void methodCallWithTypeParameters() { String output = serialize(methodCall( - instanceReceiver(reference(var(TestIds.ANY_ID, "x", ScalarType.of("X")))), + instanceReceiver(reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X")))), "y", - list(ScalarType.of("Z")), + list(ScalarType.topLevel("Z")), list(), - ScalarType.of("Y"), - signature("y", list(typeParameter(Identifier.forType("X").method("y"), "T")), list(), ScalarType.of("Y")))); + ScalarType.topLevel("Y"), + signature("y", list(typeParameter(Identifier.forType("X").method("y"), "T")), list(), ScalarType.topLevel("Y")))); assertEquals("x.y<Z>()", output); } @Test public void constructorCallWithNoArguments() { String output = serialize(constructorCall( - ScalarType.of("X"), + ScalarType.topLevel("X"), list())); assertEquals("new X()", output); } @@ -183,7 +183,7 @@ public class CsharpSerializerTests { @Test public void constructorCallWithArguments() { String output = serialize(constructorCall( - ScalarType.of("X"), + ScalarType.topLevel("X"), list(literal(1), literal(2)))); assertEquals("new X(1, 2)", output); } @@ -205,7 +205,7 @@ public class CsharpSerializerTests { public void instanceOfIsWrittenUsingIsOperator() { String output = serialize(instanceOf( reference(var(TestIds.ANY_ID, "x", Types.OBJECT)), - ScalarType.of("X"))); + ScalarType.topLevel("X"))); assertEquals("x is X", output); } @@ -218,26 +218,26 @@ public class CsharpSerializerTests { @Test public void fieldAccessSeparatesReceiverAndNameWithDot() { String output = serialize(fieldAccess( - reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))), + reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))), "y", - ScalarType.of("Y"))); + ScalarType.topLevel("Y"))); assertEquals("x.y", output); } @Test public void staticFieldAccessSeparatesReceiverAndNameWithDot() { String output = serialize(fieldAccess( - ScalarType.of("X"), + ScalarType.topLevel("X"), "y", - ScalarType.of("Y"))); + ScalarType.topLevel("Y"))); assertEquals("X.y", output); } @Test public void castIsWrittenWithParenthesisedType() { String output = serialize(cast( - reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))), - ScalarType.of("Y"))); + reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))), + ScalarType.topLevel("Y"))); assertEquals("(Y) x", output); } @@ -255,7 +255,7 @@ public class CsharpSerializerTests { @Test public void throwStatementIncludesValue() { - String output = serialize(throwNode(reference(var(TestIds.ANY_ID, "x", ScalarType.of("X"))))); + String output = serialize(throwNode(reference(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X"))))); assertEquals("throw x;\n", output); } @@ -270,7 +270,7 @@ public class CsharpSerializerTests { String output = serialize(localVariableDeclaration( TestIds.ANY_ID, "x", - ScalarType.of("X"), + ScalarType.topLevel("X"), literal("[value]"))); assertEquals("X x = \"[value]\";\n", output); } @@ -280,7 +280,7 @@ public class CsharpSerializerTests { String output = serialize(localVariableDeclaration( TestIds.ANY_ID, "x", - ScalarType.of("string"), + ScalarType.topLevel("string"), literal("[value]"))); assertEquals("string x = \"[value]\";\n", output); } @@ -349,8 +349,8 @@ public class CsharpSerializerTests { @Test public void tryStatementCanHaveBodyAndMultipleHandlers() { - VariableDeclaration varX = var(TestIds.variable("x"), "x", ScalarType.of("X")); - VariableDeclaration varY = var(TestIds.variable("y"), "y", ScalarType.of("Y")); + VariableDeclaration varX = var(TestIds.variable("x"), "x", ScalarType.topLevel("X")); + VariableDeclaration varY = var(TestIds.variable("y"), "y", ScalarType.topLevel("Y")); String output = serialize(tryStatement( list(returns(literal(1))), list( @@ -372,7 +372,7 @@ public class CsharpSerializerTests { @Test public void methodCanHaveVoidReturnType() { String output = serialize(MethodNode.staticMethod("nothing") - .returns(ScalarType.of("void")) + .returns(ScalarType.topLevel("void")) .build()); assertEquals("public static void nothing() {\n}\n", output); } @@ -380,7 +380,7 @@ public class CsharpSerializerTests { @Test public void instanceMethodHasNoStaticKeword() { String output = serialize(MethodNode.builder("nothing") - .returns(ScalarType.of("void")) + .returns(ScalarType.topLevel("void")) .build()); assertEquals("public void nothing() {\n}\n", output); } @@ -388,7 +388,7 @@ public class CsharpSerializerTests { @Test public void methodWithReturnType() { MethodNode methodNode = MethodNode.staticMethod("nothing") - .returns(ScalarType.of("X")) + .returns(ScalarType.topLevel("X")) .build(); String output = serialize(methodNode); @@ -399,9 +399,9 @@ public class CsharpSerializerTests { @Test public void methodWithArguments() { MethodNode methodNode = MethodNode.staticMethod("nothing") - .argument(formalArg(var(TestIds.variable("x"), "x", ScalarType.of("X")))) - .argument(formalArg(var(TestIds.variable("y"), "y", ScalarType.of("Y")))) - .returns(ScalarType.of("void")) + .argument(formalArg(var(TestIds.variable("x"), "x", ScalarType.topLevel("X")))) + .argument(formalArg(var(TestIds.variable("y"), "y", ScalarType.topLevel("Y")))) + .returns(ScalarType.topLevel("void")) .build(); String output = serialize(methodNode); @@ -412,7 +412,7 @@ public class CsharpSerializerTests { @Test public void methodHasSerializedBody() { MethodNode method = MethodNode.staticMethod("nothing") - .returns(ScalarType.of("void")) + .returns(ScalarType.topLevel("void")) .statement(returns(literal(true))) .build(); String output = serialize(method); @@ -424,7 +424,7 @@ public class CsharpSerializerTests { MethodNode methodNode = MethodNode.staticMethod("nothing") .typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "T")) .typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "U")) - .returns(ScalarType.of("void")) + .returns(ScalarType.topLevel("void")) .build(); String output = serialize(methodNode); @@ -457,7 +457,7 @@ public class CsharpSerializerTests { String output = serialize(localVariableDeclaration( TestIds.ANY_ID, "x", - ScalarType.of("com.example.internal.Example"), + ScalarType.topLevel("com.example.internal.Example"), literal("[value]"))); assertEquals("com.example.@internal.Example x = \"[value]\";\n", output); } @@ -467,8 +467,8 @@ public class CsharpSerializerTests { ClassNode classNode = ClassNode.builder("com.example.Example") .addTypeParameter("T") .addTypeParameter("U") - .field("x", ScalarType.of("X")) - .staticField("y", ScalarType.of("Y")) + .field("x", ScalarType.topLevel("X")) + .staticField("y", ScalarType.topLevel("Y")) .build(); String output = serialize(classNode); @@ -506,7 +506,7 @@ public class CsharpSerializerTests { @Test public void constructorHasSerializedBody() { ConstructorNode constructor = ConstructorNode.constructor( - list(formalArg(var(TestIds.ANY_ID, "x", ScalarType.of("X")))), + list(formalArg(var(TestIds.ANY_ID, "x", ScalarType.topLevel("X")))), list(expressionStatement(literal(true)))); ClassNode classNode = ClassNode.builder("com.example.Example") .constructor(constructor) @@ -521,8 +521,8 @@ public class CsharpSerializerTests { @Test public void classWithFields() { ClassNode classNode = ClassNode.builder("com.example.Example") - .staticField("x", ScalarType.of("X")) - .field("y", ScalarType.of("Y")) + .staticField("x", ScalarType.topLevel("X")) + .field("y", ScalarType.topLevel("Y")) .build(); String output = serialize(classNode); @@ -532,7 +532,7 @@ public class CsharpSerializerTests { @Test public void interfaceIsInNamespace() { - Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")).buildInterface(); + Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example")).buildInterface(); String output = serialize(node); @@ -541,7 +541,7 @@ public class CsharpSerializerTests { @Test public void interfaceWithTypeParameters() { - Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")) + Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example")) .addTypeParameter("T") .addTypeParameter("U") .buildInterface(); @@ -553,7 +553,7 @@ public class CsharpSerializerTests { @Test public void interfaceWithSuperTypes() { - Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")) + Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example")) .addSuperType("com.example.Base") .buildInterface(); @@ -564,8 +564,8 @@ public class CsharpSerializerTests { @Test public void interfaceWithMethod() { - MethodNode method = MethodNode.builder("get").isAbstract().returns(ScalarType.of("X")).build(); - Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")) + MethodNode method = MethodNode.builder("get").isAbstract().returns(ScalarType.topLevel("X")).build(); + Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example")) .method(method) .buildInterface(); @@ -579,10 +579,10 @@ public class CsharpSerializerTests { MethodNode method = MethodNode.staticMethod("nothing") .typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "T")) .typeParameter(formalTypeParameter(Identifier.forType("com.Example.example").method("nothing"), "U")) - .returns(ScalarType.of("void")) + .returns(ScalarType.topLevel("void")) .build(); - Node node = new ClassNodeBuilder(ScalarType.of("com.example.Example")) + Node node = new ClassNodeBuilder(ScalarType.topLevel("com.example.Example")) .method(method) .buildInterface(); @@ -593,7 +593,7 @@ public class CsharpSerializerTests { @Test public void enumWithSimpleValues() { - Node node = declareEnum(ScalarType.of("com.example.Example"), list("ONE", "TWO")); + Node node = declareEnum(ScalarType.topLevel("com.example.Example"), list("ONE", "TWO")); String output = serialize(node); @@ -602,7 +602,7 @@ public class CsharpSerializerTests { @Test public void arrayTypeUsesSquareBrackets() { - String output = serialize(formalArg(var(TestIds.ANY_ID, "x", Types.array(ScalarType.of("string"))))); + String output = serialize(formalArg(var(TestIds.ANY_ID, "x", Types.array(ScalarType.topLevel("string"))))); assertEquals("string[] x", output); } diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java index 87ece2d..e9bcdf0 100644 --- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java +++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonMethodRunner.java @@ -86,7 +86,7 @@ public class PythonMethodRunner implements MethodRunner { case "<class 'type'>": String typeName = value.substring(value.indexOf("'") + 1, value.lastIndexOf("'")); assertThat(typeName, Matchers.startsWith(PACKAGE_PREFIX)); - return value(ScalarType.of(typeName.substring(PACKAGE_PREFIX.length(), typeName.lastIndexOf(".")))); + return value(ScalarType.topLevel(typeName.substring(PACKAGE_PREFIX.length(), typeName.lastIndexOf(".")))); default: throw new RuntimeException("Unhandled type: " + type); } diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java index e7b2f66..4aa015e 100644 --- a/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java +++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/ExpressionReadingTests.java @@ -65,7 +65,7 @@ public class ExpressionReadingTests { @Test public void canReadThisReference() { assertEquals( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), readExpressionInInstanceMethod("com.example.Example", "this")); } @@ -81,7 +81,7 @@ public class ExpressionReadingTests { "private String name;"); assertEquals( - list(field("name", ScalarType.of("java.lang.String"))), + list(field("name", ScalarType.topLevel("java.lang.String"))), classNode.getFields()); } @@ -91,7 +91,7 @@ public class ExpressionReadingTests { "private static String name;"); assertEquals( - list(staticField("name", ScalarType.of("java.lang.String"))), + list(staticField("name", ScalarType.topLevel("java.lang.String"))), classNode.getFields()); } @@ -115,7 +115,7 @@ public class ExpressionReadingTests { ReturnNode returnNode = (ReturnNode) classNode.getMethods().get(0).getBody().get().get(0); assertEquals( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "name", Types.STRING), returnNode.getValue()); @@ -132,7 +132,7 @@ public class ExpressionReadingTests { ReturnNode returnNode = (ReturnNode) classNode.getMethods().get(0).getBody().get().get(1); assertThat(returnNode.getValue(), isFieldAccess( - fieldHasReceiver(isVariableReference("self", ScalarType.of("com.example.Example"))), + fieldHasReceiver(isVariableReference("self", ScalarType.topLevel("com.example.Example"))), fieldHasName("name"), expressionHasType(Types.STRING))); } @@ -167,7 +167,7 @@ public class ExpressionReadingTests { ReturnNode returnNode = (ReturnNode) classNode.getMethods().get(0).getBody().get().get(0); assertEquals( fieldAccess( - ScalarType.of("com.example.Example"), + ScalarType.topLevel("com.example.Example"), "name", Types.STRING), returnNode.getValue()); @@ -197,7 +197,7 @@ public class ExpressionReadingTests { assertEquals( methodCall( - ThisReferenceNode.thisReference(ScalarType.of("com.example.Example")), + ThisReferenceNode.thisReference(ScalarType.topLevel("com.example.Example")), "loop", list(), Types.STRING), @@ -208,7 +208,7 @@ public class ExpressionReadingTests { public void canReadStaticMethodCalls() { assertEquals( staticMethodCall( - ScalarType.of("java.lang.Integer"), + ScalarType.topLevel("java.lang.Integer"), "parseInt", list(literal("42")), Types.INT), @@ -225,7 +225,7 @@ public class ExpressionReadingTests { assertEquals( staticMethodCall( - ScalarType.of("com.example.Example"), + ScalarType.topLevel("com.example.Example"), "loop", list(), Types.STRING), @@ -241,12 +241,12 @@ public class ExpressionReadingTests { "asList", list(Types.STRING), list(array(Types.STRING, list())), - parameterizedType(ScalarType.of("java.util.List"), list(Types.STRING)), + parameterizedType(ScalarType.topLevel("java.util.List"), list(Types.STRING)), signature( "asList", list(typeParameter), list(Types.array(typeParameter)), - parameterizedType(ScalarType.of("java.util.List"), list(typeParameter)))), + parameterizedType(ScalarType.topLevel("java.util.List"), list(typeParameter)))), readExpression("java.util.List<String>", "java.util.Arrays.asList()")); } @@ -259,12 +259,12 @@ public class ExpressionReadingTests { "asList", list(Types.STRING), list(array(Types.STRING, list(literal("one"), literal("two")))), - parameterizedType(ScalarType.of("java.util.List"), list(Types.STRING)), + parameterizedType(ScalarType.topLevel("java.util.List"), list(Types.STRING)), signature( "asList", list(typeParameter), list(Types.array(typeParameter)), - parameterizedType(ScalarType.of("java.util.List"), list(typeParameter)))), + parameterizedType(ScalarType.topLevel("java.util.List"), list(typeParameter)))), readExpression("java.util.List<String>", "java.util.Arrays.asList(\"one\", \"two\")")); } @@ -277,12 +277,12 @@ public class ExpressionReadingTests { "asList", list(Types.STRING), list(array(Types.STRING, list())), - parameterizedType(ScalarType.of("java.util.List"), list(Types.STRING)), + parameterizedType(ScalarType.topLevel("java.util.List"), list(Types.STRING)), signature( "asList", list(typeParameter), list(Types.array(typeParameter)), - parameterizedType(ScalarType.of("java.util.List"), list(typeParameter)))), + parameterizedType(ScalarType.topLevel("java.util.List"), list(typeParameter)))), readExpression("java.util.List<String>", "java.util.Arrays.asList(new String[]{})")); } @@ -295,12 +295,12 @@ public class ExpressionReadingTests { "emptyList", list(Types.STRING), list(), - parameterizedType(ScalarType.of("java.util.List"), list(Types.STRING)), + parameterizedType(ScalarType.topLevel("java.util.List"), list(Types.STRING)), signature( "emptyList", list(typeParameter), list(), - parameterizedType(ScalarType.of("java.util.List"), list(typeParameter)))), + parameterizedType(ScalarType.topLevel("java.util.List"), list(typeParameter)))), readExpression("java.util.List<String>", "java.util.Collections.<String>emptyList()")); } @@ -313,19 +313,19 @@ public class ExpressionReadingTests { "emptyList", list(Types.STRING), list(), - parameterizedType(ScalarType.of("java.util.List"), list(Types.STRING)), + parameterizedType(ScalarType.topLevel("java.util.List"), list(Types.STRING)), signature( "emptyList", list(typeParameter), list(), - parameterizedType(ScalarType.of("java.util.List"), list(typeParameter)))), + parameterizedType(ScalarType.topLevel("java.util.List"), list(typeParameter)))), readExpression("java.util.List<String>", "java.util.Collections.emptyList()")); } @Test public void canReadConstructorCalls() { assertEquals( - constructorCall(ScalarType.of("java.lang.String"), list(literal("_"))), + constructorCall(ScalarType.topLevel("java.lang.String"), list(literal("_"))), readExpression("String", "new String(\"_\")")); } @@ -343,20 +343,20 @@ public class ExpressionReadingTests { public void canUseOperatorsOnReferences() { assertEquals( same( - constructorCall(ScalarType.of("java.lang.Object"), emptyList()), - constructorCall(ScalarType.of("java.lang.Object"), emptyList())), + constructorCall(ScalarType.topLevel("java.lang.Object"), emptyList()), + constructorCall(ScalarType.topLevel("java.lang.Object"), emptyList())), readBooleanExpression("new Object() == new Object()")); assertEquals( not(same( - constructorCall(ScalarType.of("java.lang.Object"), emptyList()), - constructorCall(ScalarType.of("java.lang.Object"), emptyList()))), + constructorCall(ScalarType.topLevel("java.lang.Object"), emptyList()), + constructorCall(ScalarType.topLevel("java.lang.Object"), emptyList()))), readBooleanExpression("new Object() != new Object()")); assertEquals( instanceOf( - constructorCall(ScalarType.of("java.lang.Object"), emptyList()), - ScalarType.of("java.lang.Object")), + constructorCall(ScalarType.topLevel("java.lang.Object"), emptyList()), + ScalarType.topLevel("java.lang.Object")), readBooleanExpression("new Object() instanceof Object")); } @@ -417,13 +417,13 @@ public class ExpressionReadingTests { assertEquals( equal( literal(1), - unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(1))))), + unboxInt(constructorCall(ScalarType.topLevel("java.lang.Integer"), list(literal(1))))), readBooleanExpression("1 == new Integer(1)")); assertEquals( notEqual( literal(1), - unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(1))))), + unboxInt(constructorCall(ScalarType.topLevel("java.lang.Integer"), list(literal(1))))), readBooleanExpression("1 != new Integer(1)")); } @@ -446,8 +446,8 @@ public class ExpressionReadingTests { public void integerOperatorUnboxesWhenBothOperandsAreBoxed() { assertEquals( integerAdd( - unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(1)))), - unboxInt(constructorCall(ScalarType.of("java.lang.Integer"), list(literal(2))))), + unboxInt(constructorCall(ScalarType.topLevel("java.lang.Integer"), list(literal(1)))), + unboxInt(constructorCall(ScalarType.topLevel("java.lang.Integer"), list(literal(2))))), readIntExpression("new Integer(1) + new Integer(2)")); } @@ -470,7 +470,7 @@ public class ExpressionReadingTests { assertEquals( assign( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "name", Types.STRING), literal("blah")), @@ -489,7 +489,7 @@ public class ExpressionReadingTests { assertEquals( assign( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "value", Types.OBJECT), typeCoercion(literal(4), Types.OBJECT)), diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java index 7b208f5..7410324 100644 --- a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java +++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java @@ -92,7 +92,7 @@ public class JavaReaderTests { "}"); MethodNode method = classNode.getMethods().get(0); - assertThat(method.getArguments(), contains(isFormalArgument("value", ScalarType.of("int")))); + assertThat(method.getArguments(), contains(isFormalArgument("value", ScalarType.topLevel("int")))); ReturnNode returnNode = (ReturnNode)method.getBody().get().get(0); assertEquals(reference(method.getArguments().get(0)), returnNode.getValue()); @@ -119,7 +119,7 @@ public class JavaReaderTests { public void canReadThrowStatements() { assertEquals( throwNode( - constructorCall(ScalarType.of("java.lang.RuntimeException"), list())), + constructorCall(ScalarType.topLevel("java.lang.RuntimeException"), list())), readStatement("void", "throw new RuntimeException();")); } @@ -128,7 +128,7 @@ public class JavaReaderTests { assertEquals( expressionStatement( staticMethodCall( - ScalarType.of("java.lang.Integer"), + ScalarType.topLevel("java.lang.Integer"), "parseInt", list(literal("42")), Types.INT)), @@ -224,7 +224,7 @@ public class JavaReaderTests { isTryStatement( hasTryBody(list(returns(literal(1)))), hasExceptionHandlers( - isExceptionHandler(ScalarType.of("java.lang.Exception"), "exception", list(returns(literal(2))))))); + isExceptionHandler(ScalarType.topLevel("java.lang.Exception"), "exception", list(returns(literal(2))))))); } @Test @@ -249,7 +249,7 @@ public class JavaReaderTests { @Test public void canReadTryWithResourceWithoutOtherClauses() { - ScalarType type = ScalarType.of("java.io.ByteArrayOutputStream"); + ScalarType type = ScalarType.topLevel("java.io.ByteArrayOutputStream"); Identifier identifier = MAIN_IDENTIFIER.variable("closeable"); LocalVariableDeclarationNode expectedResource = localVariableDeclaration(identifier, "closeable", type, constructorCall(type, list())); assertThat( @@ -292,7 +292,7 @@ public class JavaReaderTests { assertEquals( list(assignStatement( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "name", Types.STRING), literal("Flaws"))), @@ -315,13 +315,13 @@ public class JavaReaderTests { list( assignStatement( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "year", Types.INT), literal(2013)), assignStatement( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "name", Types.STRING), literal("Flaws"))), @@ -343,13 +343,13 @@ public class JavaReaderTests { list( assignStatement( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "year", Types.INT), literal(2013)), assignStatement( fieldAccess( - thisReference(ScalarType.of("com.example.Example")), + thisReference(ScalarType.topLevel("com.example.Example")), "name", Types.STRING), literal("Flaws"))), @@ -369,7 +369,7 @@ public class JavaReaderTests { assertEquals( list(assignStatement( fieldAccess( - ScalarType.of("com.example.Example"), + ScalarType.topLevel("com.example.Example"), "name", Types.STRING), literal("Flaws"))), @@ -386,7 +386,7 @@ public class JavaReaderTests { assertEquals( list(assignStatement( fieldAccess( - ScalarType.of("com.example.Example"), + ScalarType.topLevel("com.example.Example"), "name", Types.STRING), literal("Flaws"))), @@ -400,7 +400,7 @@ public class JavaReaderTests { MethodNode method = classNode.getMethods().get(0); assertEquals( - list(annotation(ScalarType.of("java.lang.Deprecated"))), + list(annotation(ScalarType.topLevel("java.lang.Deprecated"))), method.getAnnotations()); } @@ -422,7 +422,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); InterfaceNode classNode = (InterfaceNode) classes.get(0); - assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.topLevel("com.example.Example"), classNode.getName()); assertEquals(list(), classNode.getMethods()); assertEquals(set(), classNode.getSuperTypes()); assertEquals(list(), classNode.getTypeParameters()); @@ -435,7 +435,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); ClassNode classNode = (ClassNode) classes.get(0); - assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.topLevel("com.example.Example"), classNode.getName()); assertEquals(list(formalTypeParameter(Identifier.forType("com.example.Example"), "T")), classNode.getTypeParameters()); } @@ -446,7 +446,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); InterfaceNode classNode = (InterfaceNode) classes.get(0); - assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.topLevel("com.example.Example"), classNode.getName()); assertEquals(list(formalTypeParameter(Identifier.forType("com.example.Example"), "T")), classNode.getTypeParameters()); } @@ -457,9 +457,9 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); InterfaceNode classNode = (InterfaceNode) classes.get(0); - assertEquals(ScalarType.of("com.example.Example"), classNode.getName()); + assertEquals(ScalarType.topLevel("com.example.Example"), classNode.getName()); assertEquals(list(), classNode.getMethods()); - assertThat(classNode.getSuperTypes(), contains(ScalarType.of("java.util.function.IntSupplier"))); + assertThat(classNode.getSuperTypes(), contains(ScalarType.topLevel("java.util.function.IntSupplier"))); } @Test @@ -493,7 +493,7 @@ public class JavaReaderTests { assertEquals( list( assignStatement( - FieldAccessNode.fieldAccess(ScalarType.of("com.example.Example"), "VALUE", Types.INT), + FieldAccessNode.fieldAccess(ScalarType.topLevel("com.example.Example"), "VALUE", Types.INT), literal(1) ) ), @@ -508,7 +508,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); EnumNode node = (EnumNode) classes.get(0); - assertEquals(ScalarType.of("com.example.Example"), node.getName()); + assertEquals(ScalarType.topLevel("com.example.Example"), node.getName()); } @Test @@ -518,7 +518,7 @@ public class JavaReaderTests { List<TypeNode> classes = readSource("com/example/Example.java", source); assertThat(classes, hasSize(1)); EnumNode node = (EnumNode) classes.get(0); - assertEquals(ScalarType.of("com.example.Example"), node.getName()); + assertEquals(ScalarType.topLevel("com.example.Example"), node.getName()); assertEquals(list("ONE", "TWO"), node.getValues()); } diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaTypesTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaTypesTests.java index f903eeb..f256fa9 100644 --- a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaTypesTests.java +++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaTypesTests.java @@ -13,7 +13,7 @@ import static org.zwobble.couscous.types.TypeParameter.typeParameter; import static org.zwobble.couscous.util.ExtraLists.list; public class JavaTypesTests { - private static final ScalarType INT_TYPE = ScalarType.of("int"); + private static final ScalarType INT_TYPE = ScalarType.topLevel("int"); @Test public void bindingTypeToItselfReturnsType() { @@ -32,7 +32,7 @@ public class JavaTypesTests { @Test public void bindingTypeToParameterizedTypeBindsParameters() { - ScalarType rawList = ScalarType.of("List"); + ScalarType rawList = ScalarType.topLevel("List"); TypeParameter typeParameter = typeParameter(Identifier.forType("List"), "T"); assertEquals( parameterizedType(rawList, list(boundTypeParameter(typeParameter, INT_TYPE))), diff --git a/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java b/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java index 79c082d..f0cfba7 100644 --- a/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java +++ b/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java @@ -178,7 +178,7 @@ public class EvaluatorTests extends BackendEvalTests { @Test public void cannotSetValueOfInstanceFieldAsThoughItIsAStaticField() { - ScalarType type = ScalarType.of("com.example.Example"); + ScalarType type = ScalarType.topLevel("com.example.Example"); ClassNode classNode = ClassNode.builder(type) .field("value", Types.INT) .constructor(constructor -> constructor @@ -253,7 +253,7 @@ public class EvaluatorTests extends BackendEvalTests { @Test public void typeOfValueCanBeParameterisedType() { - ScalarType rawType = ScalarType.of("Box"); + ScalarType rawType = ScalarType.topLevel("Box"); ClassNode classNode = ClassNode.builder(rawType) .addTypeParameter("T") .build(); |
