Add properties on instances
parent
3e9fca6593
commit
22a4d86160
@ -1,5 +1,19 @@
|
|||||||
package fr.celticinfo.lox
|
package fr.celticinfo.lox
|
||||||
|
|
||||||
class LoxInstance(private val klass: LoxClass) {
|
class LoxInstance(private val klass: LoxClass) {
|
||||||
|
private val fields = mutableMapOf<String, Any?>()
|
||||||
|
|
||||||
|
fun get(name: Token): Any? {
|
||||||
|
if (fields.containsKey(name.lexeme)) {
|
||||||
|
return fields[name.lexeme]
|
||||||
|
}
|
||||||
|
|
||||||
|
throw RuntimeError(name, "Undefined property '${name.lexeme}'.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun set(name: Token, value: Any?) {
|
||||||
|
fields[name.lexeme] = value
|
||||||
|
}
|
||||||
|
|
||||||
override fun toString() = "$klass instance"
|
override fun toString() = "$klass instance"
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue