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

@@ -557,6 +557,11 @@
((dict? target) (get target fname))
(else (error
(str "ocaml-eval: not a record/module on .field: " target)))))))
((= tag "string-get")
;; (:string-get S I) — evaluate s.[i] as a char access.
(let ((s (ocaml-eval (nth ast 1) env))
(i (ocaml-eval (nth ast 2) env)))
(nth s i)))
((= tag "for")
;; (:for NAME LO HI DIR BODY) — DIR is "ascend" or "descend".
(let ((name (nth ast 1))