mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
Rename context to scope.
This commit is contained in:
parent
6494858f02
commit
7bec148d79
@ -3,19 +3,19 @@ package gay.pizza.pork.eval
|
||||
import gay.pizza.pork.ast.*
|
||||
import java.util.function.Function
|
||||
|
||||
class Evaluator(root: Context) : Visitor<Any> {
|
||||
private var currentContext: Context = root
|
||||
class Evaluator(root: Scope) : Visitor<Any> {
|
||||
private var currentScope: Scope = root
|
||||
|
||||
override fun visitDefine(node: Define): Any {
|
||||
val value = visit(node.value)
|
||||
currentContext.define(node.symbol.id, value)
|
||||
currentScope.define(node.symbol.id, value)
|
||||
return value
|
||||
}
|
||||
|
||||
override fun visitFunctionCall(node: FunctionCall): Any = currentContext.call(node.symbol.id)
|
||||
override fun visitFunctionCall(node: FunctionCall): Any = currentScope.call(node.symbol.id)
|
||||
|
||||
override fun visitReference(node: SymbolReference): Any =
|
||||
currentContext.value(node.symbol.id)
|
||||
currentScope.value(node.symbol.id)
|
||||
|
||||
override fun visitSymbol(node: Symbol): Any {
|
||||
return Unit
|
||||
@ -23,7 +23,7 @@ class Evaluator(root: Context) : Visitor<Any> {
|
||||
|
||||
override fun visitLambda(node: Lambda): Function<Any, Any> {
|
||||
return Function { _ ->
|
||||
currentContext = currentContext.fork()
|
||||
currentScope = currentScope.fork()
|
||||
try {
|
||||
var value: Any? = null
|
||||
for (expression in node.expressions) {
|
||||
@ -31,7 +31,7 @@ class Evaluator(root: Context) : Visitor<Any> {
|
||||
}
|
||||
value ?: Unit
|
||||
} finally {
|
||||
currentContext = currentContext.leave()
|
||||
currentScope = currentScope.leave()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package gay.pizza.pork.eval
|
||||
|
||||
import java.util.function.Function
|
||||
|
||||
class Context(val parent: Context? = null) {
|
||||
class Scope(val parent: Scope? = null) {
|
||||
private val variables = mutableMapOf<String, Any>()
|
||||
|
||||
fun define(name: String, value: Any) {
|
||||
@ -33,11 +33,11 @@ class Context(val parent: Context? = null) {
|
||||
return casted.apply(argument)
|
||||
}
|
||||
|
||||
fun fork(): Context {
|
||||
return Context(this)
|
||||
fun fork(): Scope {
|
||||
return Scope(this)
|
||||
}
|
||||
|
||||
fun leave(): Context {
|
||||
fun leave(): Scope {
|
||||
if (parent == null) {
|
||||
throw RuntimeException("Parent context not found.")
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package gay.pizza.pork
|
||||
|
||||
import gay.pizza.pork.ast.*
|
||||
import gay.pizza.pork.eval.Context
|
||||
import gay.pizza.pork.eval.Scope
|
||||
import gay.pizza.pork.eval.Evaluator
|
||||
import gay.pizza.pork.parse.*
|
||||
import kotlin.io.path.Path
|
||||
@ -9,10 +9,10 @@ import kotlin.io.path.readText
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
fun eval(ast: Program) {
|
||||
val context = Context()
|
||||
val evaluator = Evaluator(context)
|
||||
val scope = Scope()
|
||||
val evaluator = Evaluator(scope)
|
||||
evaluator.visit(ast)
|
||||
println("> ${context.call("main")}")
|
||||
println("> ${scope.call("main")}")
|
||||
}
|
||||
|
||||
val code = Path(args[0]).readText()
|
||||
|
Loading…
Reference in New Issue
Block a user