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>
46 lines
1.3 KiB
Text
46 lines
1.3 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 slen(s: ptr) -> int {
|
|
var 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)
|
|
var 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 }
|
|
var neg = false
|
|
var x = v
|
|
if x < 0 { neg = true; x = 0 - x }
|
|
let tmp = mem_alloc(16)
|
|
var n = 0
|
|
while x > 0 { poke8(tmp, n, 48 + x % 10); x = x / 10; n = n + 1 }
|
|
var total = n
|
|
if neg { total = total + 1 }
|
|
let out = mem_alloc(total + 1)
|
|
var k = 0
|
|
if neg { poke8(out, 0, 45); k = 1 }
|
|
var i = 0
|
|
while i < n { poke8(out, k + i, peek8(tmp, n - 1 - i)); i = i + 1 }
|
|
poke8(out, total, 0)
|
|
return out
|
|
}
|