Skip to content

Commit

Permalink
evaluator: functions now use their own evaluation visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 5, 2023
1 parent 9634a73 commit 63a90a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ class EvaluationContext(
val evaluationContextProvider: EvaluationContextProvider,
rootScope: Scope
) {
private var isAlreadySetup = false

val internalRootScope = rootScope.fork()
val externalRootScope = rootScope.fork()

private val evaluationVisitor = EvaluationVisitor(internalRootScope)
private var initialized = false

fun setup() {
if (isAlreadySetup) {
fun init() {
if (initialized) {
return
}
isAlreadySetup = true
initialized = true
val imports = compilationUnit.declarations.filterIsInstance<ImportDeclaration>()
for (import in imports) {
val evaluationContext = evaluationContextProvider.provideEvaluationContext(import.path.text)
internalRootScope.inherit(evaluationContext.externalRootScope)
}

for (definition in compilationUnit.definitions) {
val evaluationVisitor = EvaluationVisitor(internalRootScope)
evaluationVisitor.visit(definition)
if (definition.modifiers.export) {
externalRootScope.define(definition.symbol.id, internalRootScope.value(definition.symbol.id))
if (!definition.modifiers.export) {
continue
}
val internalValue = internalRootScope.value(definition.symbol.id)
externalRootScope.define(definition.symbol.id, internalValue)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,14 @@ class EvaluationVisitor(val root: Scope) : NodeVisitor<Any> {
}

override fun visitFunctionDefinition(node: FunctionDefinition): Any {
val blockFunction = visitBlock(node.block) as BlockFunction
val function = CallableFunction { arguments ->
val formerScope = currentScope
currentScope = root.fork()
for ((index, argumentSymbol) in node.arguments.withIndex()) {
currentScope.define(argumentSymbol.id, arguments.values[index])
}
try {
return@CallableFunction blockFunction.call()
} finally {
currentScope = formerScope
}
val visitor = EvaluationVisitor(currentScope)
val blockFunction = visitor.visitBlock(node.block) as BlockFunction
return@CallableFunction blockFunction.call()
}
currentScope.define(node.symbol.id, function)
return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Evaluator(val world: World, val scope: Scope) : EvaluationContextProvider
val context = contexts.computeIfAbsent(identity) {
EvaluationContext(unit, this, scope)
}
context.setup()
context.init()
return context
}
}

0 comments on commit 63a90a5

Please sign in to comment.