Step 9: mutable data structures — R7RS vectors

New Vector type (value array) with 11 primitives:
- make-vector, vector — constructors
- vector-ref, vector-set! — element access/mutation
- vector-length, vector?, vector-fill! — inspection
- vector->list, list->vector — conversion
- vector-copy — independent copy
- Element-wise equality in safe_eq

10 new tests (2668/2668 pass). Follows Record pattern (value array).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-04 22:56:10 +00:00
parent 5ac1ca9756
commit 6fe3476e18
3 changed files with 125 additions and 0 deletions

View File

@@ -239,6 +239,13 @@ let () =
done; !eq)
(* Parameters: same UID = same parameter *)
| Parameter a, Parameter b -> a.pm_uid = b.pm_uid
(* Vectors: same length + element-wise equal *)
| Vector a, Vector b ->
Array.length a = Array.length b &&
(let eq = ref true in
for i = 0 to Array.length a - 1 do
if not (safe_eq a.(i) b.(i)) then eq := false
done; !eq)
(* Lambda/Component/Island/Signal/NativeFn: physical only *)
| _ -> false
in
@@ -764,6 +771,9 @@ let () =
| [Lambda _] -> String "<lambda>"
| [Record r] -> String (Printf.sprintf "#<%s>" r.r_type.rt_name)
| [Parameter p] -> String (Printf.sprintf "#<parameter %s>" p.pm_uid)
| [Vector arr] ->
let elts = Array.to_list (Array.map (fun v -> inspect v) arr) in
String (Printf.sprintf "#(%s)" (String.concat " " elts))
| [a] -> String (inspect a) (* used for dedup keys in compiler *)
| _ -> raise (Eval_error "serialize: 1 arg"));
register "make-symbol" (fun args ->
@@ -1015,6 +1025,42 @@ let () =
match args with
| [Parameter p] -> (match p.pm_converter with Some c -> c | None -> Nil)
| _ -> raise (Eval_error "parameter-converter: expected parameter"));
(* R7RS vectors — mutable fixed-size arrays *)
register "make-vector" (fun args ->
match args with
| [Number n] -> Vector (Array.make (int_of_float n) Nil)
| [Number n; fill] -> Vector (Array.make (int_of_float n) fill)
| _ -> raise (Eval_error "make-vector: expected (length) or (length fill)"));
register "vector" (fun args -> Vector (Array.of_list args));
register "vector?" (fun args ->
match args with [Vector _] -> Bool true | [_] -> Bool false
| _ -> raise (Eval_error "vector?: 1 arg"));
register "vector-length" (fun args ->
match args with [Vector arr] -> Number (float_of_int (Array.length arr))
| _ -> raise (Eval_error "vector-length: expected vector"));
register "vector-ref" (fun args ->
match args with
| [Vector arr; Number n] -> arr.(int_of_float n)
| _ -> raise (Eval_error "vector-ref: expected (vector index)"));
register "vector-set!" (fun args ->
match args with
| [Vector arr; Number n; v] -> arr.(int_of_float n) <- v; Nil
| _ -> raise (Eval_error "vector-set!: expected (vector index value)"));
register "vector->list" (fun args ->
match args with [Vector arr] -> List (Array.to_list arr)
| _ -> raise (Eval_error "vector->list: expected vector"));
register "list->vector" (fun args ->
match args with
| [List l] -> Vector (Array.of_list l)
| [ListRef { contents = l }] -> Vector (Array.of_list l)
| _ -> raise (Eval_error "list->vector: expected list"));
register "vector-fill!" (fun args ->
match args with
| [Vector arr; v] -> Array.fill arr 0 (Array.length arr) v; Nil
| _ -> raise (Eval_error "vector-fill!: expected (vector value)"));
register "vector-copy" (fun args ->
match args with [Vector arr] -> Vector (Array.copy arr)
| _ -> raise (Eval_error "vector-copy: expected vector"));
register "is-else-clause?" (fun args ->
match args with