mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
minimal: pork-rt minimal runtime
This commit is contained in:
49
minimal/build.gradle.kts
Normal file
49
minimal/build.gradle.kts
Normal file
@ -0,0 +1,49 @@
|
||||
plugins {
|
||||
application
|
||||
id("gay.pizza.pork.module")
|
||||
id("com.github.johnrengelman.shadow") version "8.1.1"
|
||||
id("org.graalvm.buildtools.native") version "0.9.25"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":ast"))
|
||||
api(project(":parser"))
|
||||
api(project(":frontend"))
|
||||
api(project(":evaluator"))
|
||||
api(project(":stdlib"))
|
||||
api(project(":ffi"))
|
||||
implementation(project(":common"))
|
||||
}
|
||||
|
||||
application {
|
||||
applicationName = "pork-rt"
|
||||
mainClass.set("gay.pizza.pork.minimal.MainKt")
|
||||
}
|
||||
|
||||
for (task in arrayOf(tasks.shadowDistTar, tasks.shadowDistZip, tasks.shadowJar)) {
|
||||
val suffix = when {
|
||||
task == tasks.shadowJar -> ""
|
||||
task.name.startsWith("shadow") -> "-shadow"
|
||||
else -> ""
|
||||
}
|
||||
task.get().archiveBaseName.set("pork-rt${suffix}")
|
||||
}
|
||||
|
||||
graalvmNative {
|
||||
binaries {
|
||||
named("main") {
|
||||
imageName.set("pork-rt")
|
||||
mainClass.set("gay.pizza.pork.minimal.MainKt")
|
||||
sharedLibrary.set(false)
|
||||
buildArgs("-march=compatibility")
|
||||
resources {
|
||||
includedPatterns.addAll(listOf(
|
||||
".*/*.pork$",
|
||||
".*/*.manifest$"
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.run.get().outputs.upToDateWhen { false }
|
16
minimal/src/main/kotlin/gay/pizza/pork/minimal/FileTool.kt
Normal file
16
minimal/src/main/kotlin/gay/pizza/pork/minimal/FileTool.kt
Normal file
@ -0,0 +1,16 @@
|
||||
package gay.pizza.pork.minimal
|
||||
|
||||
import gay.pizza.dough.fs.FsPath
|
||||
import gay.pizza.dough.fs.readString
|
||||
import gay.pizza.pork.frontend.ContentSource
|
||||
import gay.pizza.pork.frontend.FsContentSource
|
||||
import gay.pizza.pork.parser.CharSource
|
||||
import gay.pizza.pork.parser.StringCharSource
|
||||
|
||||
class FileTool(val path: FsPath) : Tool() {
|
||||
override fun createCharSource(): CharSource =
|
||||
StringCharSource(path.readString())
|
||||
override fun createContentSource(): ContentSource =
|
||||
FsContentSource(path.parent!!)
|
||||
override fun rootFilePath(): String = path.fullPathString
|
||||
}
|
56
minimal/src/main/kotlin/gay/pizza/pork/minimal/Tool.kt
Normal file
56
minimal/src/main/kotlin/gay/pizza/pork/minimal/Tool.kt
Normal file
@ -0,0 +1,56 @@
|
||||
package gay.pizza.pork.minimal
|
||||
|
||||
import gay.pizza.pork.ast.CompilationUnit
|
||||
import gay.pizza.pork.ast.NodeVisitor
|
||||
import gay.pizza.pork.ast.visit
|
||||
import gay.pizza.pork.evaluator.*
|
||||
import gay.pizza.pork.ffi.JavaAutogenContentSource
|
||||
import gay.pizza.pork.ffi.JavaNativeProvider
|
||||
import gay.pizza.pork.ffi.JnaNativeProvider
|
||||
import gay.pizza.pork.frontend.ContentSource
|
||||
import gay.pizza.pork.frontend.ImportLocator
|
||||
import gay.pizza.pork.frontend.DynamicImportSource
|
||||
import gay.pizza.pork.frontend.World
|
||||
import gay.pizza.pork.parser.*
|
||||
import gay.pizza.pork.stdlib.PorkStdlib
|
||||
|
||||
abstract class Tool {
|
||||
abstract fun createCharSource(): CharSource
|
||||
abstract fun createContentSource(): ContentSource
|
||||
abstract fun rootFilePath(): String
|
||||
|
||||
fun tokenize(): TokenStream =
|
||||
Tokenizer(createCharSource()).tokenize()
|
||||
|
||||
fun parse(attribution: NodeAttribution = DiscardNodeAttribution): CompilationUnit =
|
||||
Parser(TokenStreamSource(tokenize()), attribution).readCompilationUnit()
|
||||
|
||||
fun highlight(scheme: HighlightScheme): List<Highlight> =
|
||||
Highlighter(scheme).highlight(tokenize())
|
||||
|
||||
fun reprint(): String = buildString { visit(Printer(this)) }
|
||||
|
||||
fun <T> visit(visitor: NodeVisitor<T>): T = visitor.visit(parse())
|
||||
|
||||
fun loadMainFunction(scope: Scope, setupEvaluator: Evaluator.() -> Unit = {}): CallableFunction {
|
||||
val fileContentSource = createContentSource()
|
||||
val dynamicImportSource = DynamicImportSource()
|
||||
dynamicImportSource.addContentSource("std", PorkStdlib)
|
||||
dynamicImportSource.addContentSource("local", fileContentSource)
|
||||
dynamicImportSource.addContentSource("java", JavaAutogenContentSource)
|
||||
val world = World(dynamicImportSource)
|
||||
val evaluator = Evaluator(world, scope)
|
||||
setupEvaluator(evaluator)
|
||||
val resultingScope = evaluator.evaluate(ImportLocator("local", rootFilePath()))
|
||||
return resultingScope.value("main") as CallableFunction
|
||||
}
|
||||
|
||||
fun run(scope: Scope, quiet: Boolean = false) {
|
||||
val main = loadMainFunction(scope, setupEvaluator = {
|
||||
addNativeProvider("internal", InternalNativeProvider(quiet = quiet))
|
||||
addNativeProvider("ffi", JnaNativeProvider())
|
||||
addNativeProvider("java", JavaNativeProvider())
|
||||
})
|
||||
main.call(Arguments(emptyList()))
|
||||
}
|
||||
}
|
16
minimal/src/main/kotlin/gay/pizza/pork/minimal/main.kt
Normal file
16
minimal/src/main/kotlin/gay/pizza/pork/minimal/main.kt
Normal file
@ -0,0 +1,16 @@
|
||||
package gay.pizza.pork.minimal
|
||||
|
||||
import gay.pizza.dough.fs.PlatformFsProvider
|
||||
import gay.pizza.pork.evaluator.Scope
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.size != 1) {
|
||||
System.err.println("Usage: pork-rt <file>")
|
||||
exitProcess(1)
|
||||
}
|
||||
val path = PlatformFsProvider.resolve(args[0])
|
||||
val tool = FileTool(path)
|
||||
val scope = Scope()
|
||||
tool.run(scope)
|
||||
}
|
Reference in New Issue
Block a user