feat(errors): recoverable failures as values — try/else over ok/err results (#46)
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:
parent
498593311f
commit
0d1b09e4f0
21 changed files with 16590 additions and 15566 deletions
|
|
@ -1,5 +1,53 @@
|
|||
# emit_expr.ludic — lower an expression to IR, returning its register and type.
|
||||
|
||||
# issue #46: `try E else { BODY }` as an expression. Evaluate E (a `result`); on
|
||||
# `ok` the expression is the payload, on `err` BODY runs with the failure message
|
||||
# bound to `error` and its trailing expression supplies the fallback. Pure branch
|
||||
# on the tag — no unwinding, no hidden control flow.
|
||||
function emit_try_body(b: Node, slot: pointer) -> void {
|
||||
var i = 0
|
||||
while i < len(b.kids) {
|
||||
if g_term { return }
|
||||
let st = b.kids[i]
|
||||
if (i == len(b.kids) - 1) and st.kind == S_EXPR { # trailing expression = the fallback value
|
||||
emit_cov_hit(st.line)
|
||||
let v = emit_expr(st.a)
|
||||
emit(` store i32 {coerce_code(v, "int")}, ptr {slot}\n`)
|
||||
} else { emit_stmt(st) }
|
||||
i = i + 1
|
||||
}
|
||||
}
|
||||
|
||||
function emit_try(e: Node) -> Val {
|
||||
g_uses_result = true
|
||||
let r = emit_expr(e.a)
|
||||
if not (r.ty == "result") { perr(`try expects a result value (got {r.ty}) — line {itoa(e.line)}`) }
|
||||
let slot = emit_alloca("i32")
|
||||
emit(` store i32 0, ptr {slot}\n`) # fallback default (an else block with no trailing value)
|
||||
let okp = emit_bind(`getelementptr inbounds %Result, ptr {r.code}, i32 0, i32 0`)
|
||||
let okv = emit_bind(`load i32, ptr {okp}`)
|
||||
let isok = emit_bind(`icmp ne i32 {okv}, 0`)
|
||||
let lok = lbl("tryok"); let lel = lbl("tryelse"); let lend = lbl("tryend")
|
||||
emit(` br i1 {isok}, label %{lok}, label %{lel}\n`)
|
||||
emit(`{lok}:\n`); g_term = false
|
||||
let vp = emit_bind(`getelementptr inbounds %Result, ptr {r.code}, i32 0, i32 1`)
|
||||
let vv = emit_bind(`load i32, ptr {vp}`)
|
||||
emit(` store i32 {vv}, ptr {slot}\n`)
|
||||
emit(` br label %{lend}\n`)
|
||||
emit(`{lel}:\n`); g_term = false
|
||||
let ep = emit_bind(`getelementptr inbounds %Result, ptr {r.code}, i32 0, i32 2`)
|
||||
let ev = emit_bind(`load ptr, ptr {ep}`)
|
||||
let eslot = emit_alloca("ptr")
|
||||
emit(` store ptr {ev}, ptr {eslot}\n`)
|
||||
let save = nloc
|
||||
loc_push("error", eslot, "string") # bind the failure message inside the else block
|
||||
emit_try_body(e.b, slot)
|
||||
nloc = save
|
||||
if not g_term { emit(` br label %{lend}\n`) }
|
||||
emit(`{lend}:\n`); g_term = false
|
||||
return val(emit_bind(`load i32, ptr {slot}`), "int")
|
||||
}
|
||||
|
||||
function emit_load_at(addr: pointer, ty: pointer) -> Val {
|
||||
let r = emit_bind(`load {llty(ty)}, ptr {addr}`)
|
||||
return val(r, ty)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue