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
|
2023-09-06 21:39:57 -07:00
|
|
|
import gay.pizza.pork.frontend.ImportLocator
|
2023-11-14 23:44:10 -08:00
|
|
|
import gay.pizza.pork.frontend.Slab
|
2023-09-03 02:26:21 -07:00
|
|
|
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>()
|
2023-09-10 19:27:59 -04:00
|
|
|
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-09-06 19:07:28 -07:00
|
|
|
|
2023-11-14 23:44:10 -08:00
|
|
|
fun context(locator: ImportLocator): SlabContext = context(world.load(locator))
|
|
|
|
|
2023-09-10 19:27:59 -04:00
|
|
|
fun nativeFunctionProvider(form: String): NativeProvider {
|
|
|
|
return nativeProviders[form] ?:
|
2023-09-06 19:07:28 -07:00
|
|
|
throw RuntimeException("Unknown native function form: $form")
|
|
|
|
}
|
|
|
|
|
2023-09-10 19:27:59 -04:00
|
|
|
fun addNativeProvider(form: String, nativeProvider: NativeProvider) {
|
|
|
|
nativeProviders[form] = nativeProvider
|
2023-09-06 19:07:28 -07:00
|
|
|
}
|
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
|
|
|
}
|