Skip to content

Commit

Permalink
fix game of life support in evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Dec 1, 2024
1 parent 4ec5624 commit 6e225aa
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ out/
/.fleet/*
.DS_Store
.intellijPlatform
.kotlin/
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class EvaluatorProvider(val world: World) : ExecutionContextProvider {
override fun prepare(importLocator: ImportLocator, entryPointSymbol: Symbol, nativeRegistry: NativeRegistry): ExecutionContext {
val evaluator = Evaluator(world)
nativeRegistry.forEachProvider { form, provider ->
evaluator.addNativeProvider(form, AdaptedNativeProvider(provider))
if (provider is ExpandedNativeProvider) {
evaluator.addNativeProvider(form, provider)
} else {
evaluator.addNativeProvider(form, AdaptedNativeProvider(provider))
}
}
val slab = evaluator.slabContext(importLocator)
slab.finalizeScope()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class InternalNativeProvider(val quiet: Boolean = false) : NativeProvider {
@Suppress("UNCHECKED_CAST")
val list = arguments[0] as MutableList<Any>
val value = arguments[2]
list[(arguments.at<Number>(0)).toInt()] = value
list[(arguments.at<Number>(1)).toInt()] = value
return value
}

Expand Down
8 changes: 7 additions & 1 deletion ffi/src/main/kotlin/gay/pizza/pork/ffi/FfiNativeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import com.kenai.jffi.Function
import gay.pizza.pork.ast.gen.ArgumentSpec
import gay.pizza.pork.evaluator.*
import gay.pizza.pork.execution.ArgumentList
import gay.pizza.pork.execution.NativeFunction
import gay.pizza.pork.execution.NativeProvider
import gay.pizza.pork.execution.None
import kotlin.io.path.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.exists

class FfiNativeProvider : ExpandedNativeProvider {
class FfiNativeProvider : ExpandedNativeProvider, NativeProvider {
private val internalFunctions = mutableMapOf<String, (ArgumentList) -> Any>(
"ffiStructDefine" to ::ffiStructDefine,
"ffiStructAllocate" to ::ffiStructAllocate,
Expand Down Expand Up @@ -208,6 +210,10 @@ class FfiNativeProvider : ExpandedNativeProvider {
return structType.read(address, 0)
}

override fun provideNativeFunction(definitions: List<String>): NativeFunction {
throw RuntimeException("Invalid Native Function Usage")
}

companion object {
fun typeConversion(type: FfiType): Type = when (type) {
FfiPrimitiveType.UnsignedByte -> Type.UINT8
Expand Down
8 changes: 7 additions & 1 deletion ffi/src/main/kotlin/gay/pizza/pork/ffi/JavaNativeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import gay.pizza.pork.ast.gen.ArgumentSpec
import gay.pizza.pork.evaluator.CallableFunction
import gay.pizza.pork.evaluator.SlabContext
import gay.pizza.pork.evaluator.ExpandedNativeProvider
import gay.pizza.pork.execution.NativeFunction
import gay.pizza.pork.execution.NativeProvider
import gay.pizza.pork.execution.None
import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType

class JavaNativeProvider : ExpandedNativeProvider {
class JavaNativeProvider : ExpandedNativeProvider, NativeProvider {
private val lookup = MethodHandles.lookup()

override fun provideNativeFunction(
Expand Down Expand Up @@ -62,4 +64,8 @@ class JavaNativeProvider : ExpandedNativeProvider {
"static-setter" -> lookup.findStaticSetter(javaClass, symbol, returnType)
else -> throw RuntimeException("Unknown Handle Kind: $kind")
}

override fun provideNativeFunction(definitions: List<String>): NativeFunction {
throw RuntimeException("Invalid Native Function Usage")
}
}
4 changes: 4 additions & 0 deletions tool/src/main/kotlin/gay/pizza/pork/tool/RunCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import gay.pizza.dough.fs.PlatformFsProvider
import gay.pizza.pork.ast.gen.Symbol
import gay.pizza.pork.execution.InternalNativeProvider
import gay.pizza.pork.execution.NativeRegistry
import gay.pizza.pork.ffi.FfiNativeProvider
import gay.pizza.pork.ffi.JavaNativeProvider
import gay.pizza.pork.minimal.ExecutionType
import gay.pizza.pork.minimal.FileTool

Expand All @@ -26,6 +28,8 @@ class RunCommand : CliktCommand(help = "Run Program", name = "run") {
val tool = FileTool(PlatformFsProvider.resolve(path))
val nativeRegistry = NativeRegistry()
nativeRegistry.add("internal", InternalNativeProvider(quiet = quiet))
nativeRegistry.add("java", JavaNativeProvider())
nativeRegistry.add("ffi", FfiNativeProvider())
val main = tool.createExecutionContext(executionType, Symbol("main"), nativeRegistry)
maybeLoopAndMeasure(loop, measure) {
main.execute()
Expand Down

0 comments on commit 6e225aa

Please sign in to comment.