Interpreter - Access methods through get expressions

This commit is contained in:
Emmanuel BENOîT 2023-01-09 07:32:50 +01:00
parent e371217df0
commit 58c1bbcf68

View file

@ -71,14 +71,18 @@ impl Instance {
} }
pub(super) fn get(&self, name: &Token) -> SloxResult<Value> { pub(super) fn get(&self, name: &Token) -> SloxResult<Value> {
match self.fields.get(&name.lexeme) { if let Some(value) = self.fields.get(&name.lexeme) {
Some(value) => Ok(value.clone()), return Ok(value.clone());
None => Err(SloxError::with_token( }
if let Some(method) = self.class.borrow().methods.get(&name.lexeme) {
return Ok(Value::from(method.clone()));
}
Err(SloxError::with_token(
ErrorKind::Runtime, ErrorKind::Runtime,
name, name,
"undefined property".to_owned(), "undefined property".to_owned(),
)), ))
}
} }
pub(super) fn set(&mut self, name: &Token, value: Value) { pub(super) fn set(&mut self, name: &Token, value: Value) {