Interpreter - Store static methods inside classes

This commit is contained in:
Emmanuel BENOîT 2023-01-13 08:16:26 +01:00
parent ed704b1a63
commit 75652b95cf
2 changed files with 12 additions and 2 deletions

View file

@ -19,6 +19,7 @@ pub trait PropertyCarrier {
pub struct Class {
name: String,
methods: HashMap<String, Function>,
static_methods: HashMap<String, Function>,
fields: RefCell<HashMap<String, Value>>,
}
@ -47,10 +48,15 @@ fn bind_method(method: &Function, this_value: Value) -> Function {
impl Class {
/// Create a new class, specifying its name.
pub fn new(name: String, methods: HashMap<String, Function>) -> Self {
pub fn new(
name: String,
methods: HashMap<String, Function>,
static_methods: HashMap<String, Function>,
) -> Self {
Self {
name,
methods,
static_methods,
fields: RefCell::new(HashMap::default()),
}
}

View file

@ -232,7 +232,11 @@ impl StmtNode {
ClassMemberDecl::Method(method) => Some(method),
_ => None,
});
let class = Class::new(decl.name.lexeme.clone(), methods);
let static_methods = self.extract_methods(es, decl, |member| match member {
ClassMemberDecl::StaticMethod(method) => Some(method),
_ => None,
});
let class = Class::new(decl.name.lexeme.clone(), methods, static_methods);
es.environment
.borrow_mut()
.assign(&decl.name, class.into())?;