summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-08-09 17:29:32 +0200
committernea <nea@nea.moe>2023-08-09 17:29:32 +0200
commitfc3911c68a4a8a0ff916b858e5fe029ed2ebe493 (patch)
tree99dc8aa63882ee0f4299a095b8394cc9e0821e09 /src
parentf3600dacea3e38cd541bf57076f8a7141987e10b (diff)
downloadnealisp-fc3911c68a4a8a0ff916b858e5fe029ed2ebe493.tar.gz
nealisp-fc3911c68a4a8a0ff916b858e5fe029ed2ebe493.tar.bz2
nealisp-fc3911c68a4a8a0ff916b858e5fe029ed2ebe493.zip
Add more arithmetic operations
Diffstat (limited to 'src')
-rw-r--r--src/CoreBindings.kt42
1 files changed, 41 insertions, 1 deletions
diff --git a/src/CoreBindings.kt b/src/CoreBindings.kt
index 13c6de3..645a4ae 100644
--- a/src/CoreBindings.kt
+++ b/src/CoreBindings.kt
@@ -92,7 +92,7 @@ object CoreBindings {
is LispData.LispNode -> resolved.node.toSource()
is LispData.LispString -> resolved.string
is LispData.LispNumber -> resolved.value.toString()
- is LispData.LispInterpretedCallable -> "<function ${resolved.name ?: ""} ${resolved.argNames} ${resolved.body.toSource()}>"
+ is LispData.LispInterpretedCallable -> "<function ${resolved.name ?: "<anonymous>"} ${resolved.argNames} ${resolved.body.toSource()}>"
}
})
LispData.LispNil
@@ -106,9 +106,49 @@ object CoreBindings {
?: return@externalCall reportError("Unexpected argument $b, expected number")).value
})
}
+ val sub = LispData.externalCall { args, reportError ->
+ if (args.size == 0) {
+ return@externalCall reportError("Cannot call sub without at least 1 argument")
+ }
+ val c = args.map {
+ (it as? LispData.LispNumber
+ ?: 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
+ })
+ }
+ val mul = LispData.externalCall { args, reportError ->
+ if (args.size == 0) {
+ return@externalCall reportError("Cannot call mul without at least 1 argument")
+ }
+ LispData.LispNumber(args.fold(1.0) { a, b ->
+ a * (b as? LispData.LispNumber
+ ?: return@externalCall reportError("Unexpected argument $b, expected number")).value
+ })
+ }
+ val div = LispData.externalCall { args, reportError ->
+ if (args.size == 0) {
+ return@externalCall reportError("Cannot call div without at least 1 argument")
+ }
+ val c = args.map {
+ (it as? LispData.LispNumber
+ ?: 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
+ })
+ }
fun offerArithmeticTo(bindings: StackFrame) {
bindings.setValueLocal("+", add)
+ bindings.setValueLocal("/", div)
+ bindings.setValueLocal("*", mul)
+ bindings.setValueLocal("-", sub)
}
fun offerAllTo(bindings: StackFrame) {