2023-09-11 02:34:28 -04:00
|
|
|
package gay.pizza.pork.minimal
|
2023-08-22 19:54:21 -07:00
|
|
|
|
2023-10-06 15:39:13 -07:00
|
|
|
import gay.pizza.pork.ast.gen.CompilationUnit
|
|
|
|
import gay.pizza.pork.ast.gen.NodeVisitor
|
2023-11-21 22:18:05 -08:00
|
|
|
import gay.pizza.pork.ast.gen.Symbol
|
2023-10-06 15:39:13 -07:00
|
|
|
import gay.pizza.pork.ast.gen.visit
|
2023-09-11 02:34:28 -04:00
|
|
|
import gay.pizza.pork.evaluator.*
|
2023-11-21 22:18:05 -08:00
|
|
|
import gay.pizza.pork.execution.ExecutionContext
|
|
|
|
import gay.pizza.pork.execution.ExecutionContextProvider
|
|
|
|
import gay.pizza.pork.execution.InternalNativeProvider
|
|
|
|
import gay.pizza.pork.execution.NativeRegistry
|
2023-09-23 15:40:40 -07:00
|
|
|
import gay.pizza.pork.ffi.FfiNativeProvider
|
2023-09-07 01:51:50 -07:00
|
|
|
import gay.pizza.pork.ffi.JavaAutogenContentSource
|
2023-09-11 02:34:28 -04:00
|
|
|
import gay.pizza.pork.ffi.JavaNativeProvider
|
2023-09-03 02:26:21 -07:00
|
|
|
import gay.pizza.pork.frontend.ContentSource
|
2023-09-06 21:39:57 -07:00
|
|
|
import gay.pizza.pork.frontend.ImportLocator
|
2023-09-06 22:43:03 -07:00
|
|
|
import gay.pizza.pork.frontend.DynamicImportSource
|
2023-09-03 02:26:21 -07:00
|
|
|
import gay.pizza.pork.frontend.World
|
2023-09-04 01:56:24 -07:00
|
|
|
import gay.pizza.pork.parser.*
|
2023-09-06 21:39:57 -07:00
|
|
|
import gay.pizza.pork.stdlib.PorkStdlib
|
2023-10-16 21:52:21 -07:00
|
|
|
import gay.pizza.pork.tokenizer.*
|
2023-08-22 19:54:21 -07:00
|
|
|
|
2023-09-03 01:11:27 -07:00
|
|
|
abstract class Tool {
|
2023-08-22 19:54:21 -07:00
|
|
|
abstract fun createCharSource(): CharSource
|
2023-09-03 02:26:21 -07:00
|
|
|
abstract fun createContentSource(): ContentSource
|
|
|
|
abstract fun rootFilePath(): String
|
2023-08-22 19:54:21 -07:00
|
|
|
|
2023-09-14 14:16:08 -07:00
|
|
|
val rootImportLocator: ImportLocator
|
|
|
|
get() = ImportLocator("local", rootFilePath())
|
|
|
|
|
2023-10-14 03:28:07 -07:00
|
|
|
fun tokenize(): Tokenizer =
|
|
|
|
Tokenizer(createCharSource())
|
2023-08-22 19:54:21 -07:00
|
|
|
|
2023-09-02 20:22:08 -07:00
|
|
|
fun parse(attribution: NodeAttribution = DiscardNodeAttribution): CompilationUnit =
|
2023-10-14 03:28:07 -07:00
|
|
|
Parser(tokenize(), attribution).parseCompilationUnit()
|
2023-08-22 19:54:21 -07:00
|
|
|
|
2023-10-16 22:16:53 -07:00
|
|
|
fun highlight(scheme: HighlightScheme): Sequence<Highlight> =
|
|
|
|
Highlighter(scheme).highlight(tokenize())
|
2023-08-22 19:54:21 -07:00
|
|
|
|
|
|
|
fun reprint(): String = buildString { visit(Printer(this)) }
|
|
|
|
|
|
|
|
fun <T> visit(visitor: NodeVisitor<T>): T = visitor.visit(parse())
|
2023-09-02 23:28:40 -07:00
|
|
|
|
2023-11-21 22:18:05 -08:00
|
|
|
fun createExecutionContextProvider(type: ExecutionType): ExecutionContextProvider =
|
|
|
|
type.create(buildWorld())
|
2023-09-14 14:16:08 -07:00
|
|
|
|
2023-11-21 22:18:05 -08:00
|
|
|
fun createExecutionContext(type: ExecutionType, symbol: Symbol, nativeRegistry: NativeRegistry): ExecutionContext {
|
|
|
|
val executionContextProvider = createExecutionContextProvider(type)
|
|
|
|
return executionContextProvider.prepare(rootImportLocator, symbol, nativeRegistry)
|
|
|
|
}
|
2023-10-16 22:16:53 -07:00
|
|
|
|
2023-09-14 14:16:08 -07:00
|
|
|
fun buildWorld(): World {
|
2023-09-06 21:39:57 -07:00
|
|
|
val fileContentSource = createContentSource()
|
2023-09-06 22:43:03 -07:00
|
|
|
val dynamicImportSource = DynamicImportSource()
|
|
|
|
dynamicImportSource.addContentSource("std", PorkStdlib)
|
|
|
|
dynamicImportSource.addContentSource("local", fileContentSource)
|
2023-09-07 01:51:50 -07:00
|
|
|
dynamicImportSource.addContentSource("java", JavaAutogenContentSource)
|
2023-09-14 14:16:08 -07:00
|
|
|
return World(dynamicImportSource)
|
2023-09-02 23:28:40 -07:00
|
|
|
}
|
2023-08-22 19:54:21 -07:00
|
|
|
}
|