mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
vm: very basic virtual machine
This commit is contained in:
@ -7,6 +7,8 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
api(project(":minimal"))
|
||||
api(project(":compiler"))
|
||||
api(project(":vm"))
|
||||
api("com.github.ajalt.clikt:clikt:4.2.0")
|
||||
|
||||
implementation(project(":common"))
|
||||
|
33
tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt
Normal file
33
tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt
Normal file
@ -0,0 +1,33 @@
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
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.compiler.Compiler
|
||||
import gay.pizza.pork.minimal.FileTool
|
||||
import gay.pizza.pork.vm.VirtualMachine
|
||||
|
||||
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 slab = world.load(tool.rootImportLocator)
|
||||
val compiledSlab = compiler.compilableSlabs.of(slab)
|
||||
val compiledMain = compiledSlab.compilableSymbolOf(Symbol("main"))
|
||||
?: throw RuntimeException("'main' function not found.")
|
||||
val compiledWorld = compiler.compile(compiledMain)
|
||||
for (symbol in compiledWorld.symbolTable.symbols) {
|
||||
val code = compiledWorld.code.subList(symbol.offset.toInt(), (symbol.offset + symbol.size).toInt())
|
||||
println(symbol.id)
|
||||
for ((index, op) in code.withIndex()) {
|
||||
println(" ${symbol.offset + index.toUInt()} ${op.code.name} ${op.args.joinToString(" ")}")
|
||||
}
|
||||
}
|
||||
val vm = VirtualMachine(compiledWorld)
|
||||
vm.execute()
|
||||
}
|
||||
}
|
@ -17,7 +17,8 @@ class RootCommand : CliktCommand(
|
||||
AstCommand(),
|
||||
AttributeCommand(),
|
||||
ScopeAnalysisCommand(),
|
||||
CopyStdlibCommand()
|
||||
CopyStdlibCommand(),
|
||||
CompileCommand()
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,6 @@ import com.github.ajalt.clikt.parameters.options.flag
|
||||
import com.github.ajalt.clikt.parameters.options.option
|
||||
import gay.pizza.dough.fs.PlatformFsProvider
|
||||
import gay.pizza.pork.evaluator.*
|
||||
import gay.pizza.pork.ffi.FfiNativeProvider
|
||||
import gay.pizza.pork.ffi.JavaNativeProvider
|
||||
import gay.pizza.pork.minimal.FileTool
|
||||
|
||||
class RunCommand : CliktCommand(help = "Run Program", name = "run") {
|
||||
@ -19,12 +17,11 @@ class RunCommand : CliktCommand(help = "Run Program", name = "run") {
|
||||
|
||||
override fun run() {
|
||||
val tool = FileTool(PlatformFsProvider.resolve(path))
|
||||
val scope = Scope.root()
|
||||
val main = tool.loadMainFunctionStandard(scope, quiet = quiet)
|
||||
val main = tool.loadMainFunctionStandard(quiet = quiet)
|
||||
|
||||
if (dumpScope) {
|
||||
val functionContext = main as FunctionContext
|
||||
val internalScope = functionContext.compilationUnitContext.internalScope
|
||||
val internalScope = functionContext.slabContext.internalScope
|
||||
internalScope.crawlScopePath { key, path ->
|
||||
println("[scope] $key [${path.joinToString(" -> ")}]")
|
||||
}
|
||||
|
@ -15,12 +15,12 @@ class ScopeAnalysisCommand : CliktCommand(help = "Run Scope Analysis", name = "s
|
||||
val root = world.load(tool.rootImportLocator)
|
||||
val scope = WorldScope(world).apply { index(root) }
|
||||
val rootScope = scope.scope(root)
|
||||
val visibleScopeSymbols = rootScope.findInternallyVisibleSymbols()
|
||||
for (visibleScopeSymbol in visibleScopeSymbols) {
|
||||
for (visibleScopeSymbol in rootScope.internallyVisibleSymbols) {
|
||||
println(
|
||||
"symbol ${visibleScopeSymbol.scopeSymbol.symbol.id} " +
|
||||
"type=${visibleScopeSymbol.scopeSymbol.definition.type.name} " +
|
||||
"internal=${visibleScopeSymbol.isInternalSymbol}"
|
||||
"internal=${visibleScopeSymbol.isInternalSymbol} " +
|
||||
"slab=${visibleScopeSymbol.scopeSymbol.slab.location.commonFriendlyName}"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user