diff options
author | nea <nea@nea.moe> | 2023-08-10 18:30:49 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-08-10 18:30:49 +0200 |
commit | 912c9918d9a5afbfb023b650bed6e2754f030d5e (patch) | |
tree | f73ad72e3fa2389458ef927f1bec4fb9805c813a /src | |
parent | 873864974127ec4f123c7db0560235e806c3faab (diff) | |
download | nealisp-912c9918d9a5afbfb023b650bed6e2754f030d5e.tar.gz nealisp-912c9918d9a5afbfb023b650bed6e2754f030d5e.tar.bz2 nealisp-912c9918d9a5afbfb023b650bed6e2754f030d5e.zip |
Add more tests
Diffstat (limited to 'src')
-rw-r--r-- | src/CoreBindings.kt | 29 | ||||
-rw-r--r-- | src/LispData.kt | 8 |
2 files changed, 31 insertions, 6 deletions
diff --git a/src/CoreBindings.kt b/src/CoreBindings.kt index 571b499..d88c950 100644 --- a/src/CoreBindings.kt +++ b/src/CoreBindings.kt @@ -147,9 +147,8 @@ object CoreBindings { ?: return@externalCall reportError("Unexpected argument $it, expected number") ).value } - LispData.LispNumber(args.drop(1).fold(c.first()) { a, b -> - a - (b as? LispData.LispNumber - ?: return@externalCall reportError("Unexpected argument $b, expected number")).value + LispData.LispNumber(c.drop(1).fold(c.first()) { a, b -> + a - b }) } val mul = LispData.externalCall("mul") { args, reportError -> @@ -161,6 +160,12 @@ object CoreBindings { ?: return@externalCall reportError("Unexpected argument $b, expected number")).value }) } + val eq = LispData.externalCall("eq") { args, reportError -> + if (args.size == 0) { + return@externalCall reportError("Cannot call eq without at least 1 argument") + } + LispData.boolean(!args.zipWithNext().any { it.first != it.second }) + } val div = LispData.externalCall("div") { args, reportError -> if (args.size == 0) { return@externalCall reportError("Cannot call div without at least 1 argument") @@ -170,11 +175,21 @@ object CoreBindings { ?: return@externalCall reportError("Unexpected argument $it, expected number") ).value } - LispData.LispNumber(args.drop(1).fold(c.first()) { a, b -> - a / (b as? LispData.LispNumber - ?: return@externalCall reportError("Unexpected argument $b, expected number")).value + LispData.LispNumber(c.drop(1).fold(c.first()) { a, b -> + a / b }) } + val less = LispData.externalCall("less") { args, reportError -> + if (args.size != 2) { + return@externalCall reportError("Cannot call less without exactly 2 arguments") + } + val (left, right) = args.map { + (it as? LispData.LispNumber + ?: return@externalCall reportError("Unexpected argument $it, expected number") + ).value + } + LispData.boolean(left < right) + } val import = LispData.externalRawCall("import") { context, callsite, stackFrame, args -> if (args.size != 1) { return@externalRawCall context.reportError("import needs at least one argument", callsite) @@ -194,6 +209,8 @@ object CoreBindings { bindings.setValueLocal("/", div) bindings.setValueLocal("*", mul) bindings.setValueLocal("-", sub) + bindings.setValueLocal("lt", less) + bindings.setValueLocal("=", eq) } fun offerAllTo(bindings: StackFrame) { diff --git a/src/LispData.kt b/src/LispData.kt index 5af3dd4..8eac3f7 100644 --- a/src/LispData.kt +++ b/src/LispData.kt @@ -99,5 +99,13 @@ sealed class LispData { ): LispExecutable { return LispInterpretedCallable(declarationStackFrame, args, body, nameHint) } + + fun boolean(b: Boolean): Atom { + return if (b) { + CoreBindings.trueValue + } else { + CoreBindings.falseValue + } + } } } |