Convert Setq to kotlin

This commit is contained in:
Mike Cifelli 2018-10-23 19:46:13 -04:00
parent 924357b5cd
commit ac821abde2
18 changed files with 56 additions and 60 deletions

View File

@ -12,7 +12,7 @@ import table.FunctionTable
@FunctionNames("APPLY")
class Apply(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(2)
setTrailingArgumentExpectedType(Cons::class.java)
}

View File

@ -12,9 +12,9 @@ import sexpression.SExpression
internal class BackquoteEvaluator(private val backTick: BackquoteExpression) {
private val listValidator: ArgumentValidator = ArgumentValidator("`|list|")
private val atSignValidator: ArgumentValidator = ArgumentValidator("@|list|")
private val resolvedList: Cons = Cons(Nil, Nil)
private val listValidator = ArgumentValidator("`|list|")
private val atSignValidator = ArgumentValidator("@|list|")
private val resolvedList = Cons(Nil, Nil)
private var leader = resolvedList
private var follower = resolvedList

View File

@ -21,7 +21,7 @@ import table.FunctionTable
@FunctionNames("EVAL")
class Eval(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(1)
}

View File

@ -11,7 +11,7 @@ import sexpression.SExpression
@FunctionNames("EXIT")
class Exit(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setMaximumNumberOfArguments(0)
}

View File

@ -11,7 +11,7 @@ import sexpression.SExpression
@FunctionNames("FUNCALL", "CALL")
class Funcall(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setMinimumNumberOfArguments(1)
}

View File

@ -12,7 +12,7 @@ import sexpression.Symbol
@FunctionNames("FUSE")
class Fuse(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(2)
setFirstArgumentExpectedType(Symbol::class.java)
setTrailingArgumentExpectedType(Atom::class.java)

View File

@ -12,7 +12,7 @@ import java.math.BigInteger.ZERO
@FunctionNames("GENSYM")
class Gensym(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setMaximumNumberOfArguments(0)
}

View File

@ -21,7 +21,7 @@ import java.util.Stack
@FunctionNames("LOAD")
class Load(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(1)
setEveryArgumentExpectedType(LispString::class.java)
}

View File

@ -10,7 +10,7 @@ import sexpression.SExpression
@FunctionNames("PRINT")
class Print(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(1)
}

View File

@ -13,7 +13,7 @@ import table.SymbolTable
@FunctionNames("SET")
class Set(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(2)
setFirstArgumentExpectedType(Symbol::class.java)

View File

@ -14,7 +14,7 @@ import table.FunctionTable
@FunctionNames("SYMBOL-FUNCTION")
class SymbolFunction(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(1)
setEveryArgumentExpectedType(Symbol::class.java)
}

View File

@ -10,7 +10,7 @@ import table.ExecutionContext
@FunctionNames("SYMBOLS")
class Symbols(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(0)
}

View File

@ -13,7 +13,7 @@ import java.util.regex.Pattern
@FunctionNames("GENSYM-EQUAL", "GENSYM-EQUAL?")
class GensymEqual(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(2)
}

View File

@ -12,7 +12,7 @@ import sexpression.Symbol.Companion.T
@FunctionNames("=")
class NumericEqual(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setMinimumNumberOfArguments(1)
setEveryArgumentExpectedType(LispNumber::class.java)
}

View File

@ -13,12 +13,12 @@ import sexpression.Symbol
@FunctionNames("LAMBDA", "Λ")
class Lambda(name: String) : LispSpecialFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply {
private val argumentValidator = ArgumentValidator(name).apply {
setFirstArgumentExpectedType(Cons::class.java)
setMinimumNumberOfArguments(1)
}
private val lambdaListValidator: ArgumentValidator = ArgumentValidator("$name|lambda-list|").apply {
private val lambdaListValidator = ArgumentValidator("$name|lambda-list|").apply {
setEveryArgumentExpectedType(Symbol::class.java)
}
@ -55,9 +55,10 @@ class Lambda(name: String) : LispSpecialFunction() {
fun createFunction(lambdaExpression: Cons): UserDefinedFunction {
val rest = lambdaExpression.rest
val lambdaValidator = ArgumentValidator("LAMBDA|create|")
lambdaValidator.setEveryArgumentExpectedType(Cons::class.java)
lambdaValidator.validate(makeList(rest))
ArgumentValidator("LAMBDA|create|").run {
setEveryArgumentExpectedType(Cons::class.java)
validate(makeList(rest))
}
val lambda = Lambda("LAMBDA").call(rest as Cons)

View File

@ -1,38 +0,0 @@
package function.builtin.special;
import function.ArgumentValidator;
import function.FunctionNames;
import function.LispSpecialFunction;
import sexpression.Cons;
import sexpression.SExpression;
import sexpression.Symbol;
import static function.builtin.Eval.eval;
import static function.builtin.Set.set;
import static function.builtin.cons.LIST.makeList;
@FunctionNames({ "SETQ" })
public class SETQ extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public SETQ(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2);
this.argumentValidator.setFirstArgumentExpectedType(Symbol.class);
}
@Override
public SExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
return set(evaluateValueOnly(argumentList));
}
private Cons evaluateValueOnly(Cons argumentList) {
Cons rest = (Cons) argumentList.getRest();
SExpression value = eval(rest.getFirst());
return new Cons(argumentList.getFirst(), makeList(value));
}
}

View File

@ -0,0 +1,33 @@
package function.builtin.special
import function.ArgumentValidator
import function.FunctionNames
import function.LispSpecialFunction
import function.builtin.Eval.Companion.eval
import function.builtin.Set.Companion.set
import function.builtin.cons.LIST.makeList
import sexpression.Cons
import sexpression.SExpression
import sexpression.Symbol
@FunctionNames("SETQ")
class Setq(name: String) : LispSpecialFunction() {
private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(2)
setFirstArgumentExpectedType(Symbol::class.java)
}
override fun call(argumentList: Cons): SExpression {
argumentValidator.validate(argumentList)
return set(evaluateValueOnly(argumentList))
}
private fun evaluateValueOnly(argumentList: Cons): Cons {
val rest = argumentList.rest as Cons
val value = eval(rest.first)
return Cons(argumentList.first, makeList(value))
}
}

View File

@ -13,7 +13,7 @@ import static org.junit.Assert.assertNull;
import static testutil.TestUtilities.assertSExpressionsMatch;
import static testutil.TestUtilities.evaluateString;
public class SETQTest extends SymbolAndFunctionCleaner {
public class SetqTest extends SymbolAndFunctionCleaner {
@Test
public void setq() {