language: implement proper virtual machine support

This commit is contained in:
2023-11-28 05:23:48 -08:00
parent 8951c3cd60
commit f2ff23e9be
55 changed files with 312 additions and 63 deletions

View File

@ -4,21 +4,40 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import gay.pizza.dough.fs.PlatformFsProvider
import gay.pizza.pork.ast.gen.Symbol
import gay.pizza.pork.bir.IrWorld
import gay.pizza.pork.bytecode.CompiledWorld
import gay.pizza.pork.compiler.Compiler
import gay.pizza.pork.minimal.FileTool
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
@OptIn(ExperimentalSerializationApi::class)
class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "compile") {
val path by argument("file")
override fun run() {
val tool = FileTool(PlatformFsProvider.resolve(path))
val world = tool.buildWorld()
val compiler = Compiler()
val compiler = Compiler(world)
val slab = world.load(tool.rootImportLocator)
val compiledSlab = compiler.compilableSlabs.of(slab)
val compiledMain = compiledSlab.resolve(Symbol("main"))
?: throw RuntimeException("'main' function not found.")
val compiledWorld = compiler.compile(compiledMain)
val irWorld = compiler.compileIrWorld()
printCompiledIr(irWorld)
printCompiledWorld(compiledWorld)
}
private fun printCompiledIr(irWorld: IrWorld) {
val json = Json {
prettyPrint = true
prettyPrintIndent = " "
}
println(json.encodeToString(IrWorld.serializer(), irWorld))
}
private fun printCompiledWorld(compiledWorld: CompiledWorld) {
for (symbol in compiledWorld.symbolTable.symbols) {
val code = compiledWorld.code.subList(symbol.offset.toInt(), (symbol.offset + symbol.size).toInt())
println(symbol.commonSymbolIdentity)