mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 13:11:32 +00:00
implement native type compilation
This commit is contained in:
@ -47,7 +47,8 @@ val StandardOpHandlers: List<OpHandler> = listOf(
|
||||
CallOpHandler,
|
||||
ReturnOpHandler,
|
||||
|
||||
NativeOpHandler,
|
||||
NativeFunctionOpHandler,
|
||||
NativeTypeOpHandler,
|
||||
|
||||
ScopeInOpHandler,
|
||||
ScopeOutOpHandler,
|
||||
|
@ -2,11 +2,10 @@ package gay.pizza.pork.vm.ops
|
||||
|
||||
import gay.pizza.pork.bytecode.Op
|
||||
import gay.pizza.pork.bytecode.Opcode
|
||||
import gay.pizza.pork.execution.None
|
||||
import gay.pizza.pork.vm.InternalMachine
|
||||
import gay.pizza.pork.vm.OpHandler
|
||||
|
||||
object NativeOpHandler : OpHandler(Opcode.Native) {
|
||||
object NativeFunctionOpHandler : OpHandler(Opcode.NativeFunction) {
|
||||
override fun handle(machine: InternalMachine, op: Op) {
|
||||
val handler = optimize(machine, op)
|
||||
handler.handle(machine, op)
|
||||
@ -17,6 +16,6 @@ object NativeOpHandler : OpHandler(Opcode.Native) {
|
||||
val form = nativeDefinition[0]
|
||||
val provider = machine.nativeRegistry.of(form)
|
||||
val function = provider.provideNativeFunction(nativeDefinition.subList(1, nativeDefinition.size))
|
||||
return OptimizedNativeOpHandler(function)
|
||||
return OptimizedNativeFunctionOpHandler(function)
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package gay.pizza.pork.vm.ops
|
||||
|
||||
import gay.pizza.pork.bytecode.Op
|
||||
import gay.pizza.pork.bytecode.Opcode
|
||||
import gay.pizza.pork.vm.InternalMachine
|
||||
import gay.pizza.pork.vm.OpHandler
|
||||
|
||||
object NativeTypeOpHandler : OpHandler(Opcode.NativeType) {
|
||||
override fun handle(machine: InternalMachine, op: Op) {
|
||||
val handler = optimize(machine, op)
|
||||
handler.handle(machine, op)
|
||||
}
|
||||
|
||||
override fun optimize(machine: InternalMachine, op: Op): OpHandler {
|
||||
val nativeDefinition = machine.world.constantPool.read(op.args[0]).readAsNativeDefinition()
|
||||
val form = nativeDefinition[0]
|
||||
val provider = machine.nativeRegistry.of(form)
|
||||
val type = provider.provideNativeType(nativeDefinition.subList(1, nativeDefinition.size))
|
||||
return OptimizedNativeTypeOpHandler(type)
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ import gay.pizza.pork.execution.None
|
||||
import gay.pizza.pork.vm.InternalMachine
|
||||
import gay.pizza.pork.vm.OpHandler
|
||||
|
||||
class OptimizedNativeOpHandler(val function: NativeFunction) : OpHandler(Opcode.Native) {
|
||||
class OptimizedNativeFunctionOpHandler(val function: NativeFunction) : OpHandler(Opcode.NativeFunction) {
|
||||
override fun handle(machine: InternalMachine, op: Op) {
|
||||
val argumentCount = op.args[1]
|
||||
val arguments = mutableListOf<Any>()
|
@ -0,0 +1,16 @@
|
||||
package gay.pizza.pork.vm.ops
|
||||
|
||||
import gay.pizza.pork.bytecode.Op
|
||||
import gay.pizza.pork.bytecode.Opcode
|
||||
import gay.pizza.pork.execution.NativeFunction
|
||||
import gay.pizza.pork.execution.NativeType
|
||||
import gay.pizza.pork.execution.None
|
||||
import gay.pizza.pork.vm.InternalMachine
|
||||
import gay.pizza.pork.vm.OpHandler
|
||||
|
||||
class OptimizedNativeTypeOpHandler(val type: NativeType) : OpHandler(Opcode.NativeType) {
|
||||
override fun handle(machine: InternalMachine, op: Op) {
|
||||
val result = type.value()
|
||||
machine.push(if (result == Unit) None else result)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user