feat(errors): recoverable failures as values — try/else over ok/err results (#46)
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 17s
ci / build-and-test (push) Successful in 1m14s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 19s

A fallible function returns a `result` value, built with ok(payload) on success
or err(message) on failure. The caller recovers a value with `try EXPR else {
… }`: on ok the whole expression is the payload; on err the else block runs —
with the failure message bound to `error` — and its trailing expression supplies
the fallback. It is a plain branch on the result's tag: no exceptions, no hidden
control flow, nothing unwinds. is_ok(r) / is_err(r) classify without unwrapping.

Payloads are any i32-width scalar (int/fixed/bool/entity). The feature is
additive and only kicks in when ok/err/try are used, so untouched programs
compile byte-identically (verified) and the C-free bootstrap fixpoint holds.
Complements panic/assert from #8 (the unrecoverable half). The optional
top-level frame `recover` stays deferred (needs a frame-abort mechanism); the
full tagged-union/any generalization is tracked in #1.

Adds the `try` keyword and ok/err/is_ok/is_err builtins across the compiler,
the vocabulary header, JetBrains + TextMate/VSCode grammars, the docs inventory
and pages, examples/library/recover.ludic, and a regression case.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-31 15:04:14 +03:00
parent 498593311f
commit 0d1b09e4f0
21 changed files with 16590 additions and 15566 deletions

View file

@ -479,6 +479,50 @@ function emit_call(e: Node) -> Val {
emit(`{lok}:\n`)
return val("0", "void")
}
# --- recoverable failures as values (issue #46) -----------------------------
# A `result` is a heap `%Result = { i32 ok, i32 value, ptr err }`. `ok(v)` wraps
# a success payload (any i32-width scalar: int/fixed/bool/entity), `err(msg)` a
# failure with a message. `is_ok`/`is_err` test the tag; `try E else { … }`
# (emit_try) unwraps the value or runs the fallback. Each is guarded by a
# find_fn check so a user function of the same name still wins.
if (name == "ok") and (find_fn("ok") == null) {
g_uses_result = true
let v = emit_expr(e.kids[0])
let p = emit_bind("call ptr @malloc(i64 16)")
let okp = emit_bind(`getelementptr inbounds %Result, ptr {p}, i32 0, i32 0`)
emit(` store i32 1, ptr {okp}\n`)
let vp = emit_bind(`getelementptr inbounds %Result, ptr {p}, i32 0, i32 1`)
emit(` store i32 {coerce_code(v, "int")}, ptr {vp}\n`)
let ep = emit_bind(`getelementptr inbounds %Result, ptr {p}, i32 0, i32 2`)
emit(` store ptr null, ptr {ep}\n`)
return val(p, "result")
}
if (name == "err") and (find_fn("err") == null) {
g_uses_result = true
let m = emit_expr(e.kids[0])
let p = emit_bind("call ptr @malloc(i64 16)")
let okp = emit_bind(`getelementptr inbounds %Result, ptr {p}, i32 0, i32 0`)
emit(` store i32 0, ptr {okp}\n`)
let vp = emit_bind(`getelementptr inbounds %Result, ptr {p}, i32 0, i32 1`)
emit(` store i32 0, ptr {vp}\n`)
let ep = emit_bind(`getelementptr inbounds %Result, ptr {p}, i32 0, i32 2`)
emit(` store ptr {m.code}, ptr {ep}\n`)
return val(p, "result")
}
if (name == "is_ok") and (find_fn("is_ok") == null) {
let r = emit_expr(e.kids[0])
let okp = emit_bind(`getelementptr inbounds %Result, ptr {r.code}, i32 0, i32 0`)
let okv = emit_bind(`load i32, ptr {okp}`)
let c = emit_bind(`icmp ne i32 {okv}, 0`)
return val(emit_bind(`zext i1 {c} to i32`), "bool")
}
if (name == "is_err") and (find_fn("is_err") == null) {
let r = emit_expr(e.kids[0])
let okp = emit_bind(`getelementptr inbounds %Result, ptr {r.code}, i32 0, i32 0`)
let okv = emit_bind(`load i32, ptr {okp}`)
let c = emit_bind(`icmp eq i32 {okv}, 0`)
return val(emit_bind(`zext i1 {c} to i32`), "bool")
}
# The EV2 reflection ABI (the world table), exposed to Ludic so a Ludic mod can
# introspect the world by name — the same functions a foreign mod binds. Emitted
# only for a modding program (ECS + events), so a plain game is unchanged.
@ -629,6 +673,7 @@ function emit_expr(e: Node) -> Val {
return emit_load_at(a, g_addr_ty)
}
if e.kind == S_EMIT { return emit_emit(e) } # emit as an expression -> cancelled flag
if e.kind == E_TRY { return emit_try(e) } # try E else { … } -> recovered value (issue #46)
if e.kind == E_CALL { return emit_call(e) }
if e.kind == E_BIN { return emit_bin(e) }
if e.kind == E_UN {