Add type aliases for registries
Introduces `GlobalFunctionRegistry` and `GlobalAnalyzedRegistry` type aliases to clarify the usage of `HashMap`s for storing global functions and their analyzed versions. This change also refactors the `LambdaCollector` and `Optimizer` to use these new aliases, improving code readability and maintainability.
This commit is contained in:
+14
-11
@@ -8,7 +8,10 @@ use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, BoundNode, GlobalIdx};
|
||||
use crate::ast::compiler::bound_nodes::{
|
||||
Address, AnalyzedNode, BoundKind, BoundNode, GlobalAnalyzedRegistry, GlobalFunctionRegistry,
|
||||
GlobalIdx,
|
||||
};
|
||||
use crate::ast::compiler::dumper::Dumper;
|
||||
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
|
||||
@@ -69,8 +72,8 @@ pub struct Environment {
|
||||
pub global_types: Rc<RefCell<HashMap<GlobalIdx, StaticType>>>,
|
||||
pub global_purity: Rc<RefCell<HashMap<GlobalIdx, Purity>>>,
|
||||
pub global_values: Rc<RefCell<Vec<Value>>>,
|
||||
pub function_registry: Rc<RefCell<HashMap<GlobalIdx, BoundNode>>>,
|
||||
pub typed_function_registry: Rc<RefCell<HashMap<GlobalIdx, AnalyzedNode>>>,
|
||||
pub function_registry: Rc<RefCell<GlobalFunctionRegistry>>,
|
||||
pub typed_function_registry: Rc<RefCell<GlobalAnalyzedRegistry>>,
|
||||
pub monomorph_cache: Rc<RefCell<MonoCache>>,
|
||||
pub debug_mode: bool,
|
||||
pub optimization: bool,
|
||||
@@ -79,19 +82,19 @@ pub struct Environment {
|
||||
}
|
||||
|
||||
struct EnvFunctionRegistry {
|
||||
registry: Rc<RefCell<HashMap<GlobalIdx, BoundNode>>>,
|
||||
analyzed_registry: Rc<RefCell<HashMap<GlobalIdx, AnalyzedNode>>>,
|
||||
registry: Rc<RefCell<GlobalFunctionRegistry>>,
|
||||
analyzed_registry: Rc<RefCell<GlobalAnalyzedRegistry>>,
|
||||
}
|
||||
|
||||
impl FunctionRegistry for EnvFunctionRegistry {
|
||||
fn resolve(&self, addr: Address) -> Option<BoundNode> {
|
||||
fn resolve(&self, addr: Address) -> Option<Rc<BoundNode>> {
|
||||
if let Address::Global(idx) = addr {
|
||||
self.registry.borrow().get(&idx).cloned()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn resolve_analyzed(&self, addr: Address) -> Option<AnalyzedNode> {
|
||||
fn resolve_analyzed(&self, addr: Address) -> Option<Rc<AnalyzedNode>> {
|
||||
if let Address::Global(idx) = addr {
|
||||
self.analyzed_registry.borrow().get(&idx).cloned()
|
||||
} else {
|
||||
@@ -122,7 +125,7 @@ impl MacroEvaluator for RuntimeMacroEvaluator {
|
||||
let (bound_ast, captures) = Binder::bind_root(self.global_names.clone(), node, &mut diag)?;
|
||||
let bound_ast = crate::ast::compiler::captures::CapturePass::apply(bound_ast, &captures);
|
||||
let checker = TypeChecker::new(self.global_types.clone());
|
||||
let typed_ast = checker.check(bound_ast, &[], &mut diag);
|
||||
let typed_ast = checker.check(&bound_ast, &[], &mut diag);
|
||||
|
||||
if diag.has_errors() {
|
||||
return Err(diag
|
||||
@@ -316,7 +319,7 @@ impl Environment {
|
||||
LambdaCollector::collect(&bound_ast, &mut self.function_registry.borrow_mut());
|
||||
|
||||
let checker = TypeChecker::new(self.global_types.clone());
|
||||
let typed_ast = checker.check(bound_ast, &[], &mut diagnostics);
|
||||
let typed_ast = checker.check(&bound_ast, &[], &mut diagnostics);
|
||||
|
||||
CompilationResult {
|
||||
ast: Some(typed_ast),
|
||||
@@ -420,12 +423,12 @@ impl Environment {
|
||||
let optimization = self.optimization;
|
||||
|
||||
let compiler = Rc::new(
|
||||
move |func_template: BoundNode,
|
||||
move |func_template: Rc<BoundNode>,
|
||||
arg_types: &[StaticType]|
|
||||
-> Result<(Value, StaticType), String> {
|
||||
let mut diag = Diagnostics::new();
|
||||
let checker = TypeChecker::new(global_types.clone());
|
||||
let retyped_ast = checker.check(func_template, arg_types, &mut diag);
|
||||
let retyped_ast = checker.check(func_template.as_ref(), arg_types, &mut diag);
|
||||
|
||||
if diag.has_errors() {
|
||||
return Err(diag
|
||||
|
||||
Reference in New Issue
Block a user