summaryrefslogtreecommitdiff
path: root/src/StackFrame.kt
blob: 9a0832fed76797cae467d6b098c29734c7e9d8df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package moe.nea.lisp

class StackFrame(val parent: StackFrame?) {

    val variables = mutableMapOf<String, LispData>()

    interface MetaKey<T>

    private val meta: MutableMap<MetaKey<*>, Any> = mutableMapOf()

    fun <T : Any> getMeta(key: MetaKey<T>): T? {
        return meta[key]?.let { it as T } ?: parent?.meta?.get(key)?.let { it as T }
    }

    fun <T : Any> setMeta(key: MetaKey<T>, value: T) {
        meta[key] = value
    }

    fun resolveReference(label: String): LispData? =
        variables[label] ?: parent?.resolveReference(label)

    fun setValueLocal(label: String, value: LispData): LispData {
        variables[label] = value
        return value
    }

    fun fork(): StackFrame {
        return StackFrame(this)
    }


}