Interpreter - Refactored method extraction
This commit is contained in:
parent
d2b66db852
commit
ed704b1a63
1 changed files with 32 additions and 17 deletions
|
@ -197,26 +197,41 @@ impl StmtNode {
|
||||||
Ok(InterpreterFlowControl::default())
|
Ok(InterpreterFlowControl::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract methods from a class declaration
|
||||||
|
fn extract_methods<F>(
|
||||||
|
&self,
|
||||||
|
es: &mut InterpreterState,
|
||||||
|
decl: &ClassDecl,
|
||||||
|
filter: F,
|
||||||
|
) -> HashMap<String, Function>
|
||||||
|
where
|
||||||
|
F: FnMut(&ClassMemberDecl) -> Option<&FunDecl>,
|
||||||
|
{
|
||||||
|
decl.members
|
||||||
|
.iter()
|
||||||
|
.filter_map(filter)
|
||||||
|
.map(|fdecl| {
|
||||||
|
(
|
||||||
|
fdecl.name.lexeme.clone(),
|
||||||
|
Function::new(
|
||||||
|
Some(&fdecl.name),
|
||||||
|
&fdecl.params,
|
||||||
|
&fdecl.body,
|
||||||
|
es.environment.clone(),
|
||||||
|
fdecl.name.lexeme == "init",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<HashMap<String, Function>>()
|
||||||
|
}
|
||||||
|
|
||||||
/// Handle a class declaration
|
/// Handle a class declaration
|
||||||
fn on_class_decl(&self, es: &mut InterpreterState, decl: &ClassDecl) -> InterpreterResult {
|
fn on_class_decl(&self, es: &mut InterpreterState, decl: &ClassDecl) -> InterpreterResult {
|
||||||
es.environment.borrow_mut().define(&decl.name, None)?;
|
es.environment.borrow_mut().define(&decl.name, None)?;
|
||||||
let methods = decl
|
let methods = self.extract_methods(es, decl, |member| match member {
|
||||||
.members
|
ClassMemberDecl::Method(method) => Some(method),
|
||||||
.iter()
|
_ => None,
|
||||||
.filter_map(|member| match member {
|
});
|
||||||
ClassMemberDecl::Method(method) => Some((
|
|
||||||
method.name.lexeme.clone(),
|
|
||||||
Function::new(
|
|
||||||
Some(&method.name),
|
|
||||||
&method.params,
|
|
||||||
&method.body,
|
|
||||||
es.environment.clone(),
|
|
||||||
method.name.lexeme == "init",
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.collect::<HashMap<String, Function>>();
|
|
||||||
let class = Class::new(decl.name.lexeme.clone(), methods);
|
let class = Class::new(decl.name.lexeme.clone(), methods);
|
||||||
es.environment
|
es.environment
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
|
|
Loading…
Reference in a new issue