Convert one class to kotlin

This commit is contained in:
Mike Cifelli 2018-03-18 11:39:01 -04:00
parent 182ff9f5e6
commit 5f0cb1aa86
8 changed files with 179 additions and 86 deletions

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ClojureProjectResolveSettings">
<currentScheme>IDE</currentScheme>
</component>
</project>

View File

@ -18,6 +18,17 @@
</value> </value>
</option> </option>
</JavaCodeStyleSettings> </JavaCodeStyleSettings>
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value />
</option>
<option name="ALIGN_IN_COLUMNS_CASE_BRANCH" value="true" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="IMPORT_NESTED_CLASSES" value="true" />
<option name="CONTINUATION_INDENT_FOR_CHAINED_CALLS" value="false" />
<option name="WRAP_EXPRESSION_BODY_FUNCTIONS" value="1" />
</JetCodeStyleSettings>
<XML> <XML>
<option name="XML_SPACE_INSIDE_EMPTY_TAG" value="true" /> <option name="XML_SPACE_INSIDE_EMPTY_TAG" value="true" />
</XML> </XML>
@ -60,5 +71,21 @@
<option name="INDENT_SIZE" value="2" /> <option name="INDENT_SIZE" value="2" />
</indentOptions> </indentOptions>
</codeStyleSettings> </codeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="0" />
<option name="BLANK_LINES_AFTER_CLASS_HEADER" value="1" />
<option name="ALIGN_MULTILINE_PARAMETERS_IN_CALLS" value="true" />
<option name="ALIGN_MULTILINE_EXTENDS_LIST" value="true" />
<option name="CALL_PARAMETERS_WRAP" value="5" />
<option name="METHOD_PARAMETERS_WRAP" value="5" />
<option name="EXTENDS_LIST_WRAP" value="5" />
<option name="METHOD_CALL_CHAIN_WRAP" value="5" />
<option name="ASSIGNMENT_WRAP" value="1" />
<option name="FIELD_ANNOTATION_WRAP" value="1" />
<option name="ENUM_CONSTANTS_WRAP" value="5" />
</codeStyleSettings>
</code_scheme> </code_scheme>
</component> </component>

View File

@ -1,5 +1,5 @@
<component name="ProjectCodeStyleConfiguration"> <component name="ProjectCodeStyleConfiguration">
<state> <state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" /> <option name="PREFERRED_PROJECT_CODE_STYLE" value="Mike" />
</state> </state>
</component> </component>

76
pom.xml
View File

@ -8,10 +8,12 @@
<artifactId>transcendental-lisp</artifactId> <artifactId>transcendental-lisp</artifactId>
<version>1.2.0</version> <version>1.2.0</version>
<build> <properties>
<!--<sourceDirectory>src/main/kotlin</sourceDirectory>--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--<testSourceDirectory>src/test/kotlin</testSourceDirectory>--> <kotlin.version>1.2.30</kotlin.version>
</properties>
<build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
@ -25,10 +27,61 @@
<version>3.0.2</version> <version>3.0.2</version>
</plugin> </plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version> <version>3.7.0</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
@ -79,12 +132,25 @@
</build> </build>
<dependencies> <dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.googlecode.lanterna</groupId> <groupId>com.googlecode.lanterna</groupId>
<artifactId>lanterna</artifactId> <artifactId>lanterna</artifactId>
<version>3.0.0</version> <version>3.0.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -114,8 +180,4 @@
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project> </project>

View File

@ -4,6 +4,7 @@ import error.LispException;
import function.ArgumentValidator; import function.ArgumentValidator;
import function.FunctionNames; import function.FunctionNames;
import function.LispFunction; import function.LispFunction;
import function.builtin.special.LAMBDA;
import function.builtin.special.RECUR.RecurNotInTailPositionException; import function.builtin.special.RECUR.RecurNotInTailPositionException;
import sexpression.BackquoteExpression; import sexpression.BackquoteExpression;
import sexpression.Cons; import sexpression.Cons;
@ -13,8 +14,6 @@ import sexpression.Symbol;
import table.ExecutionContext; import table.ExecutionContext;
import static function.builtin.cons.LIST.makeList; import static function.builtin.cons.LIST.makeList;
import static function.builtin.special.LAMBDA.createFunction;
import static function.builtin.special.LAMBDA.isLambdaExpression;
import static java.text.MessageFormat.format; import static java.text.MessageFormat.format;
import static sexpression.Nil.NIL; import static sexpression.Nil.NIL;
import static sexpression.Symbol.T; import static sexpression.Symbol.T;
@ -60,8 +59,8 @@ public class EVAL extends LispFunction {
private static LispFunction createLambdaFunction(SExpression lambdaExpression) { private static LispFunction createLambdaFunction(SExpression lambdaExpression) {
if (lambdaExpression.isFunction()) if (lambdaExpression.isFunction())
return ((LambdaExpression) lambdaExpression).getFunction(); return ((LambdaExpression) lambdaExpression).getFunction();
else if (isLambdaExpression(lambdaExpression)) else if (LAMBDA.Companion.isLambdaExpression(lambdaExpression))
return createFunction((Cons) lambdaExpression); return LAMBDA.Companion.createFunction((Cons) lambdaExpression);
else else
throw new UndefinedFunctionException(lambdaExpression); throw new UndefinedFunctionException(lambdaExpression);
} }

View File

@ -1,69 +0,0 @@
package function.builtin.special;
import function.ArgumentValidator;
import function.FunctionNames;
import function.LispSpecialFunction;
import function.UserDefinedFunction;
import sexpression.Cons;
import sexpression.LambdaExpression;
import sexpression.SExpression;
import sexpression.Symbol;
import static function.builtin.cons.LIST.makeList;
@FunctionNames({ "LAMBDA", "Λ" })
public class LAMBDA extends LispSpecialFunction {
public static boolean isLambdaExpression(SExpression sexpr) {
if (sexpr.isCons()) {
String first = ((Cons) sexpr).getFirst().toString();
return "LAMBDA".equals(first) || "Λ".equals(first);
}
return false;
}
public static UserDefinedFunction createFunction(Cons lambdaExpression) {
SExpression rest = lambdaExpression.getRest();
ArgumentValidator lambdaValidator = new ArgumentValidator("LAMBDA|create|");
lambdaValidator.setEveryArgumentExpectedType(Cons.class);
lambdaValidator.validate(makeList(rest));
LambdaExpression lambda = new LAMBDA("LAMBDA").call((Cons) rest);
return lambda.getFunction();
}
private ArgumentValidator argumentValidator;
private ArgumentValidator lambdaListValidator;
public LAMBDA(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setFirstArgumentExpectedType(Cons.class);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.lambdaListValidator = new ArgumentValidator(name + "|lambda-list|");
this.lambdaListValidator.setEveryArgumentExpectedType(Symbol.class);
}
@Override
public LambdaExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
SExpression first = argumentList.getFirst();
Cons lambdaList = (Cons) first;
Cons body = (Cons) argumentList.getRest();
lambdaListValidator.validate(lambdaList);
UserDefinedFunction function = new UserDefinedFunction(":LAMBDA", lambdaList, body);
return new LambdaExpression(makeOriginalLambdaExpression(argumentList), function);
}
private Cons makeOriginalLambdaExpression(Cons argumentList) {
return new Cons(new Symbol("LAMBDA"), argumentList);
}
}

