Resolver - Refactored to work with the new class structure

This commit is contained in:
Emmanuel BENOîT 2023-01-12 08:41:04 +01:00
parent 0dbc631c96
commit 599652e98e

View file

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::{ use crate::{
ast::{ExprNode, FunDecl, ProgramNode, StmtNode, VariableExpr}, ast::{ClassMemberDecl, ExprNode, FunDecl, ProgramNode, StmtNode, VariableExpr},
errors::{ErrorKind, SloxError, SloxResult}, errors::{ErrorKind, SloxError, SloxResult},
tokens::Token, tokens::Token,
}; };
@ -266,20 +266,23 @@ where
} }
/// Process all method definitions in a class. /// Process all method definitions in a class.
fn resolve_class<'a, 'b>(rs: &mut ResolverState<'a>, methods: &'b [FunDecl]) -> ResolverResult fn resolve_class<'a, 'b>(
rs: &mut ResolverState<'a>,
methods: &'b [ClassMemberDecl],
) -> ResolverResult
where where
'b: 'a, 'b: 'a,
{ {
rs.define_this(); rs.define_this();
methods.iter().try_for_each(|method| { methods.iter().try_for_each(|member| match member {
rs.with_scope( ClassMemberDecl::Method(method) | ClassMemberDecl::StaticMethod(method) => rs.with_scope(
|rs| resolve_function(rs, &method.params, &method.body), |rs| resolve_function(rs, &method.params, &method.body),
if method.name.lexeme == "init" { if method.name.lexeme == "init" {
ScopeType::Initializer ScopeType::Initializer
} else { } else {
ScopeType::Method ScopeType::Method
}, },
) ),
}) })
} }
@ -343,7 +346,7 @@ impl VarResolver for StmtNode {
StmtNode::ClassDecl(decl) => { StmtNode::ClassDecl(decl) => {
rs.declare(&decl.name, SymKind::Class)?; rs.declare(&decl.name, SymKind::Class)?;
rs.define(&decl.name); rs.define(&decl.name);
rs.with_scope(|rs| resolve_class(rs, &decl.methods), rs.current_type()) rs.with_scope(|rs| resolve_class(rs, &decl.members), rs.current_type())
} }
StmtNode::If { StmtNode::If {