summaryrefslogtreecommitdiff
path: root/src/CoreBindings.kt
diff options
context:
space:
mode:
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) {