summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-08-10 00:05:10 +0200
committernea <nea@nea.moe>2023-08-10 00:05:10 +0200
commit7ce91434a452dc68f39aa34b646e691635c55e07 (patch)
tree9d1e6a94bb91cbe46ad60a17afbe4f760692ad01 /src
parent4878767a97c9ca78ec279e5a33cd7ae5b7b65493 (diff)
downloadnealisp-7ce91434a452dc68f39aa34b646e691635c55e07.tar.gz
nealisp-7ce91434a452dc68f39aa34b646e691635c55e07.tar.bz2
nealisp-7ce91434a452dc68f39aa34b646e691635c55e07.zip
If function
Diffstat (limited to 'src')
-rw-r--r--src/CoreBindings.kt29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/CoreBindings.kt b/src/CoreBindings.kt
index aa40366..cf4fc50 100644
--- a/src/CoreBindings.kt
+++ b/src/CoreBindings.kt
@@ -16,6 +16,32 @@ object CoreBindings {
return@externalRawCall stackFrame.setValueLocal(name.label, context.resolveValue(stackFrame, value))
}
+ private fun isTruthy(data: LispData): Boolean? {
+ if (data == trueValue) return true
+ if (data == falseValue) return false
+ return null
+ }
+
+ val trueValue = LispData.Atom("true")
+ val falseValue = LispData.Atom("false")
+
+ val ifFun = LispData.externalRawCall { context, callsite, stackFrame, args ->
+ if (args.size != 3) {
+ return@externalRawCall context.reportError("if requires 3 arguments", callsite)
+ }
+ val (cond, ifTrue, ifFalse) = args
+
+ val c = isTruthy(context.resolveValue(stackFrame, cond))
+ if (c == null) {
+ return@externalRawCall context.reportError("Non boolean value $c used as condition for if", cond)
+ }
+ if (c) {
+ return@externalRawCall context.resolveValue(stackFrame, ifTrue)
+ } else {
+ return@externalRawCall context.resolveValue(stackFrame, ifFalse)
+ }
+ }
+
val pure = LispData.externalCall { args, reportError ->
return@externalCall args.singleOrNull()?.let { value ->
LispData.externalCall { args, reportError ->
@@ -156,6 +182,9 @@ object CoreBindings {
}
fun offerAllTo(bindings: StackFrame) {
+ bindings.setValueLocal("true", trueValue)
+ bindings.setValueLocal("false", falseValue)
+ bindings.setValueLocal("if", ifFun)
bindings.setValueLocal("nil", nil)
bindings.setValueLocal("def", def)
bindings.setValueLocal("pure", pure)