Refactor Record's internal representation
This commit refactors the internal representation of `BoundKind::Record` to store a `RecordLayout` and a `Vec` of values, rather than a `Vec` of key-value pairs. This change simplifies the representation and improves efficiency by decoupling the record's structure from its specific values during compilation and analysis. The `RecordLayout` now defines the structure of the record, and the values are stored in a separate vector, ordered according to the layout. This allows for better optimization and type checking, as the record's shape is explicitly defined and immutable once created.
This commit is contained in:
@@ -374,14 +374,28 @@ impl Binder {
|
||||
}
|
||||
|
||||
UntypedKind::Record { fields } => {
|
||||
let mut bound_fields = Vec::new();
|
||||
let mut bound_values = Vec::new();
|
||||
let mut layout_fields = Vec::new();
|
||||
|
||||
for (k, v) in fields {
|
||||
bound_fields.push((self.bind(k)?, self.bind(v)?));
|
||||
let key_node = self.bind(k)?;
|
||||
let val_node = self.bind(v)?;
|
||||
|
||||
if let BoundKind::Constant(crate::ast::types::Value::Keyword(kw)) = key_node.kind {
|
||||
layout_fields.push((kw, crate::ast::types::StaticType::Any));
|
||||
} else {
|
||||
return Err(format!("Record keys must be keywords, found at {:?}", key_node.identity.location));
|
||||
}
|
||||
bound_values.push(val_node);
|
||||
}
|
||||
|
||||
let layout = crate::ast::types::RecordLayout::get_or_create(layout_fields);
|
||||
|
||||
Ok(self.make_node(
|
||||
node.identity.clone(),
|
||||
BoundKind::Record {
|
||||
fields: bound_fields,
|
||||
layout,
|
||||
values: bound_values,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user