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

@@ -1278,6 +1278,14 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 5022)
(eval "(ocaml-run \"List.map ((-) 10) [1;2;3]\")")
;; ── arr.(i) and arr.(i) <- v ────────────────────────────────
(epoch 5030)
(eval "(ocaml-run \"let a = Array.make 5 7 in a.(2) <- 99; a.(2) + a.(0)\")")
(epoch 5031)
(eval "(ocaml-run \"let a = Array.init 4 (fun i -> i + 1) in a.(0) + a.(1) + a.(2) + a.(3)\")")
(epoch 5032)
(eval "(ocaml-run \"let a = Array.make 5 0 in for i = 0 to 4 do a.(i) <- i * i done; a.(3) + a.(4)\")")
EPOCHS
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -2030,6 +2038,11 @@ check 5020 "fold_left (+) sum" '15'
check 5021 "let f = (*) in f 6 7" '42'
check 5022 "List.map ((-) 10) [1;2;3]" '(9 8 7)'
# ── arr.(i) and arr.(i) <- v ────────────────────────────────────
check 5030 "a.(2) <- 99; a.(2) + a.(0)" '106'
check 5031 "Array.init 4 + sum a.(0..3)" '10'
check 5032 "for + a.(i) <- i*i + sum" '25'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"