diff --git a/src/main/kotlin/function/builtin/Apply.kt b/src/main/kotlin/function/builtin/Apply.kt index 66eb6c4..37ecd32 100644 --- a/src/main/kotlin/function/builtin/Apply.kt +++ b/src/main/kotlin/function/builtin/Apply.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/BackquoteEvaluator.kt b/src/main/kotlin/function/builtin/BackquoteEvaluator.kt index e6b06c0..77fe8af 100644 --- a/src/main/kotlin/function/builtin/BackquoteEvaluator.kt +++ b/src/main/kotlin/function/builtin/BackquoteEvaluator.kt @@ -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 diff --git a/src/main/kotlin/function/builtin/Eval.kt b/src/main/kotlin/function/builtin/Eval.kt index c6b1a53..fce2880 100644 --- a/src/main/kotlin/function/builtin/Eval.kt +++ b/src/main/kotlin/function/builtin/Eval.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/Exit.kt b/src/main/kotlin/function/builtin/Exit.kt index b0c94a5..777bd21 100644 --- a/src/main/kotlin/function/builtin/Exit.kt +++ b/src/main/kotlin/function/builtin/Exit.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/Funcall.kt b/src/main/kotlin/function/builtin/Funcall.kt index aaa9b48..761e3fd 100644 --- a/src/main/kotlin/function/builtin/Funcall.kt +++ b/src/main/kotlin/function/builtin/Funcall.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/Fuse.kt b/src/main/kotlin/function/builtin/Fuse.kt index 31534a7..2fbef83 100644 --- a/src/main/kotlin/function/builtin/Fuse.kt +++ b/src/main/kotlin/function/builtin/Fuse.kt @@ -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) diff --git a/src/main/kotlin/function/builtin/Gensym.kt b/src/main/kotlin/function/builtin/Gensym.kt index 434c266..d79b102 100644 --- a/src/main/kotlin/function/builtin/Gensym.kt +++ b/src/main/kotlin/function/builtin/Gensym.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/Load.kt b/src/main/kotlin/function/builtin/Load.kt index e37b933..a345d73 100644 --- a/src/main/kotlin/function/builtin/Load.kt +++ b/src/main/kotlin/function/builtin/Load.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/Print.kt b/src/main/kotlin/function/builtin/Print.kt index 459bd92..8d5703c 100644 --- a/src/main/kotlin/function/builtin/Print.kt +++ b/src/main/kotlin/function/builtin/Print.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/Set.kt b/src/main/kotlin/function/builtin/Set.kt index 7a9693b..131e842 100644 --- a/src/main/kotlin/function/builtin/Set.kt +++ b/src/main/kotlin/function/builtin/Set.kt @@ -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) diff --git a/src/main/kotlin/function/builtin/SymbolFunction.kt b/src/main/kotlin/function/builtin/SymbolFunction.kt index ae49af7..3b5688a 100644 --- a/src/main/kotlin/function/builtin/SymbolFunction.kt +++ b/src/main/kotlin/function/builtin/SymbolFunction.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/Symbols.kt b/src/main/kotlin/function/builtin/Symbols.kt index 6f6fd60..f41a51d 100644 --- a/src/main/kotlin/function/builtin/Symbols.kt +++ b/src/main/kotlin/function/builtin/Symbols.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/predicate/GensymEqual.kt b/src/main/kotlin/function/builtin/predicate/GensymEqual.kt index 93d3a8d..596a5fd 100644 --- a/src/main/kotlin/function/builtin/predicate/GensymEqual.kt +++ b/src/main/kotlin/function/builtin/predicate/GensymEqual.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/predicate/NumericEqual.kt b/src/main/kotlin/function/builtin/predicate/NumericEqual.kt index 5f50fcc..34a72ed 100644 --- a/src/main/kotlin/function/builtin/predicate/NumericEqual.kt +++ b/src/main/kotlin/function/builtin/predicate/NumericEqual.kt @@ -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) } diff --git a/src/main/kotlin/function/builtin/special/Lambda.kt b/src/main/kotlin/function/builtin/special/Lambda.kt index 55b3a71..91aa6ea 100644 --- a/src/main/kotlin/function/builtin/special/Lambda.kt +++ b/src/main/kotlin/function/builtin/special/Lambda.kt @@ -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) diff --git a/src/main/kotlin/function/builtin/special/SETQ.java b/src/main/kotlin/function/builtin/special/SETQ.java deleted file mode 100644 index 4fae57e..0000000 --- a/src/main/kotlin/function/builtin/special/SETQ.java +++ /dev/null @@ -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)); - } -} diff --git a/src/main/kotlin/function/builtin/special/Setq.kt b/src/main/kotlin/function/builtin/special/Setq.kt new file mode 100644 index 0000000..48b41cb --- /dev/null +++ b/src/main/kotlin/function/builtin/special/Setq.kt @@ -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)) + } +} diff --git a/src/test/kotlin/function/builtin/special/SETQTest.java b/src/test/kotlin/function/builtin/special/SetqTest.java similarity index 98% rename from src/test/kotlin/function/builtin/special/SETQTest.java rename to src/test/kotlin/function/builtin/special/SetqTest.java index 3ba3be6..1e5a152 100644 --- a/src/test/kotlin/function/builtin/special/SETQTest.java +++ b/src/test/kotlin/function/builtin/special/SetqTest.java @@ -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() {