ocaml: phase 4 'arr.(i)' and 'arr.(i) <- v' array indexing (+3 tests, 515 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s

parse-atom-postfix's '.()' branch now disambiguates between let-open
and array-get based on whether the head is a module path (':con' or
':field' chain rooted in ':con'). Module paths still emit
(:let-open M EXPR); everything else emits (:array-get ARR I).

Eval handles :array-get by reading the cell's underlying list at
index. The '<-' assignment handler now also accepts :array-get lhs
and rewrites the cell with one position changed.

Idiomatic OCaml array code now works:

  let a = Array.make 5 0 in
  for i = 0 to 4 do a.(i) <- i * i done;
  a.(3) + a.(4)               = 25

  let a = Array.init 4 (fun i -> i + 1) in
  a.(0) + a.(1) + a.(2) + a.(3)  = 10

  List.(length [1;2;3])         = 3   (* unchanged: List is a module *)
This commit is contained in:
2026-05-09 02:08:21 +00:00
parent 1ed3216ba6
commit 073588812a
4 changed files with 60 additions and 1 deletions

View File

@@ -407,6 +407,18 @@ _Newest first._
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
'a tree`) with insert + in-order traversal. Tests parametric ADT,
recursive match, List.append, List.fold_left.
- 2026-05-09 Phase 4 — `arr.(i)` and `arr.(i) <- v` array indexing
syntax (+3 tests, 515 total). parse-atom-postfix's `.(...)` branch
now disambiguates between let-open and array-get based on whether
the head is a module path (`:con` or a `:field` chain rooted in a
`:con`). Module paths still emit `(:let-open M EXPR)`; everything
else emits `(:array-get ARR I)`. Eval handles `:array-get` by
reading the cell's underlying list at index. The `<-` assignment
handler now also accepts `:array-get` lhs and rewrites the cell
with one position changed. Lets us write idiomatic OCaml array code:
let a = Array.make 5 0 in
for i = 0 to 4 do a.(i) <- i * i done;
a.(3) + a.(4) (* = 25 *)
- 2026-05-09 Phase 6 — Array module (ref-of-list backing) + (op)
operator sections (+6 tests, 512 total). Array implements
make/length/get/set/init/iter/iteri/map/mapi/fold_left/to_list/