From 7bec148d7967d541ca159cee6035b57006c1434a Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Sat, 19 Aug 2023 16:52:40 -0700 Subject: [PATCH] Rename context to scope. --- src/main/kotlin/gay/pizza/pork/eval/Evaluator.kt | 14 +++++++------- .../gay/pizza/pork/eval/{Context.kt => Scope.kt} | 8 ++++---- src/main/kotlin/gay/pizza/pork/main.kt | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) rename src/main/kotlin/gay/pizza/pork/eval/{Context.kt => Scope.kt} (89%) diff --git a/src/main/kotlin/gay/pizza/pork/eval/Evaluator.kt b/src/main/kotlin/gay/pizza/pork/eval/Evaluator.kt index 8501750..6ef30d0 100644 --- a/src/main/kotlin/gay/pizza/pork/eval/Evaluator.kt +++ b/src/main/kotlin/gay/pizza/pork/eval/Evaluator.kt @@ -3,19 +3,19 @@ package gay.pizza.pork.eval import gay.pizza.pork.ast.* import java.util.function.Function -class Evaluator(root: Context) : Visitor { - private var currentContext: Context = root +class Evaluator(root: Scope) : Visitor { + 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 { override fun visitLambda(node: Lambda): Function { 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 { } value ?: Unit } finally { - currentContext = currentContext.leave() + currentScope = currentScope.leave() } } } diff --git a/src/main/kotlin/gay/pizza/pork/eval/Context.kt b/src/main/kotlin/gay/pizza/pork/eval/Scope.kt similarity index 89% rename from src/main/kotlin/gay/pizza/pork/eval/Context.kt rename to src/main/kotlin/gay/pizza/pork/eval/Scope.kt index c759853..2c2aee8 100644 --- a/src/main/kotlin/gay/pizza/pork/eval/Context.kt +++ b/src/main/kotlin/gay/pizza/pork/eval/Scope.kt @@ -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() 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.") } diff --git a/src/main/kotlin/gay/pizza/pork/main.kt b/src/main/kotlin/gay/pizza/pork/main.kt index a28201b..5aae9a9 100644 --- a/src/main/kotlin/gay/pizza/pork/main.kt +++ b/src/main/kotlin/gay/pizza/pork/main.kt @@ -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) { 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()