diff options
author | Linnea Gräf <nea@nea.moe> | 2024-04-30 00:12:27 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-04-30 00:12:27 +0200 |
commit | b51321b27fdf513a3de09d084bbd4e69a26fb76e (patch) | |
tree | 873c433cc96e382321736fe0bcb4e2948789ced6 /src/LispData.kt | |
parent | 37d5b9ce0acf6eb25162c097990983f388ec3a19 (diff) | |
download | nealisp-b51321b27fdf513a3de09d084bbd4e69a26fb76e.tar.gz nealisp-b51321b27fdf513a3de09d084bbd4e69a26fb76e.tar.bz2 nealisp-b51321b27fdf513a3de09d084bbd4e69a26fb76e.zip |
Add lists
Diffstat (limited to 'src/LispData.kt')
-rw-r--r-- | src/LispData.kt | 198 |
1 files changed, 99 insertions, 99 deletions
diff --git a/src/LispData.kt b/src/LispData.kt index 61c2b76..8e858f3 100644 --- a/src/LispData.kt +++ b/src/LispData.kt @@ -2,111 +2,111 @@ package moe.nea.lisp sealed class LispData { - object LispNil : LispData() - data class Atom(val label: String) : LispData() - data class LispString(val string: String) : LispData() - data class LispNumber(val value: Double) : LispData() - data class LispNode(val node: LispAst.LispNode) : LispData() - data class LispHash(val map: Map<String, LispData>) : LispData() - data class ForeignObject<T : Any?>(val obj: T) : LispData() - class LispList(val elements: List<LispData>) : LispData() - sealed class LispExecutable() : LispData() { - abstract fun execute( - executionContext: LispExecutionContext, - callsite: LispAst.LispNode, - stackFrame: StackFrame, - args: List<LispAst.LispNode> - ): LispData - } + object LispNil : LispData() + data class Atom(val label: String) : LispData() + data class LispString(val string: String) : LispData() + data class LispNumber(val value: Double) : LispData() + data class LispNode(val node: LispAst.LispNode) : LispData() + data class LispHash(val map: Map<String, LispData>) : LispData() + data class ForeignObject<T : Any?>(val obj: T) : LispData() + data class LispList(val elements: List<LispData>) : LispData() + sealed class LispExecutable() : LispData() { + abstract fun execute( + executionContext: LispExecutionContext, + callsite: LispAst.LispNode, + stackFrame: StackFrame, + args: List<LispAst.LispNode> + ): LispData + } - abstract class JavaExecutable(val name: String) : LispExecutable() + abstract class JavaExecutable(val name: String) : LispExecutable() - data class LispInterpretedCallable( - val declarationStackFrame: StackFrame, - val argNames: List<String>, - val body: LispAst.Parenthesis, - val name: String?, - ) : LispExecutable() { - override fun execute( - executionContext: LispExecutionContext, - callsite: LispAst.LispNode, - stackFrame: StackFrame, - args: List<LispAst.LispNode> - ): LispData { + data class LispInterpretedCallable( + val declarationStackFrame: StackFrame, + val argNames: List<String>, + val body: LispAst.Parenthesis, + val name: String?, + ) : LispExecutable() { + override fun execute( + executionContext: LispExecutionContext, + callsite: LispAst.LispNode, + stackFrame: StackFrame, + args: List<LispAst.LispNode> + ): LispData { - val invocationFrame = declarationStackFrame.fork() - if (argNames.lastOrNull() == "...") { - for ((name, value) in argNames.dropLast(1).zip(args)) { - invocationFrame.setValueLocal(name, executionContext.resolveValue(stackFrame, value)) - } - invocationFrame.setValueLocal( - "...", - LispList( - args.drop(argNames.size - 1).map { executionContext.resolveValue(stackFrame, it) }) - ) - } else if (argNames.size != args.size) { - return stackFrame.reportError( - "Expected ${argNames.size} arguments, got ${args.size} instead", - callsite - ) - } else - for ((name, value) in argNames.zip(args)) { - invocationFrame.setValueLocal(name, executionContext.resolveValue(stackFrame, value)) - } - return executionContext.executeLisp(invocationFrame, body) - } - } + val invocationFrame = declarationStackFrame.fork() + if (argNames.lastOrNull() == "...") { + for ((name, value) in argNames.dropLast(1).zip(args)) { + invocationFrame.setValueLocal(name, executionContext.resolveValue(stackFrame, value)) + } + invocationFrame.setValueLocal( + "...", + LispList( + args.drop(argNames.size - 1).map { executionContext.resolveValue(stackFrame, it) }) + ) + } else if (argNames.size != args.size) { + return stackFrame.reportError( + "Expected ${argNames.size} arguments, got ${args.size} instead", + callsite + ) + } else + for ((name, value) in argNames.zip(args)) { + invocationFrame.setValueLocal(name, executionContext.resolveValue(stackFrame, value)) + } + return executionContext.executeLisp(invocationFrame, body) + } + } - companion object { - fun externalRawCall( - name: String, - callable: (context: LispExecutionContext, callsite: LispAst.LispNode, stackFrame: StackFrame, args: List<LispAst.LispNode>) -> LispData - ): LispExecutable { - return object : JavaExecutable(name) { - override fun execute( - executionContext: LispExecutionContext, - callsite: LispAst.LispNode, - stackFrame: StackFrame, - args: List<LispAst.LispNode> - ): LispData { - return callable.invoke(executionContext, callsite, stackFrame, args) - } - } - } + companion object { + fun externalRawCall( + name: String, + callable: (context: LispExecutionContext, callsite: LispAst.LispNode, stackFrame: StackFrame, args: List<LispAst.LispNode>) -> LispData + ): LispExecutable { + return object : JavaExecutable(name) { + override fun execute( + executionContext: LispExecutionContext, + callsite: LispAst.LispNode, + stackFrame: StackFrame, + args: List<LispAst.LispNode> + ): LispData { + return callable.invoke(executionContext, callsite, stackFrame, args) + } + } + } - fun externalCall( - name: String, - callable: (args: List<LispData>, reportError: (String) -> LispData) -> LispData - ): LispExecutable { - return object : JavaExecutable(name) { - override fun execute( - executionContext: LispExecutionContext, - callsite: LispAst.LispNode, - stackFrame: StackFrame, - args: List<LispAst.LispNode> - ): LispData { - val mappedArgs = args.map { executionContext.resolveValue(stackFrame, it) } - return callable.invoke(mappedArgs) { stackFrame.reportError(it, callsite) } - } - } - } + fun externalCall( + name: String, + callable: (args: List<LispData>, reportError: (String) -> LispData) -> LispData + ): LispExecutable { + return object : JavaExecutable(name) { + override fun execute( + executionContext: LispExecutionContext, + callsite: LispAst.LispNode, + stackFrame: StackFrame, + args: List<LispAst.LispNode> + ): LispData { + val mappedArgs = args.map { executionContext.resolveValue(stackFrame, it) } + return callable.invoke(mappedArgs) { stackFrame.reportError(it, callsite) } + } + } + } - fun createLambda( - declarationStackFrame: StackFrame, - args: List<String>, - body: LispAst.Parenthesis, - nameHint: String? = null, - ): LispExecutable { - return LispInterpretedCallable(declarationStackFrame, args, body, nameHint) - } + fun createLambda( + declarationStackFrame: StackFrame, + args: List<String>, + body: LispAst.Parenthesis, + nameHint: String? = null, + ): LispExecutable { + return LispInterpretedCallable(declarationStackFrame, args, body, nameHint) + } - fun boolean(b: Boolean): Atom { - return if (b) { - CoreBindings.trueValue - } else { - CoreBindings.falseValue - } - } - } + fun boolean(b: Boolean): Atom { + return if (b) { + CoreBindings.trueValue + } else { + CoreBindings.falseValue + } + } + } } |