Files
pork/evaluator/src/main/kotlin/gay/pizza/pork/evaluator/Evaluator.kt

46 lines
1.5 KiB
Kotlin
Raw Normal View History

2023-09-04 01:56:24 -07:00
package gay.pizza.pork.evaluator
2023-08-19 15:29:07 -07:00
2023-11-14 23:44:10 -08:00
import gay.pizza.pork.ast.gen.Symbol
import gay.pizza.pork.execution.ExecutionContext
import gay.pizza.pork.execution.ExecutionContextProvider
import gay.pizza.pork.frontend.ImportLocator
2023-11-14 23:44:10 -08:00
import gay.pizza.pork.frontend.Slab
import gay.pizza.pork.frontend.World
2023-08-19 15:29:07 -07:00
2023-11-14 23:44:10 -08:00
class Evaluator(val world: World) : ExecutionContextProvider {
private val scope = Scope.root()
private val contexts = mutableMapOf<Slab, SlabContext>()
private val nativeProviders = mutableMapOf<String, NativeProvider>()
2023-08-19 15:29:07 -07:00
2023-11-14 23:44:10 -08:00
fun evaluate(locator: ImportLocator): Scope {
val slabContext = context(locator)
slabContext.finalizeScope()
return slabContext.externalScope
}
2023-08-19 15:29:07 -07:00
2023-11-14 23:44:10 -08:00
fun context(slab: Slab): SlabContext {
val slabContext = contexts.computeIfAbsent(slab) {
SlabContext(slab, this, scope)
2023-09-02 23:28:40 -07:00
}
2023-11-14 23:44:10 -08:00
slabContext.ensureImportedContextsExist()
return slabContext
2023-08-19 15:29:07 -07:00
}
2023-11-14 23:44:10 -08:00
fun context(locator: ImportLocator): SlabContext = context(world.load(locator))
fun nativeFunctionProvider(form: String): NativeProvider {
return nativeProviders[form] ?:
throw RuntimeException("Unknown native function form: $form")
}
fun addNativeProvider(form: String, nativeProvider: NativeProvider) {
nativeProviders[form] = nativeProvider
}
2023-11-14 23:44:10 -08:00
override fun prepare(importLocator: ImportLocator, entryPointSymbol: Symbol): ExecutionContext {
val slab = context(importLocator)
slab.finalizeScope()
return EvaluatorExecutionContext(this, slab, entryPointSymbol)
}
2023-08-19 15:29:07 -07:00
}