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") @FunctionNames("APPLY")
class Apply(name: String) : LispFunction() { class Apply(name: String) : LispFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply { private val argumentValidator = ArgumentValidator(name).apply {
setExactNumberOfArguments(2) setExactNumberOfArguments(2)
setTrailingArgumentExpectedType(Cons::class.java) setTrailingArgumentExpectedType(Cons::class.java)
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,12 +13,12 @@ import sexpression.Symbol
@FunctionNames("LAMBDA", "Λ") @FunctionNames("LAMBDA", "Λ")
class Lambda(name: String) : LispSpecialFunction() { class Lambda(name: String) : LispSpecialFunction() {
private val argumentValidator: ArgumentValidator = ArgumentValidator(name).apply { private val argumentValidator = ArgumentValidator(name).apply {
setFirstArgumentExpectedType(Cons::class.java) setFirstArgumentExpectedType(Cons::class.java)
setMinimumNumberOfArguments(1) setMinimumNumberOfArguments(1)
} }
private val lambdaListValidator: ArgumentValidator = ArgumentValidator("$name|lambda-list|").apply { private val lambdaListValidator = ArgumentValidator("$name|lambda-list|").apply {
setEveryArgumentExpectedType(Symbol::class.java) setEveryArgumentExpectedType(Symbol::class.java)
} }
@ -55,9 +55,10 @@ class Lambda(name: String) : LispSpecialFunction() {
fun createFunction(lambdaExpression: Cons): UserDefinedFunction { fun createFunction(lambdaExpression: Cons): UserDefinedFunction {
val rest = lambdaExpression.rest val rest = lambdaExpression.rest
val lambdaValidator = ArgumentValidator("LAMBDA|create|") ArgumentValidator("LAMBDA|create|").run {
lambdaValidator.setEveryArgumentExpectedType(Cons::class.java) setEveryArgumentExpectedType(Cons::class.java)
lambdaValidator.validate(makeList(rest)) validate(makeList(rest))
}
val lambda = Lambda("LAMBDA").call(rest as Cons) 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.assertSExpressionsMatch;
import static testutil.TestUtilities.evaluateString; import static testutil.TestUtilities.evaluateString;
public class SETQTest extends SymbolAndFunctionCleaner { public class SetqTest extends SymbolAndFunctionCleaner {
@Test @Test
public void setq() { public void setq() {