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,
 | 
					  val evaluationContextProvider: EvaluationContextProvider,
 | 
				
			||||||
  rootScope: Scope
 | 
					  rootScope: Scope
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
  private var isAlreadySetup = false
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  val internalRootScope = rootScope.fork()
 | 
					  val internalRootScope = rootScope.fork()
 | 
				
			||||||
  val externalRootScope = rootScope.fork()
 | 
					  val externalRootScope = rootScope.fork()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private val evaluationVisitor = EvaluationVisitor(internalRootScope)
 | 
					  private var initialized = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  fun setup() {
 | 
					  fun init() {
 | 
				
			||||||
    if (isAlreadySetup) {
 | 
					    if (initialized) {
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    isAlreadySetup = true
 | 
					    initialized = true
 | 
				
			||||||
    val imports = compilationUnit.declarations.filterIsInstance<ImportDeclaration>()
 | 
					    val imports = compilationUnit.declarations.filterIsInstance<ImportDeclaration>()
 | 
				
			||||||
    for (import in imports) {
 | 
					    for (import in imports) {
 | 
				
			||||||
      val evaluationContext = evaluationContextProvider.provideEvaluationContext(import.path.text)
 | 
					      val evaluationContext = evaluationContextProvider.provideEvaluationContext(import.path.text)
 | 
				
			||||||
@ -27,10 +25,13 @@ class EvaluationContext(
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (definition in compilationUnit.definitions) {
 | 
					    for (definition in compilationUnit.definitions) {
 | 
				
			||||||
 | 
					      val evaluationVisitor = EvaluationVisitor(internalRootScope)
 | 
				
			||||||
      evaluationVisitor.visit(definition)
 | 
					      evaluationVisitor.visit(definition)
 | 
				
			||||||
      if (definition.modifiers.export) {
 | 
					      if (!definition.modifiers.export) {
 | 
				
			||||||
        externalRootScope.define(definition.symbol.id, internalRootScope.value(definition.symbol.id))
 | 
					        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 {
 | 
					  override fun visitFunctionDefinition(node: FunctionDefinition): Any {
 | 
				
			||||||
    val blockFunction = visitBlock(node.block) as BlockFunction
 | 
					 | 
				
			||||||
    val function = CallableFunction { arguments ->
 | 
					    val function = CallableFunction { arguments ->
 | 
				
			||||||
      val formerScope = currentScope
 | 
					 | 
				
			||||||
      currentScope = root.fork()
 | 
					      currentScope = root.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])
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      try {
 | 
					      val visitor = EvaluationVisitor(currentScope)
 | 
				
			||||||
 | 
					      val blockFunction = visitor.visitBlock(node.block) as BlockFunction
 | 
				
			||||||
      return@CallableFunction blockFunction.call()
 | 
					      return@CallableFunction blockFunction.call()
 | 
				
			||||||
      } finally {
 | 
					 | 
				
			||||||
        currentScope = formerScope
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    currentScope.define(node.symbol.id, function)
 | 
					    currentScope.define(node.symbol.id, function)
 | 
				
			||||||
    return None
 | 
					    return None
 | 
				
			||||||
 | 
				
			|||||||
@ -16,7 +16,7 @@ class Evaluator(val world: World, val scope: Scope) : EvaluationContextProvider
 | 
				
			|||||||
    val context = contexts.computeIfAbsent(identity) {
 | 
					    val context = contexts.computeIfAbsent(identity) {
 | 
				
			||||||
      EvaluationContext(unit, this, scope)
 | 
					      EvaluationContext(unit, this, scope)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    context.setup()
 | 
					    context.init()
 | 
				
			||||||
    return context
 | 
					    return context
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user