ocaml: phase 4 's.[i]' string indexing syntax (+3 tests, 484 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 44s

parse-atom-postfix now dispatches three cases after consuming '.':

  .field  -> existing field/module access
  .(EXPR) -> existing local-open
  .[EXPR] -> new string-get syntax  (this commit)

Eval reduces (:string-get S I) to host (nth S I), which already returns
a one-character string for OCaml's char model.

Lets us write idiomatic OCaml string traversal:

  let s = "hi" in
  let n = ref 0 in
  for i = 0 to String.length s - 1 do
    n := !n + Char.code s.[i]
  done;
  !n  (* = 209 *)
This commit is contained in:
2026-05-08 23:58:37 +00:00
parent bc4f4a5477
commit f895a118fb
4 changed files with 32 additions and 0 deletions

View File

@@ -407,6 +407,12 @@ _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-08 Phase 4 — `s.[i]` string indexing syntax (+3 tests, 484
total). parse-atom-postfix now handles `.[expr]` after `.`,
emitting `(:string-get S I)`; eval reduces to host `(nth s i)`.
Pairs with the existing `M.(expr)` and `.field` postfixes — all three
share one dot loop. `let s = "hi" in for i = 0 to String.length s -
1 do n := !n + Char.code s.[i] done; !n` returns 209 (h+i).
- 2026-05-08 Phase 5.1 — roman.ml baseline (Roman numeral greedy
encoding). Side-quest: top-level `let () = expr` was unsupported by
ocaml-parse-program — now parse-decl-let recognises `()` as a unit