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
+49
View File
@@ -0,0 +1,49 @@
;; Benchmark: 3.4us
;; Benchmark-Repeat: 573
;; Output: ["Alice" 101 :admin "Zürich" ["Alice" "Bob"] true]
; ---------------------------------------------------------
; Records & Optimized Field Access Showcase
; ---------------------------------------------------------
(do
; 1. Record Creation
; Records use interned layouts for O(1) access and low memory overhead.
(def user {:id 101 :name "Alice" :role :admin})
; 2. Optimized Field Access
; These calls are transformed by the optimizer into specialized instructions.
(def name (.name user))
(def id (.id user))
; 3. First-Class Field Accessors
; Accessors start with a dot and can be passed around like functions.
(def get-role .role)
(def role (get-role user))
; 4. Nested Records
(def company {
:name "Myc Corp"
:location {:city "Zürich" :zip 8000}
:employees [user {:id 102 :name "Bob" :role :dev}]
})
; Deep access (also optimized)
(def city (.city (.location company)))
; 5. Higher-Order usage
; Accessors are perfect for mapping over data.
(def names ((fn [f list]
(do
(def [e1 e2] list)
[(f e1) (f e2)]
))
.name (.employees company)))
; 6. Structural Identity
; Records with the same layout share memory; equality checks values.
(def is-equal (= {:x 1 :y 2} {:x 1 :y 2}))
; Return a summary of all tested features
[name id role city names is-equal]
)