summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-08-10 01:37:33 +0200
committernea <nea@nea.moe>2023-08-10 01:37:33 +0200
commit74371302593ee2ee365e697911c364246f460b63 (patch)
treef4c8e5c4e7f31fbab219425795d967ed8b77e4d0 /src
parent7ce91434a452dc68f39aa34b646e691635c55e07 (diff)
downloadnealisp-74371302593ee2ee365e697911c364246f460b63.tar.gz
nealisp-74371302593ee2ee365e697911c364246f460b63.tar.bz2
nealisp-74371302593ee2ee365e697911c364246f460b63.zip
Add builtins
Diffstat (limited to 'src')
-rw-r--r--src/Builtins.kt18
-rw-r--r--src/LispExecutionContext.kt7
-rw-r--r--src/LispScriptEngine.kt6
3 files changed, 29 insertions, 2 deletions
diff --git a/src/Builtins.kt b/src/Builtins.kt
new file mode 100644
index 0000000..484ea3a
--- /dev/null
+++ b/src/Builtins.kt
@@ -0,0 +1,18 @@
+package moe.nea.lisp
+
+object Builtins {
+ val builtinSource = Builtins::class.java.getResourceAsStream("/builtins.lisp")!!.bufferedReader().readText()
+ val builtinProgram = LispParser.parse("builtins.lisp", builtinSource)
+ fun loadBuiltins(
+ lispExecutionContext: LispExecutionContext,
+ consumer: (String, LispData) -> Unit,
+ ) {
+ val stackFrame = lispExecutionContext.genBindings()
+ stackFrame.setValueLocal("export", LispData.externalRawCall { context, callsite, stackFrame, args ->
+ val (name) = args
+ consumer((name as LispAst.Reference).label, context.resolveValue(stackFrame, name))
+ return@externalRawCall LispData.LispNil
+ })
+ lispExecutionContext.executeProgram(stackFrame, builtinProgram)
+ }
+} \ No newline at end of file
diff --git a/src/LispExecutionContext.kt b/src/LispExecutionContext.kt
index ba7df41..f85cc4b 100644
--- a/src/LispExecutionContext.kt
+++ b/src/LispExecutionContext.kt
@@ -3,7 +3,7 @@ package moe.nea.lisp
class LispExecutionContext() {
private val errorReporter = LispErrorReporter()
- private val rootStackFrame = StackFrame(null)
+ val rootStackFrame = StackFrame(null)
fun reportError(name: String, position: HasLispPosition): LispData.LispNil {
@@ -16,6 +16,11 @@ class LispExecutionContext() {
return StackFrame(rootStackFrame)
}
+ fun setupStandardBindings() {
+ CoreBindings.offerAllTo(rootStackFrame)
+ Builtins.loadBuiltins(this, rootStackFrame::setValueLocal)
+ }
+
fun executeProgram(stackFrame: StackFrame, program: LispAst.Program): LispData? {
var lastValue: LispData? = null
for (node in program.nodes) {
diff --git a/src/LispScriptEngine.kt b/src/LispScriptEngine.kt
index 3d2db8f..94dd4c6 100644
--- a/src/LispScriptEngine.kt
+++ b/src/LispScriptEngine.kt
@@ -8,11 +8,15 @@ import javax.script.SimpleBindings
class LispScriptEngine(private val factory: LispScriptEngineFactory) : AbstractScriptEngine() {
val executionContext = LispExecutionContext()
+
+ init {
+ executionContext.setupStandardBindings()
+ }
+
override fun eval(script: String, context: ScriptContext): LispData? {
val fileName = context.getAttribute("scriptName") as? String ?: "script.lisp"
val program = LispParser.parse(fileName, script)
val root = executionContext.genBindings()
- CoreBindings.offerAllTo(root)
for ((name, value) in context.getBindings(ScriptContext.ENGINE_SCOPE)) {
when (value) {
is String -> root.setValueLocal(name, LispData.LispString(value))