Refactor Binder to support recursion and add stdlib operators
This commit introduces two main changes:
1. **Binder Refactoring**: The `Binder` has been refactored to support
recursive definitions by pre-declaring names in the scope before
binding their values. This ensures that a name is visible to itself
when its definition is being processed.
2. **Stdlib Operator Implementation**: Several standard library
operators, including `+`, `-`, `*`, `/`, `>`, and `<`, have been
implemented. These operators now correctly handle both `Int` and
`Float` types for numerical operations and comparisons.
Additionally, the display formatting for `Value::List` and
`Value::Record` has been improved for better readability. The
default source code in `main.rs` has also been updated to include a
Fibonacci example, demonstrating the new recursive capabilities.
This commit is contained in:
+30
-12
@@ -119,28 +119,46 @@ impl Binder {
|
||||
},
|
||||
|
||||
UntypedKind::Def { name, value } => {
|
||||
let val_node = self.bind(value)?;
|
||||
let ty = val_node.ty.clone();
|
||||
|
||||
if self.functions.len() == 1 {
|
||||
// 1. Pre-declare name to support recursion
|
||||
let slot_or_idx = if self.functions.len() == 1 {
|
||||
let mut globals = self.globals.borrow_mut();
|
||||
let idx = if let Some((idx, _)) = globals.get(name.as_ref()) {
|
||||
if let Some((idx, _)) = globals.get(name.as_ref()) {
|
||||
*idx
|
||||
} else {
|
||||
let idx = globals.len() as u32;
|
||||
globals.insert(name.to_string(), (idx, ty.clone()));
|
||||
globals.insert(name.to_string(), (idx, StaticType::Any));
|
||||
idx
|
||||
};
|
||||
}
|
||||
} else {
|
||||
let current_fn = self.functions.last_mut().unwrap();
|
||||
current_fn.scope.define(name, StaticType::Any)
|
||||
};
|
||||
|
||||
// 2. Bind Value (now 'name' is visible)
|
||||
let val_node = self.bind(value)?;
|
||||
let ty = val_node.ty.clone();
|
||||
|
||||
// 3. Update Type and Return Node
|
||||
if self.functions.len() == 1 {
|
||||
{
|
||||
let mut globals = self.globals.borrow_mut();
|
||||
if let Some(entry) = globals.get_mut(name.as_ref()) {
|
||||
entry.1 = ty.clone();
|
||||
}
|
||||
}
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::DefGlobal {
|
||||
global_index: idx,
|
||||
global_index: slot_or_idx,
|
||||
value: Box::new(val_node)
|
||||
}, ty))
|
||||
} else {
|
||||
let current_fn = self.functions.last_mut().unwrap();
|
||||
let slot = current_fn.scope.define(name, ty.clone());
|
||||
|
||||
{
|
||||
let current_fn = self.functions.last_mut().unwrap();
|
||||
if let Some(info) = current_fn.scope.locals.get_mut(name.as_ref()) {
|
||||
info.ty = ty.clone();
|
||||
}
|
||||
}
|
||||
Ok(self.make_node(node.identity.clone(), BoundKind::Set {
|
||||
addr: Address::Local(slot),
|
||||
addr: Address::Local(slot_or_idx),
|
||||
value: Box::new(val_node)
|
||||
}, ty))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user