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