summaryrefslogtreecommitdiff
path: root/src/CoreBindings.kt
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-08-10 18:30:49 +0200
committernea <nea@nea.moe>2023-08-10 18:30:49 +0200
commit912c9918d9a5afbfb023b650bed6e2754f030d5e (patch)
treef73ad72e3fa2389458ef927f1bec4fb9805c813a /src/CoreBindings.kt
parent873864974127ec4f123c7db0560235e806c3faab (diff)
downloadnealisp-912c9918d9a5afbfb023b650bed6e2754f030d5e.tar.gz
nealisp-912c9918d9a5afbfb023b650bed6e2754f030d5e.tar.bz2
nealisp-912c9918d9a5afbfb023b650bed6e2754f030d5e.zip
Add more tests
Diffstat (limited to 'src/CoreBindings.kt')
-rw-r--r--src/CoreBindings.kt29
1 files changed, 23 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) {