Refactor address types to use newtype wrappers
This commit introduces newtype wrappers for `LocalSlot`, `UpvalueIdx`, and `GlobalIdx` to improve type safety and clarity. These wrappers replace direct use of `u32` for indices, making the code more robust and easier to understand. The changes include: - Defining `LocalSlot`, `UpvalueIdx`, and `GlobalIdx` structs. - Implementing `Display` for these new types to provide user-friendly output. - Updating the `Address` enum to use these new types. - Modifying various compiler components (analyzer, binder, optimizer, type checker, environment, VM) to use the new types. - Adjusting tests to accommodate the changes.
This commit is contained in:
+12
-12
@@ -8,7 +8,7 @@ use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, BoundNode};
|
||||
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, BoundNode, GlobalIdx};
|
||||
use crate::ast::compiler::dumper::Dumper;
|
||||
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||
use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry};
|
||||
@@ -22,21 +22,21 @@ use crate::ast::types::{
|
||||
};
|
||||
|
||||
pub struct Environment {
|
||||
pub global_names: Rc<RefCell<HashMap<Symbol, (u32, Identity)>>>,
|
||||
pub global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
pub global_purity: Rc<RefCell<HashMap<u32, Purity>>>,
|
||||
pub global_names: Rc<RefCell<HashMap<Symbol, (GlobalIdx, Identity)>>>,
|
||||
pub global_types: Rc<RefCell<HashMap<GlobalIdx, StaticType>>>,
|
||||
pub global_purity: Rc<RefCell<HashMap<GlobalIdx, Purity>>>,
|
||||
pub global_values: Rc<RefCell<Vec<Value>>>,
|
||||
pub prng: Rc<RefCell<fastrand::Rng>>,
|
||||
pub function_registry: Rc<RefCell<HashMap<u32, BoundNode>>>,
|
||||
pub typed_function_registry: Rc<RefCell<HashMap<u32, AnalyzedNode>>>,
|
||||
pub function_registry: Rc<RefCell<HashMap<GlobalIdx, BoundNode>>>,
|
||||
pub typed_function_registry: Rc<RefCell<HashMap<GlobalIdx, AnalyzedNode>>>,
|
||||
pub monomorph_cache: Rc<RefCell<MonoCache>>,
|
||||
pub debug_mode: bool,
|
||||
pub optimization: bool,
|
||||
}
|
||||
|
||||
struct EnvFunctionRegistry {
|
||||
registry: Rc<RefCell<HashMap<u32, BoundNode>>>,
|
||||
analyzed_registry: Rc<RefCell<HashMap<u32, AnalyzedNode>>>,
|
||||
registry: Rc<RefCell<HashMap<GlobalIdx, BoundNode>>>,
|
||||
analyzed_registry: Rc<RefCell<HashMap<GlobalIdx, AnalyzedNode>>>,
|
||||
}
|
||||
|
||||
impl FunctionRegistry for EnvFunctionRegistry {
|
||||
@@ -57,8 +57,8 @@ impl FunctionRegistry for EnvFunctionRegistry {
|
||||
}
|
||||
|
||||
struct RuntimeMacroEvaluator {
|
||||
global_names: Rc<RefCell<HashMap<Symbol, (u32, Identity)>>>,
|
||||
global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
global_names: Rc<RefCell<HashMap<Symbol, (GlobalIdx, Identity)>>>,
|
||||
global_types: Rc<RefCell<HashMap<GlobalIdx, StaticType>>>,
|
||||
global_values: Rc<RefCell<Vec<Value>>>,
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ impl Environment {
|
||||
let mut values = self.global_values.borrow_mut();
|
||||
let mut purity = self.global_purity.borrow_mut();
|
||||
|
||||
let idx = values.len() as u32;
|
||||
let idx = GlobalIdx(values.len() as u32);
|
||||
let identity = NodeIdentity::new(SourceLocation { line: 0, col: 0 });
|
||||
names.insert(Symbol::from(name), (idx, identity));
|
||||
types.insert(idx, ty);
|
||||
@@ -164,7 +164,7 @@ impl Environment {
|
||||
let mut values = self.global_values.borrow_mut();
|
||||
let mut purity = self.global_purity.borrow_mut();
|
||||
|
||||
let idx = values.len() as u32;
|
||||
let idx = GlobalIdx(values.len() as u32);
|
||||
let identity = NodeIdentity::new(SourceLocation { line: 0, col: 0 });
|
||||
names.insert(Symbol::from(name), (idx, identity));
|
||||
types.insert(idx, ty);
|
||||
|
||||
Reference in New Issue
Block a user