Refactor: Implement Record Layouts and Optimized Field Access

Introduces a new `RecordLayout` system for efficient and type-safe
record handling.

Key changes:
- `RecordLayout` struct with `O(1)` field lookup using FMap
  optimization.
- Field accessors (`.name`) are now first-class callable values.
- Parser recognizes dot-prefixed identifiers as field accessors.
- Binder and TypeChecker handle field accessors.
- Optimizer transforms field accessor calls into specialized `GetField`
  nodes.
- VM executes `GetField` efficiently by directly accessing record
  values.
- Adds a new `records.myc` example showcasing record features.
- Improves comparison logic for records to use pointer equality for
  layout.
This commit is contained in:
Michael Schimmel
2026-02-26 21:38:20 +01:00
parent 2ad2eb5d43
commit bf74795e01
20 changed files with 548 additions and 63 deletions
+15
View File
@@ -97,6 +97,15 @@ pub enum BoundKind<T = ()> {
captured_by: Vec<Identity>,
},
/// A first-class field accessor (e.g. .name)
FieldAccessor(crate::ast::types::Keyword),
/// Specialized field access (O(1) via RecordLayout)
GetField {
rec: Box<BoundNode<T>>,
field: crate::ast::types::Keyword,
},
If {
cond: Box<BoundNode<T>>,
then_br: Box<BoundNode<T>>,
@@ -188,6 +197,10 @@ where
captured_by: cb,
},
) => na == nb && aa == ab && ka == kb && va == vb && ca == cb,
(BoundKind::FieldAccessor(a), BoundKind::FieldAccessor(b)) => a == b,
(BoundKind::GetField { rec: ra, field: fa }, BoundKind::GetField { rec: rb, field: fb }) => {
ra == rb && fa == fb
}
(
BoundKind::If {
cond: ca,
@@ -273,6 +286,8 @@ impl<T> BoundKind<T> {
};
format!("DEF_{}({}, {:?})", k_str, name.name, addr)
}
BoundKind::FieldAccessor(k) => format!("FIELD_ACCESSOR(.{})", k.name()),
BoundKind::GetField { field, .. } => format!("GET_FIELD(.{})", field.name()),
BoundKind::If { .. } => "IF".to_string(),
BoundKind::Destructure { .. } => "DESTRUCTURE".to_string(),
BoundKind::Lambda {