Phase 7c: string operators + and ==/!= (retire streq/sconcat)

Strings are values now: `a + b` concatenates and `a == b` / `a != b` compare by
content, replacing the 605 `sconcat(...)` / `streq(...)` calls that made the
compiler read like C.

  streq(name, "let")            -> name == "let"
  sconcat("load ", reg)         -> "load " + reg
  sconcat(a, sconcat(b, c))     -> a + b + c

Implementation: emit_bin gains a string path. Strings are pointer-typed, so any
`+` with a pointer operand concatenates and `==`/`!=` between pointers compares
content — except when one side is the `null` literal, which stays a pointer
identity test (the only two kinds of pointer `==` in the codebase). Both call a
small hand-written IR prelude, @fn_str_eq / @fn_str_concat, emitted once into any
program that uses string ops (so it works for tools, games and the compiler with
no runtime-splice dependency and no duplicate symbols).

Delivered as two reseeds: (A) add the operators + prelude with the full
pointer-aware dispatch, keeping streq/sconcat; (B) migrate every call site
(354 lines, via a string-literal-safe balanced-paren script that leaves the
function definitions alone) and delete streq/sconcat. examples/strings.ludic +
a test.sh smoke (prints 1 2 3 4 5) guard it.

Reseeded (21890 lines); C-free fixpoint holds; goldens byte-identical; 18/18;
vocab + doc-fences clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-28 01:05:36 +03:00
parent 56024f2cba
commit a021362cdb
25 changed files with 3553 additions and 3248 deletions

View file

@ -4,7 +4,7 @@
fn find_arch_id(name: ptr) -> int {
var i = 0; var n = 1
while i < len(prog) { if prog[i].kind == N_ARCH { if streq(prog[i].s, name) { return n }; n = n + 1 }; i = i + 1 }
while i < len(prog) { if prog[i].kind == N_ARCH { if (prog[i].s == name) { return n }; n = n + 1 }; i = i + 1 }
return 0
}
@ -18,16 +18,16 @@ fn emit_query(st: Node) -> void {
let cond = lbl("qcond"); let body = lbl("qbody"); let nxt = lbl("qnext"); let endl = lbl("qend")
emit(" br label %"); emit(cond); emit("\n")
emit(cond); emit(":\n")
let i0 = emit_bind(sconcat("load i32, ptr ", ip))
let i0 = emit_bind(("load i32, ptr " + ip))
let ec = emit_bind("load i32, ptr @L_entc")
let lt = emit_bind(sconcat("icmp slt i32 ", sconcat(i0, sconcat(", ", ec))))
let lt = emit_bind(("icmp slt i32 " + (i0 + (", " + ec))))
emit(" br i1 "); emit(lt); emit(", label %"); emit(body); emit(", label %"); emit(endl); emit("\n")
emit(body); emit(":\n"); g_term = false
let i1 = emit_bind(sconcat("load i32, ptr ", ip))
let i1 = emit_bind(("load i32, ptr " + ip))
# alive?
let ap = nreg(); emit(" "); emit(ap); emit(" = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_alive, i32 0, i32 "); emit(i1); emit("\n")
let al = emit_bind(sconcat("load i32, ptr ", ap))
let alc = emit_bind(sconcat("icmp ne i32 ", sconcat(al, ", 0")))
let al = emit_bind(("load i32, ptr " + ap))
let alc = emit_bind(("icmp ne i32 " + (al + ", 0")))
let ka = lbl("qa")
emit(" br i1 "); emit(alc); emit(", label %"); emit(ka); emit(", label %"); emit(nxt); emit("\n")
emit(ka); emit(":\n")
@ -41,15 +41,15 @@ fn emit_query(st: Node) -> void {
var ok = "0"
if ak > 0 {
let kp = nreg(); emit(" "); emit(kp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_kind, i32 0, i32 "); emit(i1); emit("\n")
let kv = emit_bind(sconcat("load i32, ptr ", kp))
let kok = emit_bind(sconcat("icmp eq i32 ", sconcat(kv, sconcat(", ", itoa(ak)))))
let mev = emit_bind(sconcat("load i32, ptr @ME_", tm.s)) # model-enabled flag
let meok = emit_bind(sconcat("icmp ne i32 ", sconcat(mev, ", 0")))
ok = emit_bind(sconcat("and i1 ", sconcat(kok, sconcat(", ", meok))))
let kv = emit_bind(("load i32, ptr " + kp))
let kok = emit_bind(("icmp eq i32 " + (kv + (", " + itoa(ak)))))
let mev = emit_bind(("load i32, ptr @ME_" + tm.s)) # model-enabled flag
let meok = emit_bind(("icmp ne i32 " + (mev + ", 0")))
ok = emit_bind(("and i1 " + (kok + (", " + meok))))
} else {
let hp = nreg(); emit(" "); emit(hp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(tm.s); emit(", i32 0, i32 "); emit(i1); emit("\n")
let hv = emit_bind(sconcat("load i8, ptr ", hp))
ok = emit_bind(sconcat("icmp ne i8 ", sconcat(hv, ", 0")))
let hv = emit_bind(("load i8, ptr " + hp))
ok = emit_bind(("icmp ne i8 " + (hv + ", 0")))
}
let keep = lbl("qt")
emit(" br i1 "); emit(ok); emit(", label %"); emit(keep); emit(", label %"); emit(nxt); emit("\n")
@ -79,7 +79,7 @@ fn emit_query(st: Node) -> void {
# optional where-clause
if (st.b != null) {
let w = emit_expr(st.b)
let wc = emit_bind(sconcat("icmp ne i32 ", sconcat(w.code, ", 0")))
let wc = emit_bind(("icmp ne i32 " + (w.code + ", 0")))
let kw = lbl("qw")
emit(" br i1 "); emit(wc); emit(", label %"); emit(kw); emit(", label %"); emit(nxt); emit("\n")
emit(kw); emit(":\n")
@ -92,8 +92,8 @@ fn emit_query(st: Node) -> void {
if not g_term { emit(" br label %"); emit(nxt); emit("\n") }
emit(nxt); emit(":\n")
let i2 = emit_bind(sconcat("load i32, ptr ", ip))
let i3 = emit_bind(sconcat("add i32 ", sconcat(i2, ", 1")))
let i2 = emit_bind(("load i32, ptr " + ip))
let i3 = emit_bind(("add i32 " + (i2 + ", 1")))
store_at("i32", i3, ip)
emit(" br label %"); emit(cond); emit("\n")
emit(endl); emit(":\n"); g_term = false