+namespaces with direct resolution
+national chars in ids
This commit is contained in:
1 parent
b220299fc5
commit
1f2afbbe38
10 files changed
+240
-116
No files matched your search
@@ -1,15 +1,13 @@
|
||||
package net.sergeych.ling
|
||||
|
||||
import kotlin.math.PI
|
||||
|
||||
class Context(
|
||||
val callerPos: Pos,
|
||||
val parent: Context? = null,
|
||||
val args: Arguments = Arguments.EMPTY
|
||||
) {
|
||||
|
||||
data class Item(
|
||||
val name: String, var value: Obj?,
|
||||
val name: String,
|
||||
var value: Obj?,
|
||||
val isMutable: Boolean = false
|
||||
)
|
||||
|
||||
@@ -17,30 +15,51 @@ class Context(
|
||||
|
||||
operator fun get(name: String): Item? = objects[name] ?: parent?.get(name)
|
||||
|
||||
fun copy(from: Pos, args: Arguments = Arguments.EMPTY): Context = Context(from, this, args)
|
||||
fun copy(args: Arguments = Arguments.EMPTY): Context = Context(this, args)
|
||||
|
||||
fun addFn(name: String, fn: suspend Context.() -> Obj) = objects.put(name, Item(name,
|
||||
object : Statement() {
|
||||
fun addItem(name: String, isMutable: Boolean, value: Obj?) {
|
||||
objects.put(name, Item(name, value, isMutable))
|
||||
}
|
||||
|
||||
fun getOrCreateNamespace(name: String) =
|
||||
(objects.getOrPut(name) { Item(name, ObjNamespace(name,copy()), isMutable = false) }.value as ObjNamespace)
|
||||
.context
|
||||
|
||||
inline fun <reified T> addFn(vararg names: String, crossinline fn: suspend Context.() -> T) {
|
||||
val newFn = object : Statement() {
|
||||
override val pos: Pos = Pos.builtIn
|
||||
|
||||
override suspend fun execute(context: Context): Obj {
|
||||
return try {
|
||||
context.fn()
|
||||
from(context.fn())
|
||||
|
||||
} catch (e: Exception) {
|
||||
raise(e.message ?: "unexpected error")
|
||||
}
|
||||
}
|
||||
}
|
||||
for (name in names) {
|
||||
addItem(
|
||||
name,
|
||||
false,
|
||||
newFn
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
)
|
||||
inline fun <reified T> addConst(value: T,vararg names: String) {
|
||||
val obj = Obj.from(value)
|
||||
for (name in names) {
|
||||
addItem(
|
||||
name,
|
||||
false,
|
||||
obj
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
operator fun invoke() = Context()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
val basicContext = Context(Pos.builtIn).apply {
|
||||
addFn("println") {
|
||||
require(args.size == 1)
|
||||
println(args[0].execute(this).asStr.value)
|
||||
Void
|
||||
}
|
||||
addFn("π") { ObjReal(PI) }
|
||||
}
|
||||
Reference in new issue
Block a user