Interpreter - Store static methods inside classes
This commit is contained in:
parent
ed704b1a63
commit
75652b95cf
2 changed files with 12 additions and 2 deletions
|
@ -19,6 +19,7 @@ pub trait PropertyCarrier {
|
||||||
pub struct Class {
|
pub struct Class {
|
||||||
name: String,
|
name: String,
|
||||||
methods: HashMap<String, Function>,
|
methods: HashMap<String, Function>,
|
||||||
|
static_methods: HashMap<String, Function>,
|
||||||
fields: RefCell<HashMap<String, Value>>,
|
fields: RefCell<HashMap<String, Value>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,10 +48,15 @@ fn bind_method(method: &Function, this_value: Value) -> Function {
|
||||||
|
|
||||||
impl Class {
|
impl Class {
|
||||||
/// Create a new class, specifying its name.
|
/// 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 {
|
Self {
|
||||||
name,
|
name,
|
||||||
methods,
|
methods,
|
||||||
|
static_methods,
|
||||||
fields: RefCell::new(HashMap::default()),
|
fields: RefCell::new(HashMap::default()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,7 +232,11 @@ impl StmtNode {
|
||||||
ClassMemberDecl::Method(method) => Some(method),
|
ClassMemberDecl::Method(method) => Some(method),
|
||||||
_ => None,
|
_ => 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
|
es.environment
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.assign(&decl.name, class.into())?;
|
.assign(&decl.name, class.into())?;
|
||||||
|
|
Loading…
Reference in a new issue