ludic/selfhost/str.ludic
Orkuncakilkaya eac8f8f335 Phase 2: enforce statement separators (Rule B)
The parser now requires a newline or ';' between statements (block() in
selfhost/parse.ludic); two statements may no longer sit adjacent with only
spaces. Also fixed if-without-else swallowing its trailing separator.

Migration: tools/ludic-tools/migrate_separators.c inserts ';' at statement
boundaries corpus-wide (examples, runtime, 25 self-host fragments, ~1100
boundaries). Verified semantically identical — the migrated compiler compiles
itself to IR byte-identical to the pre-migration seed, and every golden game
renders identically. Reseeded to the strict compiler; C-free fixpoint holds;
test.sh 14/14.

Docs: Rule B documented in LANGUAGE.md; BOOTSTRAP.md R1 + stale fences updated;
check-docs green across all docs. Fixed a multi-line string literal in
emit_expr.ludic (byte-identical \n escape) that the C toolchain lexer mis-lexes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-27 15:41:29 +03:00

58 lines
1.5 KiB
Text

# str.ludic — the string handling a compiler lives on: comparison, copying,
# slicing out substrings, and integer<->text. All over raw NUL-terminated
# byte buffers reached with peek8/poke8.
fn streq(a: ptr, b: ptr) -> bool {
let i = 0
while true {
let ca = peek8(a, i)
let cb = peek8(b, i)
if ca != cb { return false }
if ca == 0 { return true }
i = i + 1
}
return false
}
fn slen(s: ptr) -> int {
let n = 0
while peek8(s, n) != 0 { n = n + 1 }
return n
}
# a fresh NUL-terminated copy of src[start .. start+n]
fn substr(src: ptr, start: int, n: int) -> ptr {
let b = mem_alloc(n + 1)
let i = 0
while i < n { poke8(b, i, peek8(src, start + i)); i = i + 1 }
poke8(b, n, 0)
return b
}
fn char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
fn char_is_alpha(c: int) -> bool {
if c >= 65 and c <= 90 { return true }
if c >= 97 and c <= 122 { return true }
return c == 95
}
fn char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
# integer -> fresh decimal string
fn itoa(v: int) -> ptr {
if v == 0 { let z = mem_alloc(2); poke8(z, 0, 48); poke8(z, 1, 0); return z }
let neg = false
let x = v
if x < 0 { neg = true; x = 0 - x }
let tmp = mem_alloc(16)
let n = 0
while x > 0 { poke8(tmp, n, 48 + x % 10); x = x / 10; n = n + 1 }
let total = n
if neg { total = total + 1 }
let out = mem_alloc(total + 1)
let k = 0
if neg { poke8(out, 0, 45); k = 1 }
let i = 0
while i < n { poke8(out, k + i, peek8(tmp, n - 1 - i)); i = i + 1 }
poke8(out, total, 0)
return out
}