mirror of
				https://github.com/GayPizzaSpecifications/pork.git
				synced 2025-11-03 17:39:38 +00:00 
			
		
		
		
	evaluator: functions now use their own evaluation visitor
This commit is contained in:
		@ -8,18 +8,16 @@ 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)
 | 
			
		||||
@ -27,10 +25,13 @@ class EvaluationContext(
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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 {
 | 
			
		||||
      val visitor = EvaluationVisitor(currentScope)
 | 
			
		||||
      val blockFunction = visitor.visitBlock(node.block) as BlockFunction
 | 
			
		||||
      return@CallableFunction blockFunction.call()
 | 
			
		||||
      } finally {
 | 
			
		||||
        currentScope = formerScope
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    currentScope.define(node.symbol.id, function)
 | 
			
		||||
    return None
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user