View File

@ -0,0 +1,68 @@
package function.builtin.special
import function.ArgumentValidator
import function.FunctionNames
import function.LispSpecialFunction
import function.UserDefinedFunction
import function.builtin.cons.LIST.makeList
import sexpression.Cons
import sexpression.LambdaExpression
import sexpression.SExpression
import sexpression.Symbol
@FunctionNames("LAMBDA", "Λ")
class LAMBDA(name: String) : LispSpecialFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name)
private val lambdaListValidator: ArgumentValidator = ArgumentValidator("$name|lambda-list|")
init {
this.argumentValidator.setFirstArgumentExpectedType(Cons::class.java)
this.argumentValidator.setMinimumNumberOfArguments(1)
this.lambdaListValidator.setEveryArgumentExpectedType(Symbol::class.java)
}
override fun call(argumentList: Cons): LambdaExpression {
argumentValidator.validate(argumentList)
val first = argumentList.first
val lambdaList = first as Cons
val body = argumentList.rest as Cons
lambdaListValidator.validate(lambdaList)
val function = UserDefinedFunction(":LAMBDA", lambdaList, body)
return LambdaExpression(makeOriginalLambdaExpression(argumentList), function)
}
private fun makeOriginalLambdaExpression(argumentList: Cons): Cons {
return Cons(Symbol("LAMBDA"), argumentList)
}
companion object {
fun isLambdaExpression(sexpr: SExpression): Boolean {
if (sexpr.isCons) {
val first = (sexpr as Cons).first.toString()
return "LAMBDA" == first || "Λ" == first
}
return false
}
fun createFunction(lambdaExpression: Cons): UserDefinedFunction {
val rest = lambdaExpression.rest
val lambdaValidator = ArgumentValidator("LAMBDA|create|")
lambdaValidator.setEveryArgumentExpectedType(Cons::class.java)
lambdaValidator.validate(makeList(rest))
val lambda = LAMBDA("LAMBDA").call(rest as Cons)
return lambda.function
}
}
}

View File

@ -47,12 +47,12 @@ public class LAMBDATest extends SymbolAndFunctionCleaner {
public void lambdaExpressionIsLambdaExpression() { public void lambdaExpressionIsLambdaExpression() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL))); Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL)));
assertTrue(LAMBDA.isLambdaExpression(lambdaExpression)); assertTrue(LAMBDA.Companion.isLambdaExpression(lambdaExpression));
} }
@Test @Test
public void somethingElseIsNotLambdaExpression() { public void somethingElseIsNotLambdaExpression() {
assertFalse(LAMBDA.isLambdaExpression(T)); assertFalse(LAMBDA.Companion.isLambdaExpression(T));
} }
@Test @Test
@ -60,7 +60,7 @@ public class LAMBDATest extends SymbolAndFunctionCleaner {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL))); Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL)));
assertSExpressionsMatch(parseString("(:LAMBDA () ())"), assertSExpressionsMatch(parseString("(:LAMBDA () ())"),
LAMBDA.createFunction(lambdaExpression).getLambdaExpression()); LAMBDA.Companion.createFunction(lambdaExpression).getLambdaExpression());
} }
@Test(expected = DottedArgumentListException.class) @Test(expected = DottedArgumentListException.class)
@ -81,14 +81,14 @@ public class LAMBDATest extends SymbolAndFunctionCleaner {
public void createFunctionWithDottedArgumentList() { public void createFunctionWithDottedArgumentList() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, ONE)); Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, ONE));
LAMBDA.createFunction(lambdaExpression); LAMBDA.Companion.createFunction(lambdaExpression);
} }
@Test(expected = BadArgumentTypeException.class) @Test(expected = BadArgumentTypeException.class)
public void createFunctionWithNonList() { public void createFunctionWithNonList() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), ONE); Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), ONE);
LAMBDA.createFunction(lambdaExpression); LAMBDA.Companion.createFunction(lambdaExpression);
} }
@Test(expected = BadArgumentTypeException.class) @Test(expected = BadArgumentTypeException.class)