Add function objects
parent
f24f8729e0
commit
20033fd74a
@ -0,0 +1,27 @@
|
||||
package fr.celticinfo.lox
|
||||
|
||||
class LoxFunction : LoxCallable {
|
||||
private val declaration: Function
|
||||
|
||||
constructor(declaration: Function) {
|
||||
this.declaration = declaration
|
||||
}
|
||||
|
||||
override fun call(interpreter: Interpreter, arguments: List<Any?>): Any? {
|
||||
val environment = Environment(interpreter.globals)
|
||||
for (i in declaration.params.indices) {
|
||||
environment.define(declaration.params[i].lexeme, arguments[i])
|
||||
}
|
||||
interpreter.executeBlock(declaration.body, environment)
|
||||
return null
|
||||
}
|
||||
|
||||
override fun arity(): Int {
|
||||
return declaration.params.size
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "<fn ${declaration.name.lexeme}>"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue