aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-08 08:42:49 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-08 08:42:49 +0000
commit0577f8637123c92fa6c1d4e27e0d2281c9a6b519 (patch)
treeedaa735a2582030b1d6da5a66eef15e90bcc40bc
parent17072b4d14dad00acac247508e35b52c0233170b (diff)
Use list() to create lists of fixed size
-rw-r--r--src/main/java/org/zwobble/couscous/ast/MethodCallNode.java5
-rw-r--r--src/main/java/org/zwobble/couscous/ast/StaticMethodCallNode.java3
-rw-r--r--src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java4
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java9
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java6
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java15
-rw-r--r--src/main/java/org/zwobble/couscous/backends/python/ast/PythonBlock.java3
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java3
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java5
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java3
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java18
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java7
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java8
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java9
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java16
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java25
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/InternalCouscousInterpreterValue.java3
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java3
-rw-r--r--src/main/java/org/zwobble/couscous/util/ExtraLists.java14
-rw-r--r--src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java31
-rw-r--r--src/test/java/org/zwobble/couscous/tests/BackendMethodTests.java15
-rw-r--r--src/test/java/org/zwobble/couscous/tests/CompilerTests.java23
-rw-r--r--src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java3
-rw-r--r--src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java3
-rw-r--r--src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java7
-rw-r--r--src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java5
-rw-r--r--src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java17
-rw-r--r--src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java9
-rw-r--r--src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java55
-rw-r--r--src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java33
-rw-r--r--src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java3
31 files changed, 202 insertions, 161 deletions
diff --git a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java
index ba50f17..076ac9e 100644
--- a/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java
+++ b/src/main/java/org/zwobble/couscous/ast/MethodCallNode.java
@@ -10,6 +10,7 @@ import java.util.function.Function;
import static java.util.Arrays.asList;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class MethodCallNode implements ExpressionNode {
public static ExpressionNode not(ExpressionNode value) {
@@ -41,7 +42,7 @@ public class MethodCallNode implements ExpressionNode {
}
public static MethodCallNode integerOperation(String methodName, ExpressionNode left, ExpressionNode right) {
- return methodCall(left, methodName, asList(right), IntegerValue.REF);
+ return methodCall(left, methodName, list(right), IntegerValue.REF);
}
public static ExpressionNode equal(ExpressionNode left, ExpressionNode right) {
@@ -69,7 +70,7 @@ public class MethodCallNode implements ExpressionNode {
}
private static ExpressionNode booleanOperation(String methodName, ExpressionNode left, ExpressionNode right) {
- return methodCall(left, methodName, asList(right), BooleanValue.REF);
+ return methodCall(left, methodName, list(right), BooleanValue.REF);
}
public static MethodCallNode methodCall(
diff --git a/src/main/java/org/zwobble/couscous/ast/StaticMethodCallNode.java b/src/main/java/org/zwobble/couscous/ast/StaticMethodCallNode.java
index 5302f0a..4676b67 100644
--- a/src/main/java/org/zwobble/couscous/ast/StaticMethodCallNode.java
+++ b/src/main/java/org/zwobble/couscous/ast/StaticMethodCallNode.java
@@ -12,10 +12,11 @@ import org.zwobble.couscous.values.ObjectValues;
import static java.util.Arrays.asList;
import static org.zwobble.couscous.ast.TypeCoercionNode.typeCoercion;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class StaticMethodCallNode implements ExpressionNode {
public static ExpressionNode same(ExpressionNode left, ExpressionNode right) {
- return staticMethodCall(InternalCouscousValue.REF, "same", asList(left, right), BooleanValue.REF);
+ return staticMethodCall(InternalCouscousValue.REF, "same", list(left, right), BooleanValue.REF);
}
public static ExpressionNode boxInt(ExpressionNode value) {
diff --git a/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java b/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java
index 29a33eb..0c0d047 100644
--- a/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java
+++ b/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java
@@ -4,8 +4,10 @@ import com.google.common.collect.ImmutableList;
import java.util.List;
+import static org.zwobble.couscous.util.ExtraLists.list;
+
public class Identifier {
- public static final Identifier TOP = new Identifier(ImmutableList.of());
+ public static final Identifier TOP = new Identifier(list());
private final List<String> parts;
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java
index d2f89ba..cc2ec06 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PrimitiveMethods.java
@@ -22,6 +22,7 @@ import static org.zwobble.couscous.backends.python.ast.PythonBinaryOperation.pyt
import static org.zwobble.couscous.backends.python.ast.PythonCallNode.pythonCall;
import static org.zwobble.couscous.backends.python.ast.PythonGetSliceNode.pythonGetSlice;
import static org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode.pythonVariableReference;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class PrimitiveMethods {
private static final Map<String, PrimitiveMethodGenerator> STRING_METHODS =
@@ -30,7 +31,7 @@ public class PrimitiveMethods {
.put("length", (receiver, arguments) ->
pythonCall(
pythonVariableReference("len"),
- asList(receiver)))
+ list(receiver)))
.put("substring", (receiver, arguments) ->
pythonGetSlice(receiver, arguments))
@@ -51,7 +52,7 @@ public class PrimitiveMethods {
ImmutableMap.Builder<String, PrimitiveMethodGenerator> methods = ImmutableMap.builder();
methods.put("toString", (receiver, arguments) ->
- pythonCall(pythonVariableReference("str"), ImmutableList.of(receiver)));
+ pythonCall(pythonVariableReference("str"), list(receiver)));
addOperation(methods, Operator.ADD.getMethodName(), "+");
addOperation(methods, Operator.SUBTRACT.getMethodName(), "-");
@@ -60,11 +61,11 @@ public class PrimitiveMethods {
methods.put(Operator.DIVIDE.getMethodName(), (receiver, arguments) ->
pythonCall(
internalReference("_div_round_to_zero"),
- asList(receiver, arguments.get(0))));
+ list(receiver, arguments.get(0))));
methods.put(Operator.MOD.getMethodName(), (receiver, arguments) ->
pythonCall(
internalReference("_mod_round_to_zero"),
- asList(receiver, arguments.get(0))));
+ list(receiver, arguments.get(0))));
addOperation(methods, Operator.EQUALS.getMethodName(), "==");
addOperation(methods, Operator.NOT_EQUALS.getMethodName(), "!=");
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java b/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java
index f30beed..de36ed6 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonBackend.java
@@ -8,6 +8,7 @@ import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
+import com.google.common.collect.ImmutableList;
import org.zwobble.couscous.Backend;
import org.zwobble.couscous.ast.ClassNode;
@@ -16,9 +17,10 @@ import com.google.common.io.Resources;
import static java.util.Arrays.asList;
import static org.zwobble.couscous.backends.python.PythonCodeGenerator.generateCode;
import static org.zwobble.couscous.backends.python.PythonSerializer.serialize;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class PythonBackend implements Backend {
- private static final List<String> RUNTIME_FILES = asList(
+ private static final List<String> RUNTIME_FILES = ImmutableList.of(
"java.lang.Object",
"java.lang.Boolean",
"java.lang.Integer",
@@ -63,7 +65,7 @@ public class PythonBackend implements Backend {
Path path = destinationPathForModule(name);
Files.createDirectories(path.getParent());
createPythonPackages(path.getParent());
- Files.write(path, asList(contents));
+ Files.write(path, list(contents));
}
private void createPythonPackages(Path packagePath) throws IOException {
diff --git a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
index 104854c..96fc544 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/PythonCodeGenerator.java
@@ -40,6 +40,7 @@ import static org.zwobble.couscous.backends.python.ast.PythonReturnNode.pythonRe
import static org.zwobble.couscous.backends.python.ast.PythonStringLiteralNode.pythonStringLiteral;
import static org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode.pythonVariableReference;
import static org.zwobble.couscous.backends.python.ast.PythonWhileNode.pythonWhile;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class PythonCodeGenerator {
public static PythonModuleNode generateCode(ClassNode classNode) {
@@ -47,7 +48,7 @@ public class PythonCodeGenerator {
PythonImportNode internalsImport = pythonImport(
importPathToRoot(classNode),
- asList(pythonImportAlias("_couscous")));
+ list(pythonImportAlias("_couscous")));
PythonFunctionDefinitionNode constructor =
generateConstructor(classNode.getConstructor());
@@ -58,7 +59,7 @@ public class PythonCodeGenerator {
PythonClassNode pythonClass = pythonClass(
classNode.getSimpleName(),
- ImmutableList.copyOf(Iterables.concat(asList(constructor), pythonMethods)));
+ ImmutableList.copyOf(Iterables.concat(list(constructor), pythonMethods)));
return pythonModule(ImmutableList.copyOf(Iterators.concat(
singletonIterator(pythonClass),
@@ -70,7 +71,7 @@ public class PythonCodeGenerator {
Set<TypeName> classes = findReferencedClasses(classNode);
return classes.stream()
.filter(name -> !name.equals(InternalCouscousValue.REF))
- .map(name -> pythonImport(importPathToRoot(classNode) + name.getQualifiedName(), asList(pythonImportAlias(name.getSimpleName()))));
+ .map(name -> pythonImport(importPathToRoot(classNode) + name.getQualifiedName(), list(pythonImportAlias(name.getSimpleName()))));
}
private static String importPathToRoot(ClassNode classNode) {
@@ -131,7 +132,7 @@ public class PythonCodeGenerator {
private static PythonFunctionDefinitionNode generateConstructor(ConstructorNode constructor) {
Iterable<String> explicitArgumentNames = transform(constructor.getArguments(), argument -> argument.getName());
- Iterable<String> argumentNames = Iterables.concat(asList("self"), explicitArgumentNames);
+ Iterable<String> argumentNames = Iterables.concat(list("self"), explicitArgumentNames);
List<PythonStatementNode> pythonBody = constructor.getBody().stream().map(PythonCodeGenerator::generateStatement).collect(Collectors.toList());
return pythonFunctionDefinition("__init__", ImmutableList.copyOf(argumentNames), new PythonBlock(pythonBody));
}
@@ -139,7 +140,7 @@ public class PythonCodeGenerator {
private static PythonFunctionDefinitionNode generateFunction(MethodNode method) {
Iterable<String> explicitArgumentNames = transform(method.getArguments(), argument -> argument.getName());
Iterable<String> argumentNames = Iterables.concat(
- method.isStatic() ? Collections.<String>emptyList() : asList("self"),
+ method.isStatic() ? Collections.<String>emptyList() : list("self"),
explicitArgumentNames);
List<PythonStatementNode> pythonBody = generateStatements(method.getBody());
return pythonFunctionDefinition(toName(method.signature()), ImmutableList.copyOf(argumentNames), new PythonBlock(pythonBody));
@@ -270,7 +271,7 @@ public class PythonCodeGenerator {
PythonExpressionNode value = generateExpression(typeCoercion.getExpression());
if (isIntegerBox(typeCoercion)) {
// TODO: replace with direct access to java.lang.Integer
- return internalMethod("boxInt", asList(value));
+ return internalMethod("boxInt", list(value));
} else if (isIntegerUnbox(typeCoercion)) {
return pythonAttributeAccess(value, "_value");
} else {
@@ -297,7 +298,7 @@ public class PythonCodeGenerator {
public static String toName(MethodSignature signature) {
return Joiner.on("__").join(Iterables.concat(
- ImmutableList.of(signature.getName()),
+ list(signature.getName()),
transform(signature.getArguments(), argument -> argument.getQualifiedName().replace('.', '_'))));
}
diff --git a/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBlock.java b/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBlock.java
index 8bc7132..b1889a7 100644
--- a/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBlock.java
+++ b/src/main/java/org/zwobble/couscous/backends/python/ast/PythonBlock.java
@@ -6,13 +6,14 @@ import java.util.List;
import com.google.common.collect.ImmutableList;
import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class PythonBlock implements Iterable<PythonStatementNode> {
private final List<PythonStatementNode> statements;
public PythonBlock(List<? extends PythonStatementNode> statements) {
if (statements.isEmpty()) {
- this.statements = asList(PythonPassNode.PASS);
+ this.statements = list(PythonPassNode.PASS);
} else {
this.statements = ImmutableList.copyOf(statements);
}
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java
index d9a55e3..ab78bdb 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionMethodReferenceReader.java
@@ -16,6 +16,7 @@ import static org.zwobble.couscous.ast.StaticMethodCallNode.staticMethodCall;
import static org.zwobble.couscous.ast.sugar.Lambda.lambda;
import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class JavaExpressionMethodReferenceReader {
private final JavaReader javaReader;
@@ -30,7 +31,7 @@ public class JavaExpressionMethodReferenceReader {
return lambda(
formalArguments,
- ImmutableList.of(returns(JavaExpressionReader.coerceExpression(
+ list(returns(JavaExpressionReader.coerceExpression(
typeOf(functionalInterfaceMethod.getReturnType()),
generateValue(scope, expression, formalArguments)))));
}
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 0e910a1..67c1e04 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaExpressionReader.java
@@ -18,6 +18,7 @@ import static org.zwobble.couscous.ast.TypeCoercionNode.typeCoercion;
import static org.zwobble.couscous.frontends.java.JavaOperators.readOperator;
import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf;
import static org.zwobble.couscous.util.ExtraLists.concat;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class JavaExpressionReader {
private final Scope scope;
@@ -261,7 +262,7 @@ public class JavaExpressionReader {
MethodCallNode.methodCall(
readExpression(IntegerValue.REF, expression.getOperand()),
operator.getMethodName(),
- asList(literal(1)),
+ list(literal(1)),
IntegerValue.REF));
}
}
@@ -295,7 +296,7 @@ public class JavaExpressionReader {
return MethodCallNode.methodCall(
left,
operator.getMethodName(),
- asList(right),
+ list(right),
operator.isBoolean() ? BooleanValue.REF : left.getType());
}
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java
index c8b5f4c..ee4fc06 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaLambdaExpressionReader.java
@@ -14,6 +14,7 @@ import static org.zwobble.couscous.ast.ReturnNode.returns;
import static org.zwobble.couscous.ast.sugar.Lambda.lambda;
import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class JavaLambdaExpressionReader {
private final JavaReader reader;
@@ -50,7 +51,7 @@ public class JavaLambdaExpressionReader {
return reader.readStatements(scope, statements, Optional.of(returnType));
} else {
Expression body = (Expression) expression.getBody();
- return asList(returns(reader.readExpression(scope, returnType, body)));
+ return list(returns(reader.readExpression(scope, returnType, body)));
}
}
}
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java
index 9de85cc..e1addd3 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java
@@ -1,5 +1,12 @@
package org.zwobble.couscous.frontends.java;
+import com.google.common.base.Charsets;
+import com.google.common.collect.Iterables;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
@@ -8,17 +15,10 @@ import java.nio.file.Path;
import java.util.Hashtable;
import java.util.List;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
-import org.eclipse.jdt.core.JavaCore;
-import org.eclipse.jdt.core.dom.AST;
-import org.eclipse.jdt.core.dom.ASTParser;
-import org.eclipse.jdt.core.dom.CompilationUnit;
-import com.google.common.base.Charsets;
-
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.transform;
import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class JavaParser {
private final ASTParser parser;
@@ -41,7 +41,7 @@ public class JavaParser {
String[] sourcePathArguments = Iterables.toArray(
concat(
- ImmutableList.of("/usr/lib/jvm/java-8-openjdk-amd64/jre/src.zip"),
+ list("/usr/lib/jvm/java-8-openjdk-amd64/jre/src.zip"),
transform(sourcePaths, Object::toString)),
String.class);
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 04aa3b8..6c1f503 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java
@@ -4,8 +4,6 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.eclipse.jdt.core.dom.*;
import org.zwobble.couscous.ast.*;
-import org.zwobble.couscous.ast.VariableDeclaration;
-import org.zwobble.couscous.ast.identifiers.Identifier;
import org.zwobble.couscous.ast.sugar.Lambda;
import org.zwobble.couscous.util.ExtraLists;
@@ -19,7 +17,6 @@ import java.util.function.Function;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.transform;
-import static com.google.common.collect.Maps.immutableEntry;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.zwobble.couscous.ast.AnnotationNode.annotation;
@@ -27,13 +24,11 @@ import static org.zwobble.couscous.ast.AssignmentNode.assignStatement;
import static org.zwobble.couscous.ast.ConstructorNode.constructor;
import static org.zwobble.couscous.ast.FieldAccessNode.fieldAccess;
import static org.zwobble.couscous.ast.FieldDeclarationNode.field;
-import static org.zwobble.couscous.ast.FormalArgumentNode.formalArg;
import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables;
import static org.zwobble.couscous.frontends.java.JavaTypes.*;
import static org.zwobble.couscous.util.ExtraLists.*;
-import static org.zwobble.couscous.util.ExtraMaps.toMap;
public class JavaReader {
public static List<ClassNode> readClassFromFile(List<Path> sourcePaths, Path sourcePath) throws IOException {
@@ -99,7 +94,7 @@ public class JavaReader {
name,
superTypesAndSelf(functionalInterfaceMethod.getDeclaringClass()),
emptyList(),
- ImmutableList.of(method));
+ list(method));
classes.add(closure.getClassNode());
return closure;
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java
index 6a12a72..41809b7 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaStatementReader.java
@@ -35,16 +35,16 @@ class JavaStatementReader {
return readBlock((Block)statement);
case ASTNode.RETURN_STATEMENT:
- return asList(readReturnStatement((ReturnStatement)statement));
+ return list(readReturnStatement((ReturnStatement)statement));
case ASTNode.EXPRESSION_STATEMENT:
- return asList(readExpressionStatement((ExpressionStatement)statement));
+ return list(readExpressionStatement((ExpressionStatement)statement));
case ASTNode.IF_STATEMENT:
- return asList(readIfStatement((IfStatement)statement));
+ return list(readIfStatement((IfStatement)statement));
case ASTNode.WHILE_STATEMENT:
- return asList(readWhileStatement((WhileStatement)statement));
+ return list(readWhileStatement((WhileStatement)statement));
case ASTNode.FOR_STATEMENT:
return readForStatement((ForStatement)statement);
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java
index 9bf6a51..bc3ef80 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java
@@ -6,6 +6,7 @@ import org.zwobble.couscous.values.ObjectValues;
import org.zwobble.couscous.values.StringValue;
import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class BoxedIntegerInterpreterValue {
public static ObjectInterpreterValue of(int value) {
@@ -21,19 +22,19 @@ public class BoxedIntegerInterpreterValue {
public static final ConcreteType TYPE = ConcreteType.classBuilder("java.lang.Integer")
.field("value", IntegerValue.REF)
- .staticMethod("parseInt", asList(StringValue.REF),
+ .staticMethod("parseInt", list(StringValue.REF),
(environment, arguments) -> {
StringInterpreterValue value = (StringInterpreterValue)arguments.get(0);
return new IntegerInterpreterValue(Integer.parseInt(value.getValue()));
})
- .staticMethod("valueOf", asList(IntegerValue.REF),
+ .staticMethod("valueOf", list(IntegerValue.REF),
(environment, arguments) -> {
IntegerInterpreterValue value = (IntegerInterpreterValue)arguments.get(0);
return of(value);
})
- .method(Operator.EQUALS.getMethodName(), asList(ObjectValues.OBJECT),
+ .method(Operator.EQUALS.getMethodName(), list(ObjectValues.OBJECT),
(environment, arguments) -> {
InterpreterValue right = arguments.getPositionalArguments().get(0);
if (right.getType().getName().equals(ObjectValues.BOXED_INT)) {
@@ -45,7 +46,7 @@ public class BoxedIntegerInterpreterValue {
}
})
- .method("toString", asList(),
+ .method("toString", list(),
(environment, arguments) -> {
IntegerInterpreterValue value = (IntegerInterpreterValue)arguments.getReceiver().getField("value");
return new StringInterpreterValue(Integer.toString(value.getValue()));
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java b/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java
index 9c688ab..b3f9eb1 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/ConcreteType.java
@@ -1,15 +1,19 @@
package org.zwobble.couscous.interpreter.values;
+import com.google.common.collect.ImmutableMap;
+import org.zwobble.couscous.ast.ClassNode;
+import org.zwobble.couscous.ast.FormalArgumentNode;
+import org.zwobble.couscous.ast.MethodSignature;
+import org.zwobble.couscous.ast.TypeName;
+import org.zwobble.couscous.interpreter.*;
+import org.zwobble.couscous.util.Casts;
+
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
-import org.zwobble.couscous.ast.*;
-import org.zwobble.couscous.interpreter.*;
-import org.zwobble.couscous.util.Casts;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
import static java.util.stream.Collectors.toMap;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class ConcreteType {
@@ -27,7 +31,7 @@ public class ConcreteType {
public static class Builder<T> {
private final ImmutableMap.Builder<String, FieldValue> fields = ImmutableMap.builder();
- private MethodValue constructor = new MethodValue(ImmutableList.of(), (environment, arguments) -> InterpreterValues.UNIT);
+ private MethodValue constructor = new MethodValue(list(), (environment, arguments) -> InterpreterValues.UNIT);
private final ImmutableMap.Builder<MethodSignature, MethodValue> methods = ImmutableMap.builder();
private final ImmutableMap.Builder<MethodSignature, StaticMethodValue> staticMethods = ImmutableMap.builder();
private final Class<T> interpreterValueType;
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java
index 3e80a21..4349d5d 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/IntegerInterpreterValue.java
@@ -11,20 +11,21 @@ import org.zwobble.couscous.values.PrimitiveValue;
import org.zwobble.couscous.values.PrimitiveValues;
import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraLists.list;
public final class IntegerInterpreterValue implements InterpreterValue {
public static final ConcreteType TYPE = ConcreteType.builder(IntegerInterpreterValue.class, IntegerValue.REF)
- .method(Operator.ADD.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.ADD.getMethodName(), list(IntegerValue.REF),
infixReturningInteger((left, right) -> left + right))
- .method(Operator.SUBTRACT.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.SUBTRACT.getMethodName(), list(IntegerValue.REF),
infixReturningInteger((left, right) -> left - right))
- .method(Operator.MULTIPLY.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.MULTIPLY.getMethodName(), list(IntegerValue.REF),
infixReturningInteger((left, right) -> left * right))
- .method(Operator.DIVIDE.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.DIVIDE.getMethodName(), list(IntegerValue.REF),
infixReturningInteger((left, right) -> left / right))
- .method(Operator.MOD.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.MOD.getMethodName(), list(IntegerValue.REF),
infixReturningInteger((left, right) -> left % right))
- .method(Operator.EQUALS.getMethodName(), asList(ObjectValues.OBJECT),
+ .method(Operator.EQUALS.getMethodName(), list(ObjectValues.OBJECT),
(environment, arguments) -> {
InterpreterValue right = arguments.getPositionalArguments().get(0);
if (right instanceof IntegerInterpreterValue) {
@@ -33,20 +34,20 @@ public final class IntegerInterpreterValue implements InterpreterValue {
return new BooleanInterpreterValue(false);
}
})
- .method(Operator.EQUALS.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.EQUALS.getMethodName(), list(IntegerValue.REF),
(environment, arguments) -> {
IntegerInterpreterValue right = (IntegerInterpreterValue)arguments.getPositionalArguments().get(0);
return integerEquals(arguments, right);
})
- .method(Operator.NOT_EQUALS.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.NOT_EQUALS.getMethodName(), list(IntegerValue.REF),
infixReturningBoolean((left, right) -> !left.equals(right)))
- .method(Operator.GREATER_THAN.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.GREATER_THAN.getMethodName(), list(IntegerValue.REF),
infixReturningBoolean((left, right) -> left > right))
- .method(Operator.GREATER_THAN_OR_EQUAL.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.GREATER_THAN_OR_EQUAL.getMethodName(), list(IntegerValue.REF),
infixReturningBoolean((left, right) -> left >= right))
- .method(Operator.LESS_THAN.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.LESS_THAN.getMethodName(), list(IntegerValue.REF),
infixReturningBoolean((left, right) -> left < right))
- .method(Operator.LESS_THAN_OR_EQUAL.getMethodName(), asList(IntegerValue.REF),
+ .method(Operator.LESS_THAN_OR_EQUAL.getMethodName(), list(IntegerValue.REF),
infixReturningBoolean((left, right) -> left <= right))
.build();
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/InternalCouscousInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/InternalCouscousInterpreterValue.java
index 7231d0b..023dec7 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/InternalCouscousInterpreterValue.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/InternalCouscousInterpreterValue.java
@@ -5,10 +5,11 @@ import org.zwobble.couscous.values.IntegerValue;
import org.zwobble.couscous.values.ObjectValues;
import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class InternalCouscousInterpreterValue {
public static final ConcreteType TYPE = ConcreteType.classBuilder("_couscous")
- .staticMethod("same", asList(ObjectValues.OBJECT, ObjectValues.OBJECT),
+ .staticMethod("same", list(ObjectValues.OBJECT, ObjectValues.OBJECT),
(environment, arguments) ->
new BooleanInterpreterValue(arguments.get(0) == arguments.get(1)))
diff --git a/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java b/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java
index 42ee713..bbc9f6c 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/values/StringInterpreterValue.java
@@ -7,9 +7,10 @@ import org.zwobble.couscous.values.PrimitiveValue;
import org.zwobble.couscous.values.PrimitiveValues;
import org.zwobble.couscous.values.StringValue;
import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraLists.list;
public final class StringInterpreterValue implements InterpreterValue {
- public static final ConcreteType TYPE = ConcreteType.builder(StringInterpreterValue.class, StringValue.REF).method("length", asList(), (environment, arguments) -> new IntegerInterpreterValue(arguments.getReceiver().value.length())).method("substring", asList(IntegerValue.REF, IntegerValue.REF), (environment, arguments) -> {
+ public static final ConcreteType TYPE = ConcreteType.builder(StringInterpreterValue.class, StringValue.REF).method("length", list(), (environment, arguments) -> new IntegerInterpreterValue(arguments.getReceiver().value.length())).method("substring", list(IntegerValue.REF, IntegerValue.REF), (environment, arguments) -> {
IntegerInterpreterValue startIndex = (IntegerInterpreterValue)arguments.get(0);
IntegerInterpreterValue endIndex = (IntegerInterpreterValue)arguments.get(1);
return new StringInterpreterValue(arguments.getReceiver().value.substring(startIndex.getValue(), endIndex.getValue()));
diff --git a/src/main/java/org/zwobble/couscous/util/ExtraLists.java b/src/main/java/org/zwobble/couscous/util/ExtraLists.java
index e0c183a..dda2479 100644
--- a/src/main/java/org/zwobble/couscous/util/ExtraLists.java
+++ b/src/main/java/org/zwobble/couscous/util/ExtraLists.java
@@ -1,5 +1,7 @@
package org.zwobble.couscous.util;
+import com.google.common.collect.ImmutableList;
+
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -8,6 +10,18 @@ import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class ExtraLists {
+ public static <T> List<T> list() {
+ return ImmutableList.of();
+ }
+
+ public static <T> List<T> list(T value1) {
+ return ImmutableList.of(value1);
+ }
+
+ public static <T> List<T> list(T value1, T value2) {
+ return ImmutableList.of(value1, value2);
+ }
+
public static <T> List<T> cons(T value, List<T> list) {
return Stream.concat(Stream.of(value), list.stream()).collect(Collectors.toList());
}
diff --git a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java
index 9d6bb9f..3ca5a1a 100644
--- a/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/BackendEvalTests.java
@@ -30,6 +30,7 @@ import static org.zwobble.couscous.ast.TernaryConditionalNode.ternaryConditional
import static org.zwobble.couscous.ast.VariableDeclaration.var;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.tests.TestIds.ANY_ID;
+import static org.zwobble.couscous.util.ExtraLists.list;
import static org.zwobble.couscous.values.PrimitiveValues.value;
public abstract class BackendEvalTests {
@@ -129,7 +130,7 @@ public abstract class BackendEvalTests {
PrimitiveValue result = evalExpression(methodCall(
literal("hello"),
"length",
- asList(),
+ list(),
IntegerValue.REF));
assertEquals(value(5), result);
}
@@ -139,7 +140,7 @@ public abstract class BackendEvalTests {
PrimitiveValue result = evalExpression(methodCall(
literal("hello"),
"substring",
- asList(literal(1), literal(4)),
+ list(literal(1), literal(4)),
StringValue.REF));
assertEquals(value("ell"), result);
}
@@ -149,7 +150,7 @@ public abstract class BackendEvalTests {
PrimitiveValue result = evalExpression(staticMethodCall(
"java.lang.Integer",
"parseInt",
- asList(literal("42")),
+ list(literal("42")),
IntegerValue.REF));
assertEquals(value(42), result);
}
@@ -161,11 +162,11 @@ public abstract class BackendEvalTests {
.statement(returns(staticMethodCall(
"java.lang.Integer",
"parseInt",
- asList(literal("42")),
+ list(literal("42")),
IntegerValue.REF))))
.build();
- PrimitiveValue result = evalExpression(asList(classNode),
- staticMethodCall("com.example.Example", "main", asList(), IntegerValue.REF));
+ PrimitiveValue result = evalExpression(list(classNode),
+ staticMethodCall("com.example.Example", "main", list(), IntegerValue.REF));
assertEquals(value(42), result);
}
@@ -175,8 +176,8 @@ public abstract class BackendEvalTests {
.method("main", method -> method
.statement(returns(literal(42))))
.build();
- PrimitiveValue result = evalExpression(asList(classNode),
- methodCall(constructorCall(classNode.getName(), asList()), "main", asList(), IntegerValue.REF));
+ PrimitiveValue result = evalExpression(list(classNode),
+ methodCall(constructorCall(classNode.getName(), list()), "main", list(), IntegerValue.REF));
assertEquals(value(42), result);
}
@@ -190,11 +191,11 @@ public abstract class BackendEvalTests {
.build();
PrimitiveValue result = evalExpression(
- asList(classNode),
+ list(classNode),
methodCall(
- constructorCall(classNode.getName(), asList()),
+ constructorCall(classNode.getName(), list()),
"main",
- asList(literal(42)),
+ list(literal(42)),
IntegerValue.REF));
assertEquals(value(42), result);
@@ -216,18 +217,18 @@ public abstract class BackendEvalTests {
.build();
PrimitiveValue result = evalExpression(
- asList(classNode),
+ list(classNode),
methodCall(
- constructorCall(classNode.getName(), asList(literal(42))),
+ constructorCall(classNode.getName(), list(literal(42))),
"main",
- asList(),
+ list(),
IntegerValue.REF));
assertEquals(value(42), result);
}
private PrimitiveValue evalExpression(ExpressionNode expression) {
- return evalExpression(ImmutableList.of(), expression);
+ return evalExpression(list(), expression);
}
protected abstract PrimitiveValue evalExpression(
diff --git a/src/test/java/org/zwobble/couscous/tests/BackendMethodTests.java b/src/test/java/org/zwobble/couscous/tests/BackendMethodTests.java
index a5a8d2c..1f34127 100644
--- a/src/test/java/org/zwobble/couscous/tests/BackendMethodTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/BackendMethodTests.java
@@ -28,6 +28,7 @@ import static org.zwobble.couscous.ast.VariableDeclaration.var;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.ast.WhileNode.whileLoop;
import static org.zwobble.couscous.tests.TestIds.ANY_ID;
+import static org.zwobble.couscous.util.ExtraLists.list;
import static org.zwobble.couscous.values.PrimitiveValues.value;
public abstract class BackendMethodTests {
@@ -97,8 +98,8 @@ public abstract class BackendMethodTests {
MethodNode.Builder method = staticMethod("hello")
.statement(ifStatement(
literal(true),
- asList(returns(literal("[true]"))),
- asList(returns(literal("[false]")))));
+ list(returns(literal("[true]"))),
+ list(returns(literal("[false]")))));
assertEquals(value("[true]"), runMethod(method));
}
@@ -107,8 +108,8 @@ public abstract class BackendMethodTests {
MethodNode.Builder method = staticMethod("hello")
.statement(ifStatement(
literal(false),
- asList(returns(literal("[true]"))),
- asList(returns(literal("[false]")))));
+ list(returns(literal("[true]"))),
+ list(returns(literal("[false]")))));
assertEquals(value("[false]"), runMethod(method));
}
@@ -121,7 +122,7 @@ public abstract class BackendMethodTests {
.statement(y)
.statement(whileLoop(
notEqual(reference(x), reference(y)),
- asList(assignStatement(x, integerAdd(reference(x), literal(1))))))
+ list(assignStatement(x, integerAdd(reference(x), literal(1))))))
.statement(returns(reference(x)));
assertEquals(value(2), runMethod(method));
}
@@ -135,7 +136,7 @@ public abstract class BackendMethodTests {
.statement(y)
.statement(whileLoop(
notEqual(reference(x), reference(y)),
- asList(
+ list(
assignStatement(x, integerAdd(reference(x), literal(1))),
returns(reference(x)))))
.statement(returns(reference(x)));
@@ -147,7 +148,7 @@ public abstract class BackendMethodTests {
String className = "com.example.Program";
ClassNode classNode = ClassNode.builder(className).method(method).build();
MethodRunner runner = buildMethodRunner();
- return runner.runMethod(asList(classNode), classNode.getName(), method.getName(), asList(arguments));
+ return runner.runMethod(list(classNode), classNode.getName(), method.getName(), asList(arguments));
}
protected abstract MethodRunner buildMethodRunner();
diff --git a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java
index 710db7d..ea12bc4 100644
--- a/src/test/java/org/zwobble/couscous/tests/CompilerTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/CompilerTests.java
@@ -15,6 +15,7 @@ import java.util.List;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
+import static org.zwobble.couscous.util.ExtraLists.list;
import static org.zwobble.couscous.values.PrimitiveValues.value;
public abstract class CompilerTests {
@@ -52,7 +53,7 @@ public abstract class CompilerTests {
"recursive-factorial",
TypeName.of("com.example.RecursiveFactorial"),
"factorial",
- asList(value(6))));
+ list(value(6))));
}
@Test
@@ -63,7 +64,7 @@ public abstract class CompilerTests {
"while-factorial",
TypeName.of("com.example.WhileFactorial"),
"factorial",
- asList(value(6))));
+ list(value(6))));
}
@Test
@@ -74,7 +75,7 @@ public abstract class CompilerTests {
"for-factorial",
TypeName.of("com.example.ForFactorial"),
"factorial",
- asList(value(6))));
+ list(value(6))));
}
@Test
@@ -85,7 +86,7 @@ public abstract class CompilerTests {
"anonymous-class",
TypeName.of("com.example.AnonymousClass"),
"value",
- asList()));
+ list()));
}
@Test
@@ -96,7 +97,7 @@ public abstract class CompilerTests {
"anonymous-class-capture",
TypeName.of("com.example.AnonymousClass"),
"value",
- asList(value(42))));
+ list(value(42))));
}
@Test
@@ -107,7 +108,7 @@ public abstract class CompilerTests {
"lambda",
TypeName.of("com.example.Lambda"),
"value",
- asList()));
+ list()));
}
@Test
@@ -118,7 +119,7 @@ public abstract class CompilerTests {
"lambda-capture",
TypeName.of("com.example.Lambda"),
"value",
- asList(value(2))));
+ list(value(2))));
}
@Test
@@ -129,7 +130,7 @@ public abstract class CompilerTests {
"lambda-this-capture",
TypeName.of("com.example.Lambda"),
"value",
- asList(value(2))));
+ list(value(2))));
}
@Test
@@ -140,7 +141,7 @@ public abstract class CompilerTests {
"static-method-overloads",
TypeName.of("com.example.StaticMethodOverloads"),
"value",
- asList()));
+ list()));
}
private PrimitiveValue execTestProgram(
@@ -182,12 +183,12 @@ public abstract class CompilerTests {
Path directoryPath = Files.createTempDirectory(null);
try {
Files.createDirectories(directoryPath.resolve("com/example"));
- Files.write(directoryPath.resolve("com/example/Example.java"), asList(javaClass));
+ Files.write(directoryPath.resolve("com/example/Example.java"), list(javaClass));
return execProgram(
directoryPath,
TypeName.of("com.example.Example"),
"main",
- asList());
+ list());
} finally {
deleteRecursively(directoryPath.toFile());
}
diff --git a/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java b/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java
index 8008261..59cc6d6 100644
--- a/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/JavaToInterpreterTests.java
@@ -16,6 +16,7 @@ import java.nio.file.Path;
import java.util.List;
import static com.google.common.collect.Iterables.transform;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class JavaToInterpreterTests extends CompilerTests {
@Override
@@ -26,7 +27,7 @@ public class JavaToInterpreterTests extends CompilerTests {
List<PrimitiveValue> arguments
) throws IOException, InterruptedException {
JavaFrontend frontend = new JavaFrontend();
- List<ClassNode> classNodes = frontend.readSourceDirectory(ImmutableList.of(directory), directory);
+ List<ClassNode> classNodes = frontend.readSourceDirectory(list(directory), directory);
Project project = JavaProject.of(classNodes);
Interpreter interpreter = new Interpreter(project);
diff --git a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java
index d2d62b1..a0d1c7e 100644
--- a/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/JavaToPythonTests.java
@@ -16,6 +16,7 @@ import java.nio.file.Path;
import java.util.List;
import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class JavaToPythonTests extends CompilerTests {
protected PrimitiveValue execProgram(
@@ -29,7 +30,7 @@ public class JavaToPythonTests extends CompilerTests {
CouscousCompiler compiler = new CouscousCompiler(
new JavaFrontend(),
new PythonBackend(directoryPath, "couscous"));
- compiler.compileDirectory(ImmutableList.of(directory), directory);
+ compiler.compileDirectory(list(directory), directory);
return PythonMethodRunner.runFunction(directoryPath, type, methodName, arguments);
} finally {
deleteRecursively(directoryPath.toFile());
diff --git a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java
index 4366ac4..e74ac78 100644
--- a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.zwobble.couscous.util.ExtraArrays.stream;
import static org.zwobble.couscous.util.ExtraLists.eagerFilter;
+import static org.zwobble.couscous.util.ExtraLists.list;
@RunWith(Parameterized.class)
public class ValueObjectTests {
@@ -130,7 +131,7 @@ public class ValueObjectTests {
} else if (type.equals(boolean.class)) {
return true;
} else if (type.equals(List.class)) {
- return asList("[list 1]");
+ return list("[list 1]");
} else if (type.equals(Set.class)) {
return Collections.singleton("[set 1]");
} else if (type.equals(Identifier.class)) {
@@ -152,7 +153,7 @@ public class ValueObjectTests {
} else if (type.equals(boolean.class)) {
return false;
} else if (type.equals(List.class)) {
- return asList("[list 2]");
+ return list("[list 2]");
} else if (type.equals(Set.class)) {
return Collections.singleton("[set 2]");
} else if (type.equals(Identifier.class)) {
@@ -174,7 +175,7 @@ public class ValueObjectTests {
} else if (type.equals(boolean.class)) {
return true;
} else if (type.equals(List.class)) {
- return asList("[list]");
+ return list("[list]");
} else if (type.equals(Set.class)) {
return Collections.singleton("[set]");
} else if (type.equals(Identifier.class)) {
diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java
index c5c7aab..61f5c38 100644
--- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonCompilerEvalTests.java
@@ -10,6 +10,7 @@ import org.zwobble.couscous.values.PrimitiveValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class PythonCompilerEvalTests extends BackendEvalTests {
@Override
@@ -23,9 +24,9 @@ public class PythonCompilerEvalTests extends BackendEvalTests {
return runner.runMethod(
ImmutableList.copyOf(Iterables.concat(
classes,
- asList(programNode))),
+ list(programNode))),
programNode.getName(),
"run",
- ImmutableList.of());
+ list());
}
} \ No newline at end of file
diff --git a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java
index 9411d3e..59ab22c 100644
--- a/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/backends/python/PythonSerializerTests.java
@@ -26,6 +26,7 @@ import static org.zwobble.couscous.backends.python.ast.PythonReturnNode.pythonRe
import static org.zwobble.couscous.backends.python.ast.PythonStringLiteralNode.pythonStringLiteral;
import static org.zwobble.couscous.backends.python.ast.PythonVariableReferenceNode.pythonVariableReference;
import static org.zwobble.couscous.backends.python.ast.PythonWhileNode.pythonWhile;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class PythonSerializerTests {
@Test
@@ -69,12 +70,12 @@ public class PythonSerializerTests {
@Test
public void callIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(f)(x, y)", serialize(pythonCall(pythonVariableReference("f"), asList(pythonVariableReference("x"), pythonVariableReference("y")))));
+ assertEquals("(f)(x, y)", serialize(pythonCall(pythonVariableReference("f"), list(pythonVariableReference("x"), pythonVariableReference("y")))));
}
@Test
public void getSliceIsSerializedUsingParenthesisedSubExpression() {
- assertEquals("(x)[y:z]", serialize(pythonGetSlice(pythonVariableReference("x"), asList(pythonVariableReference("y"), pythonVariableReference("z")))));
+ assertEquals("(x)[y:z]", serialize(pythonGetSlice(pythonVariableReference("x"), list(pythonVariableReference("y"), pythonVariableReference("z")))));
}
@Test
@@ -148,7 +149,7 @@ public class PythonSerializerTests {
public void importIsSerializedUsingImportKeyword() {
String output = serialize(pythonImport(
"com.example",
- asList(pythonImportAlias("Program"))));
+ list(pythonImportAlias("Program"))));
assertEquals("from com.example import Program\n", output);
}
@@ -156,7 +157,7 @@ public class PythonSerializerTests {
public void importAliasesAreSeparatedByCommas() {
String output = serialize(pythonImport(
"com.example",
- asList(pythonImportAlias("Program"),pythonImportAlias("Runner"))));
+ list(pythonImportAlias("Program"),pythonImportAlias("Runner"))));
assertEquals("from com.example import Program, Runner\n", output);
}
@@ -164,8 +165,8 @@ public class PythonSerializerTests {
public void ifStatementIsSerializedWithIfKeyword() {
String output = serialize(pythonIfStatement(
pythonBooleanLiteral(true),
- asList(pythonReturn(pythonIntegerLiteral(1))),
- asList(pythonReturn(pythonIntegerLiteral(2)))));
+ list(pythonReturn(pythonIntegerLiteral(1))),
+ list(pythonReturn(pythonIntegerLiteral(2)))));
assertEquals("if True:\n return 1\nelse:\n return 2\n", output);
}
@@ -173,13 +174,13 @@ public class PythonSerializerTests {
public void whileIsSerializedWithWhileKeyword() {
String output = serialize(pythonWhile(
pythonBooleanLiteral(true),
- asList(pythonReturn(pythonIntegerLiteral(1)))));
+ list(pythonReturn(pythonIntegerLiteral(1)))));
assertEquals("while True:\n return 1\n", output);
}
@Test
public void moduleIsSerializedStatements() {
- PythonModuleNode classNode = pythonModule(asList(PASS));
+ PythonModuleNode classNode = pythonModule(list(PASS));
String output = serialize(classNode);
assertEquals("pass\n", output);
}
diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java
index 7cf349d..84e9508 100644
--- a/src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/FreeVariablesTests.java
@@ -17,20 +17,21 @@ import static org.zwobble.couscous.ast.LocalVariableDeclarationNode.localVariabl
import static org.zwobble.couscous.ast.VariableDeclaration.var;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class FreeVariablesTests {
@Test
public void referenceIsFreeIfThereIsNoAssociatedDeclaration() {
VariableDeclaration declaration = var(TestIds.ANY_ID, "[name]", StringValue.REF);
- List<ReferenceNode> freeVariables = findFreeVariables(asList(reference(declaration)));
- assertEquals(asList(reference(declaration)), freeVariables);
+ List<ReferenceNode> freeVariables = findFreeVariables(list(reference(declaration)));
+ assertEquals(list(reference(declaration)), freeVariables);
}
@Test
public void referenceIsNotFreeIfIsDeclaredAsArgument() {
VariableDeclaration declaration = var(TestIds.ANY_ID, "[name]", StringValue.REF);
List<ReferenceNode> freeVariables = findFreeVariables(
- asList(formalArg(declaration), reference(declaration)));
+ list(formalArg(declaration), reference(declaration)));
assertEquals(emptyList(), freeVariables);
}
@@ -38,7 +39,7 @@ public class FreeVariablesTests {
public void referenceIsNotFreeIfIsReferenceToLocalDefinedInBody() {
VariableDeclaration declaration = var(TestIds.ANY_ID, "[name]", StringValue.REF);
List<ReferenceNode> freeVariables = findFreeVariables(
- asList(
+ list(
localVariableDeclaration(declaration, literal("[value]")),
reference(declaration)));
assertEquals(emptyList(), freeVariables);
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 5548b2f..46e010c 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
@@ -37,6 +37,7 @@ import static org.zwobble.couscous.ast.TypeCoercionNode.typeCoercion;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.ast.WhileNode.whileLoop;
import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class JavaReaderTests {
@Test
@@ -61,7 +62,7 @@ public class JavaReaderTests {
"private String name;");
assertEquals(
- asList(field("name", TypeName.of("java.lang.String"))),
+ list(field("name", TypeName.of("java.lang.String"))),
classNode.getFields());
}
@@ -94,14 +95,14 @@ public class JavaReaderTests {
@Test
public void canReadInstanceMethodCalls() {
assertEquals(
- methodCall(literal("hello"), "startsWith", asList(literal("h")), BooleanValue.REF),
+ methodCall(literal("hello"), "startsWith", list(literal("h")), BooleanValue.REF),
readBooleanExpression("\"hello\".startsWith(\"h\")"));
List<StatementNode> statements = readStatements("int", "Object x = 1; return x.hashCode();");
LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0);
ReturnNode returnNode = (ReturnNode) statements.get(1);
assertEquals(
- methodCall(reference(declaration), "hashCode", asList(), IntegerValue.REF),
+ methodCall(reference(declaration), "hashCode", list(), IntegerValue.REF),
returnNode.getValue());
}
@@ -117,7 +118,7 @@ public class JavaReaderTests {
methodCall(
ThisReferenceNode.thisReference(TypeName.of("com.example.Example")),
"loop",
- asList(),
+ list(),
StringValue.REF),
returnNode.getValue());
}
@@ -128,7 +129,7 @@ public class JavaReaderTests {
staticMethodCall(
TypeName.of("java.lang.Integer"),
"parseInt",
- asList(literal("42")),
+ list(literal("42")),
IntegerValue.REF),
readIntExpression("Integer.parseInt(\"42\")"));
}
@@ -145,7 +146,7 @@ public class JavaReaderTests {
staticMethodCall(
TypeName.of("com.example.Example"),
"loop",
- asList(),
+ list(),
StringValue.REF),
returnNode.getValue());
}
@@ -153,7 +154,7 @@ public class JavaReaderTests {
@Test
public void canReadConstructorCalls() {
assertEquals(
- constructorCall(TypeName.of("java.lang.String"), asList(literal("_"))),
+ constructorCall(TypeName.of("java.lang.String"), list(literal("_"))),
readExpression("String", "new String(\"_\")"));
}
@@ -274,13 +275,13 @@ public class JavaReaderTests {
assertEquals(
equal(
literal(1),
- unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1))))),
+ unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(1))))),
readBooleanExpression("1 == new Integer(1)"));
assertEquals(
notEqual(
literal(1),
- unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1))))),
+ unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(1))))),
readBooleanExpression("1 != new Integer(1)"));
}
@@ -288,14 +289,14 @@ public class JavaReaderTests {
public void equalityOperatorDoesNotUnboxIfBothOperandsAreBoxed() {
assertEquals(
same(
- typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(1))), ObjectValues.OBJECT),
- typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(2))), ObjectValues.OBJECT)),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, list(literal(1))), ObjectValues.OBJECT),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, list(literal(2))), ObjectValues.OBJECT)),
readBooleanExpression("new Integer(1) == new Integer(2)"));
assertEquals(
not(same(
- typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(1))), ObjectValues.OBJECT),
- typeCoercion(constructorCall(ObjectValues.BOXED_INT, asList(literal(2))), ObjectValues.OBJECT))),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, list(literal(1))), ObjectValues.OBJECT),
+ typeCoercion(constructorCall(ObjectValues.BOXED_INT, list(literal(2))), ObjectValues.OBJECT))),
readBooleanExpression("new Integer(1) != new Integer(2)"));
}
@@ -303,8 +304,8 @@ public class JavaReaderTests {
public void integerOperatorUnboxesWhenBothOperandsAreBoxed() {
assertEquals(
integerAdd(
- unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(1)))),
- unboxInt(constructorCall(TypeName.of("java.lang.Integer"), asList(literal(2))))),
+ unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(1)))),
+ unboxInt(constructorCall(TypeName.of("java.lang.Integer"), list(literal(2))))),
readIntExpression("new Integer(1) + new Integer(2)"));
}
@@ -506,7 +507,7 @@ public class JavaReaderTests {
staticMethodCall(
TypeName.of("java.lang.Integer"),
"parseInt",
- asList(literal("42")),
+ list(literal("42")),
IntegerValue.REF)),
readStatement("Integer.parseInt(\"42\");"));
}
@@ -516,8 +517,8 @@ public class JavaReaderTests {
assertEquals(
ifStatement(
literal(true),
- asList(returns(literal(1))),
- asList(returns(literal(2)))),
+ list(returns(literal(1))),
+ list(returns(literal(2)))),
readStatement("int", "if (true) { return 1; } else { return 2; }"));
}
@@ -526,7 +527,7 @@ public class JavaReaderTests {
assertEquals(
whileLoop(
literal(true),
- asList(returns(literal(1)))),
+ list(returns(literal(1)))),
readStatement("int", "while (true) { return 1; }"));
}
@@ -535,11 +536,11 @@ public class JavaReaderTests {
List<StatementNode> statements = readStatements("int", "for (int x = 0; true; ++x) { return 1; }");
LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0);
assertEquals(
- asList(
+ list(
localVariableDeclaration(declaration.getDeclaration(), literal(0)),
whileLoop(
literal(true),
- asList(
+ list(
returns(literal(1)),
expressionStatement(assign(declaration, integerAdd(reference(declaration), literal(1))))))),
statements);
@@ -555,10 +556,10 @@ public class JavaReaderTests {
ConstructorNode constructor = classNode.getConstructor();
- assertEquals(asList(), constructor.getArguments());
+ assertEquals(list(), constructor.getArguments());
assertEquals(
- asList(assignStatement(
+ list(assignStatement(
fieldAccess(
thisReference(TypeName.of("com.example.Example")),
"name",
@@ -574,7 +575,7 @@ public class JavaReaderTests {
MethodNode method = classNode.getMethods().get(0);
assertEquals(
- asList(annotation(TypeName.of("java.lang.Deprecated"))),
+ list(annotation(TypeName.of("java.lang.Deprecated"))),
method.getAnnotations());
}
@@ -585,7 +586,7 @@ public class JavaReaderTests {
MethodNode method = classNode.getMethods().get(0);
assertEquals(
- asList(returns(boxInt(literal(1)))),
+ list(returns(boxInt(literal(1)))),
method.getBody());
}
@@ -645,9 +646,9 @@ public class JavaReaderTests {
Path sourcePath = directoryPath.resolve("com/example/Example.java");
try {
Files.createDirectories(directoryPath.resolve("com/example"));
- Files.write(sourcePath, asList(javaClass));
+ Files.write(sourcePath, list(javaClass));
- return JavaReader.readClassFromFile(ImmutableList.of(directoryPath), sourcePath);
+ return JavaReader.readClassFromFile(list(directoryPath), sourcePath);
} finally {
deleteRecursively(directoryPath.toFile());
}
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 f3087ed..f9d57b8 100644
--- a/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/interpreter/EvaluatorTests.java
@@ -34,6 +34,7 @@ import static org.zwobble.couscous.interpreter.Evaluator.eval;
import static org.zwobble.couscous.interpreter.values.InterpreterValues.value;
import static org.zwobble.couscous.tests.TestIds.ANY_ID;
import static org.zwobble.couscous.tests.util.ExtraAsserts.assertThrows;
+import static org.zwobble.couscous.util.ExtraLists.list;
public class EvaluatorTests extends BackendEvalTests {
@Test
@@ -62,9 +63,9 @@ public class EvaluatorTests extends BackendEvalTests {
methodCall(
literal("hello"),
"size",
- asList(),
+ list(),
IntegerValue.REF)));
- assertEquals(new MethodSignature("size", asList()), exception.getSignature());
+ assertEquals(new MethodSignature("size", list()), exception.getSignature());
}
@Test
@@ -74,9 +75,9 @@ public class EvaluatorTests extends BackendEvalTests {
methodCall(
literal("hello"),
"substring",
- asList(literal(1)),
+ list(literal(1)),
StringValue.REF)));
- assertEquals(new MethodSignature("substring", asList(IntegerValue.REF)), exception.getSignature());
+ assertEquals(new MethodSignature("substring", list(IntegerValue.REF)), exception.getSignature());
}
@Test
@@ -86,10 +87,10 @@ public class EvaluatorTests extends BackendEvalTests {
methodCall(
literal("hello"),
"substring",
- asList(literal(0), literal("")),
+ list(literal(0), literal("")),
StringValue.REF)));
assertEquals(
- new MethodSignature("substring", asList(IntegerValue.REF, StringValue.REF)),
+ new MethodSignature("substring", list(IntegerValue.REF, StringValue.REF)),
exception.getSignature());
}
@@ -103,10 +104,10 @@ public class EvaluatorTests extends BackendEvalTests {
UnexpectedValueType exception = assertThrows(UnexpectedValueType.class,
() -> evalExpression(
- asList(classNode),
+ list(classNode),
constructorCall(
classNode.getName(),
- asList(literal("")))));
+ list(literal("")))));
assertEquals(new UnexpectedValueType(IntegerValue.REF, StringValue.REF), exception);
}
@@ -118,11 +119,11 @@ public class EvaluatorTests extends BackendEvalTests {
NoSuchField exception = assertThrows(NoSuchField.class,
() -> evalExpression(
- asList(classNode),
+ list(classNode),
fieldAccess(
constructorCall(
classNode.getName(),
- asList()),
+ list()),
"value",
IntegerValue.REF)));
@@ -137,11 +138,11 @@ public class EvaluatorTests extends BackendEvalTests {
UnboundField exception = assertThrows(UnboundField.class,
() -> evalExpression(
- asList(classNode),
+ list(classNode),
fieldAccess(
constructorCall(
classNode.getName(),
- asList()),
+ list()),
"value",
IntegerValue.REF)));
@@ -159,10 +160,10 @@ public class EvaluatorTests extends BackendEvalTests {
NoSuchField exception = assertThrows(NoSuchField.class,
() -> evalExpression(
- asList(classNode),
+ list(classNode),
constructorCall(
classNode.getName(),
- asList())));
+ list())));
assertEquals(new NoSuchField("value"), exception);
}
@@ -179,10 +180,10 @@ public class EvaluatorTests extends BackendEvalTests {
UnexpectedValueType exception = assertThrows(UnexpectedValueType.class,
() -> evalExpression(
- asList(classNode),
+ list(classNode),
constructorCall(
classNode.getName(),
- asList())));
+ list())));
assertEquals(new UnexpectedValueType(IntegerValue.REF, StringValue.REF), exception);
}
diff --git a/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java b/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java
index 8023ee5..d8cd4e0 100644
--- a/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java
+++ b/src/test/java/org/zwobble/couscous/tests/interpreter/InterpreterMethodTests.java
@@ -27,6 +27,7 @@ import static org.zwobble.couscous.ast.VariableDeclaration.var;
import static org.zwobble.couscous.ast.VariableReferenceNode.reference;
import static org.zwobble.couscous.tests.TestIds.ANY_ID;
import static org.zwobble.couscous.tests.util.ExtraAsserts.assertThrows;
+import static org.zwobble.couscous.util.ExtraLists.list;
import static org.zwobble.couscous.values.PrimitiveValues.value;
public class InterpreterMethodTests extends BackendMethodTests {
@@ -37,7 +38,7 @@ public class InterpreterMethodTests extends BackendMethodTests {
NoSuchMethod exception = assertThrows(NoSuchMethod.class,
() -> runMethod(method, value("hello, world!")));
- assertEquals(new MethodSignature("hello", asList(StringValue.REF)), exception.getSignature());
+ assertEquals(new MethodSignature("hello", list(StringValue.REF)), exception.getSignature());
}
@Test