-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global: a working virtual machine for some of the use cases. APIs and…
… validation still WIP.
- Loading branch information
Showing
53 changed files
with
432 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/AdaptedNativeProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package gay.pizza.pork.evaluator | ||
|
||
import gay.pizza.pork.ast.gen.ArgumentSpec | ||
import gay.pizza.pork.execution.NativeProvider | ||
|
||
class AdaptedNativeProvider(val provider: NativeProvider) : ExpandedNativeProvider { | ||
override fun provideNativeFunction( | ||
definitions: List<String>, | ||
arguments: List<ArgumentSpec>, | ||
inside: SlabContext | ||
): CallableFunction { | ||
val function = provider.provideNativeFunction(definitions) | ||
return CallableFunction { args, _ -> function.invoke(args) } | ||
} | ||
} |
3 changes: 0 additions & 3 deletions
3
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/Arguments.kt
This file was deleted.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/CallableFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package gay.pizza.pork.evaluator | ||
|
||
import gay.pizza.pork.execution.ArgumentList | ||
|
||
fun interface CallableFunction { | ||
fun call(arguments: ArgumentList, stack: CallStack): Any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 7 additions & 16 deletions
23
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/Evaluator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,36 @@ | ||
package gay.pizza.pork.evaluator | ||
|
||
import gay.pizza.pork.ast.gen.Symbol | ||
import gay.pizza.pork.execution.ExecutionContext | ||
import gay.pizza.pork.execution.ExecutionContextProvider | ||
import gay.pizza.pork.frontend.ImportLocator | ||
import gay.pizza.pork.frontend.Slab | ||
import gay.pizza.pork.frontend.World | ||
|
||
class Evaluator(val world: World) : ExecutionContextProvider { | ||
class Evaluator(val world: World) { | ||
private val scope = Scope.root() | ||
private val contexts = mutableMapOf<Slab, SlabContext>() | ||
private val nativeProviders = mutableMapOf<String, NativeProvider>() | ||
private val nativeProviders = mutableMapOf<String, ExpandedNativeProvider>() | ||
|
||
fun evaluate(locator: ImportLocator): Scope { | ||
val slabContext = context(locator) | ||
val slabContext = slabContext(locator) | ||
slabContext.finalizeScope() | ||
return slabContext.externalScope | ||
} | ||
|
||
fun context(slab: Slab): SlabContext { | ||
fun slabContext(slab: Slab): SlabContext { | ||
val slabContext = contexts.computeIfAbsent(slab) { | ||
SlabContext(slab, this, scope) | ||
} | ||
slabContext.ensureImportedContextsExist() | ||
return slabContext | ||
} | ||
|
||
fun context(locator: ImportLocator): SlabContext = context(world.load(locator)) | ||
fun slabContext(locator: ImportLocator): SlabContext = slabContext(world.load(locator)) | ||
|
||
fun nativeFunctionProvider(form: String): NativeProvider { | ||
fun nativeFunctionProvider(form: String): ExpandedNativeProvider { | ||
return nativeProviders[form] ?: | ||
throw RuntimeException("Unknown native function form: $form") | ||
} | ||
|
||
fun addNativeProvider(form: String, nativeProvider: NativeProvider) { | ||
fun addNativeProvider(form: String, nativeProvider: ExpandedNativeProvider) { | ||
nativeProviders[form] = nativeProvider | ||
} | ||
|
||
override fun prepare(importLocator: ImportLocator, entryPointSymbol: Symbol): ExecutionContext { | ||
val slab = context(importLocator) | ||
slab.finalizeScope() | ||
return EvaluatorExecutionContext(this, slab, entryPointSymbol) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/EvaluatorProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package gay.pizza.pork.evaluator | ||
|
||
import gay.pizza.pork.ast.gen.Symbol | ||
import gay.pizza.pork.execution.ExecutionContext | ||
import gay.pizza.pork.execution.ExecutionContextProvider | ||
import gay.pizza.pork.execution.NativeRegistry | ||
import gay.pizza.pork.frontend.ImportLocator | ||
import gay.pizza.pork.frontend.World | ||
|
||
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)) | ||
} | ||
val slab = evaluator.slabContext(importLocator) | ||
slab.finalizeScope() | ||
return EvaluatorExecutionContext(evaluator, slab, entryPointSymbol) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/FunctionContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/InternalNativeProvider.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
evaluator/src/main/kotlin/gay/pizza/pork/evaluator/ValueStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
export func main() { | ||
var x = 1 | ||
while x <= 5 { | ||
if x == 3 { | ||
println("The value is 3") | ||
} else { | ||
println("The value is not 3") | ||
} | ||
println(x) | ||
x++ | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
execution/src/main/kotlin/gay/pizza/pork/execution/ArgumentList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package gay.pizza.pork.execution | ||
|
||
typealias ArgumentList = List<Any> | ||
|
||
inline fun <reified T> ArgumentList.at(index: Int): T = this[index] as T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
execution/src/main/kotlin/gay/pizza/pork/execution/InternalNativeProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package gay.pizza.pork.execution | ||
|
||
class InternalNativeProvider(val quiet: Boolean = false) : NativeProvider { | ||
private val functions = mutableMapOf( | ||
"print" to NativeFunction(::printValues), | ||
"println" to NativeFunction(::printLine), | ||
"listSet" to NativeFunction(::setInList), | ||
"listInitWith" to NativeFunction(::listInitWith) | ||
) | ||
|
||
fun add(name: String, function: NativeFunction) { | ||
functions[name] = function | ||
} | ||
|
||
override fun provideNativeFunction(definitions: List<String>): NativeFunction { | ||
val definition = definitions[0] | ||
return functions[definition] ?: | ||
throw RuntimeException("Unknown internal function: $definition") | ||
} | ||
|
||
private fun printValues(arguments: ArgumentList): Any { | ||
if (quiet || arguments.isEmpty()) return None | ||
print(arguments.at<List<*>>(0).joinToString(" ") { it.toString() }) | ||
return None | ||
} | ||
|
||
private fun printLine(arguments: ArgumentList): Any { | ||
if (quiet) return None | ||
println(arguments.at<List<*>>(0).joinToString(" ") { it.toString() }) | ||
return Unit | ||
} | ||
|
||
private fun setInList(arguments: ArgumentList): Any { | ||
@Suppress("UNCHECKED_CAST") | ||
val list = arguments[0] as MutableList<Any> | ||
val value = arguments[2] | ||
list[(arguments.at<Number>(0)).toInt()] = value | ||
return value | ||
} | ||
|
||
private fun listInitWith(arguments: ArgumentList): Any { | ||
val size = arguments.at<Number>(0).toInt() | ||
return MutableList(size) { arguments[1] } | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
execution/src/main/kotlin/gay/pizza/pork/execution/NativeFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package gay.pizza.pork.execution | ||
|
||
fun interface NativeFunction { | ||
fun invoke(args: ArgumentList): Any | ||
} |
5 changes: 5 additions & 0 deletions
5
execution/src/main/kotlin/gay/pizza/pork/execution/NativeProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package gay.pizza.pork.execution | ||
|
||
interface NativeProvider { | ||
fun provideNativeFunction(definitions: List<String>): NativeFunction | ||
} |
18 changes: 18 additions & 0 deletions
18
execution/src/main/kotlin/gay/pizza/pork/execution/NativeRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package gay.pizza.pork.execution | ||
|
||
class NativeRegistry { | ||
private val providers = mutableMapOf<String, NativeProvider>() | ||
|
||
fun add(form: String, provider: NativeProvider) { | ||
providers[form] = provider | ||
} | ||
|
||
fun forEachProvider(block: (String, NativeProvider) -> Unit) { | ||
for ((form, provider) in providers) { | ||
block(form, provider) | ||
} | ||
} | ||
|
||
fun of(form: String): NativeProvider = | ||
providers[form] ?: throw RuntimeException("Unknown native form: ${form}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package gay.pizza.pork.execution | ||
|
||
data object None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.