mirror of
				https://github.com/GayPizzaSpecifications/pork.git
				synced 2025-11-03 17:39:38 +00:00 
			
		
		
		
	evaluator: cache native function resolution
This commit is contained in:
		@ -3,18 +3,25 @@ package gay.pizza.pork.evaluator
 | 
				
			|||||||
import gay.pizza.pork.ast.FunctionDefinition
 | 
					import gay.pizza.pork.ast.FunctionDefinition
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class FunctionContext(val compilationUnitContext: CompilationUnitContext, val node: FunctionDefinition) : CallableFunction {
 | 
					class FunctionContext(val compilationUnitContext: CompilationUnitContext, val node: FunctionDefinition) : CallableFunction {
 | 
				
			||||||
  override fun call(arguments: Arguments): Any {
 | 
					  private fun resolveMaybeNative(): CallableFunction? = if (node.native == null) {
 | 
				
			||||||
    val scope = compilationUnitContext.internalScope.fork()
 | 
					    null
 | 
				
			||||||
    for ((index, argumentSymbol) in node.arguments.withIndex()) {
 | 
					  } else {
 | 
				
			||||||
      scope.define(argumentSymbol.id, arguments.values[index])
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (node.native != null) {
 | 
					 | 
				
			||||||
    val native = node.native!!
 | 
					    val native = node.native!!
 | 
				
			||||||
    val nativeFunctionProvider =
 | 
					    val nativeFunctionProvider =
 | 
				
			||||||
      compilationUnitContext.evaluator.nativeFunctionProvider(native.form.id)
 | 
					      compilationUnitContext.evaluator.nativeFunctionProvider(native.form.id)
 | 
				
			||||||
      val nativeFunction = nativeFunctionProvider.provideNativeFunction(native.definition.text)
 | 
					    nativeFunctionProvider.provideNativeFunction(native.definition.text)
 | 
				
			||||||
      return nativeFunction.call(arguments)
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private val nativeCached by lazy { resolveMaybeNative() }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  override fun call(arguments: Arguments): Any {
 | 
				
			||||||
 | 
					    if (nativeCached != null) {
 | 
				
			||||||
 | 
					      return nativeCached!!.call(arguments)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    val scope = compilationUnitContext.internalScope.fork()
 | 
				
			||||||
 | 
					    for ((index, argumentSymbol) in node.arguments.withIndex()) {
 | 
				
			||||||
 | 
					      scope.define(argumentSymbol.id, arguments.values[index])
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (node.block == null) {
 | 
					    if (node.block == null) {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										10
									
								
								examples/java-speed.pork
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								examples/java-speed.pork
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					import java java.lang.Math
 | 
				
			||||||
 | 
					import java java.lang.System
 | 
				
			||||||
 | 
					import java java.io.PrintStream
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export func main() {
 | 
				
			||||||
 | 
					  while true {
 | 
				
			||||||
 | 
					    let pi = java_lang_Math_PI_get()
 | 
				
			||||||
 | 
					    println(pi)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user