ocaml: phase 2 mutable record fields r.f <- v (+4 tests, 451 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s

<- added to op-table at level 1 (same as :=). Eval short-circuits on
<- to mutate the lhs's field via host SX dict-set!. The lhs must be a
:field expression; otherwise raises.

Tested:
  let r = { x = 1; y = 2 } in r.x <- 5; r.x       (5)
  let r = { x = 0 } in for i = 1 to 5 do r.x <- r.x + i done; r.x  (15)
  let r = { name = ...; age = 30 } in r.name <- "Alice"; r.name

The 'mutable' keyword in record type decls is parsed-and-discarded;
runtime semantics: every field is mutable. Phase 2 closes this gap
without changing the dict-based record representation.
This commit is contained in:
2026-05-08 18:35:31 +00:00
parent 66da0e5b84
commit d9979eaf6c
4 changed files with 40 additions and 0 deletions

View File

@@ -442,6 +442,18 @@
(let ((cell (ocaml-eval (nth ast 2) env))
(new-val (ocaml-eval (nth ast 3) env)))
(begin (set-nth! cell 0 new-val) nil)))
;; <- mutates a record field. The lhs must be a (:field ...).
((= op "<-")
(let ((lhs-ast (nth ast 2)) (new-val (ocaml-eval (nth ast 3) env)))
(cond
((= (ocaml-tag-of lhs-ast) "field")
(let ((target (ocaml-eval (nth lhs-ast 1) env))
(fname (nth lhs-ast 2)))
(begin (dict-set! target fname new-val) nil)))
(else
(error
(str "ocaml-eval: <- expects a field-access lhs, got "
(ocaml-tag-of lhs-ast)))))))
(else
(ocaml-eval-op op
(ocaml-eval (nth ast 2) env)