R7RS core: call/cc, raise/guard, multi-arity map, cond =>, do iteration

Phase 1 engine step 4 — R7RS compatibility primitives for the CEK evaluator.

call/cc: undelimited continuation capture with separate CallccContinuation
type (distinct from delimited shift/reset continuations). Escape semantics —
invoking k replaces the current continuation entirely.

raise/raise-continuable: proper CEK arg evaluation via raise-eval frame.
Non-continuable raise uses raise-guard frame that errors on handler return.
host-error primitive for safe unhandled exception fallback.

Multi-arity map: (map fn list1 list2 ...) zips multiple lists. Single-list
path unchanged for performance. New multi-map frame type.

cond =>: arrow clause syntax (cond (test => fn)) calls fn with test value.
New cond-arrow frame type.

R7RS do: shape-detecting dispatch — (do ((var init step) ...) (test result) body)
desugars to named let. Existing (do expr1 expr2) sequential form preserved.

integer? primitive, host-error alias. Transpiler fixes: match/case routing,
wildcard _ support, nested match arm handling.

2522/2524 OCaml tests pass (2 pre-existing scope failures from transpiler
match codegen, not related to these changes).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-03 00:29:53 +00:00
parent ede05c26f5
commit 67c4a6a14d
7 changed files with 2025 additions and 1374 deletions

View File

@@ -50,6 +50,8 @@ let sx_call f args =
Thunk (l.l_body, local)
| Continuation (k, _) ->
k (match args with x :: _ -> x | [] -> Nil)
| CallccContinuation _ ->
raise (Eval_error "callcc continuations must be invoked through the CEK machine")
| _ ->
let nargs = List.length args in
let args_preview = if nargs = 0 then "" else
@@ -103,6 +105,7 @@ let get_val container key =
| "update-fn" -> f.cf_extra | "head-name" -> f.cf_extra
| "emitted" -> f.cf_extra2 | "effect-list" -> f.cf_extra2
| "first-render" -> f.cf_extra2 | "file" -> f.cf_env
| "extra" -> f.cf_extra | "extra2" -> f.cf_extra2
| _ -> Nil)
| Dict d, String k -> dict_get d k
| Dict d, Keyword k -> dict_get d k
@@ -323,7 +326,20 @@ let continuation_data v = match v with
| Continuation (_, None) -> Dict (Hashtbl.create 0)
| _ -> raise (Eval_error "not a continuation")
(* Callcc (undelimited) continuation support *)
let callcc_continuation_p v = match v with CallccContinuation _ -> Bool true | _ -> Bool false
let make_callcc_continuation captured =
CallccContinuation (sx_to_list captured)
let callcc_continuation_data v = match v with
| CallccContinuation frames -> List frames
| _ -> raise (Eval_error "not a callcc continuation")
(* Dynamic wind — simplified for OCaml (no async) *)
let host_error msg =
raise (Eval_error (value_to_str msg))
let dynamic_wind_call before body after _env =
ignore (sx_call before []);
let result = sx_call body [] in