Interpreter - Set instance properties

This commit is contained in:
Emmanuel BENOîT 2023-01-09 07:10:10 +01:00
parent 7634354122
commit 5cfe6d3e5c
2 changed files with 23 additions and 1 deletions

View file

@ -79,6 +79,10 @@ impl Instance {
)),
}
}
pub(super) fn set(&mut self, name: &Token, value: Value) {
self.fields.insert(name.lexeme.clone(), value);
}
}
impl Display for Instance {

View file

@ -1,7 +1,7 @@
use std::{cell::RefCell, rc::Rc};
use crate::{
ast::{ClassDecl, ExprNode, FunDecl, GetExpr, ProgramNode, StmtNode},
ast::{ClassDecl, ExprNode, FunDecl, GetExpr, ProgramNode, SetExpr, StmtNode},
errors::{ErrorKind, SloxError, SloxResult},
resolver::ResolvedVariables,
tokens::{Token, TokenType},
@ -342,6 +342,7 @@ impl Interpretable for ExprNode {
Ok(Value::from(lambda).into())
}
ExprNode::Get(get_expr) => self.on_get_expression(es, get_expr),
ExprNode::Set(set_expr) => self.on_set_expression(es, set_expr),
}
}
}
@ -523,4 +524,21 @@ impl ExprNode {
|| error(&get_expr.name, "only instances have properties"),
)
}
/// Evaluate a set expression.
fn on_set_expression(
&self,
itpr_state: &mut InterpreterState,
set_expr: &SetExpr,
) -> InterpreterResult {
let instance = set_expr.instance.interpret(itpr_state)?.result();
instance.with_instance_mut(
|instance| {
let value = set_expr.value.interpret(itpr_state)?.result();
instance.set(&set_expr.name, value.clone());
Ok(value.into())
},
|| error(&set_expr.name, "only instances have properties"),
)
}
}