evaluator: inherit fast cache for calls to functions in a function definition

This commit is contained in:
Alex Zenla 2023-09-05 01:43:59 -07:00
parent 65f61fcd90
commit 2631b6752b
Signed by: alex
GPG Key ID: C0780728420EBFE5

View File

@ -4,7 +4,6 @@ import gay.pizza.pork.ast.*
class EvaluationVisitor(root: Scope) : NodeVisitor<Any> { class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
private var currentScope: Scope = root private var currentScope: Scope = root
private var insideFastCachePreservation = false
override fun visitIntLiteral(node: IntLiteral): Any = node.value override fun visitIntLiteral(node: IntLiteral): Any = node.value
override fun visitStringLiteral(node: StringLiteral): Any = node.text override fun visitStringLiteral(node: StringLiteral): Any = node.text
@ -29,7 +28,7 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
override fun visitLambda(node: Lambda): CallableFunction { override fun visitLambda(node: Lambda): CallableFunction {
return CallableFunction { arguments -> return CallableFunction { arguments ->
currentScope = currentScope.fork(inheritFastCache = insideFastCachePreservation) currentScope = currentScope.fork()
for ((index, argumentSymbol) in node.arguments.withIndex()) { for ((index, argumentSymbol) in node.arguments.withIndex()) {
currentScope.define(argumentSymbol.id, arguments.values[index]) currentScope.define(argumentSymbol.id, arguments.values[index])
} }
@ -105,7 +104,7 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
override fun visitFunctionDefinition(node: FunctionDefinition): Any { override fun visitFunctionDefinition(node: FunctionDefinition): Any {
val blockFunction = visitBlock(node.block) as BlockFunction val blockFunction = visitBlock(node.block) as BlockFunction
val function = CallableFunction { arguments -> val function = CallableFunction { arguments ->
currentScope = currentScope.fork(inheritFastCache = insideFastCachePreservation) currentScope = currentScope.fork(inheritFastCache = true)
currentScope.fastVariableCache.put(node.symbol.id, currentScope.value(node.symbol.id)) currentScope.fastVariableCache.put(node.symbol.id, currentScope.value(node.symbol.id))
for ((index, argumentSymbol) in node.arguments.withIndex()) { for ((index, argumentSymbol) in node.arguments.withIndex()) {
currentScope.define(argumentSymbol.id, arguments.values[index]) currentScope.define(argumentSymbol.id, arguments.values[index])