You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
846 B
Kotlin

package fr.celticinfo.lox
/**
* The LoxClass class represents a class in the Lox language.
*/
class LoxClass(private val name: String, private val superClass: LoxClass?, private val methods: Map<String, LoxFunction>) : LoxCallable {
override fun call(interpreter: Interpreter, arguments: List<Any?>): Any? {
val instance = LoxInstance(this)
val initializer = findMethod("init")
initializer?.bind(instance)?.call(interpreter, arguments)
return instance
}
fun findMethod(name: String): LoxFunction? {
if (methods.containsKey(name)) {
return methods[name]
}
if (superClass != null) {
return superClass.findMethod(name)
}
return null
}
override fun arity() = findMethod("init")?.arity() ?: 0
override fun toString() = name
}