refactor(selfhost): reorganise into concern-based subdirectories
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 12s
ci / build-and-test (push) Successful in 50s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 2s

Split the flat 38-file selfhost/ into concern-based subdirectories:

  frontend/        lex, parse, parse_game, ast
  support/         str, buf, io
  backend/         core IR + expression/statement lowering
  backend/game/    ECS/scene/event/world lowering
  backend/stdlib/  the namespaced Math.*/Text.*/Crypto.*/… intrinsics

and split the three oversized emitters at responsibility boundaries so
no file mixes concerns:

  emit_game.ludic  -> + emit_world.ludic         (reflection world table,
                                                  tick helpers, @main synthesis)
  emit_expr.ludic  -> + emit_call.ludic          (namespaced builtins, call
                                                  lowering, expr dispatch)
  emit_text.ludic  -> + emit_text_prelude.ludic  (emitted string-builder runtime)

FRAGS in tools/x/selfhost.ludic is updated to the new paths with the link
order preserved, and the Python doc/vocabulary tooling is updated to walk
the new layout. Because the build is a plain in-order concatenation and
every split lands on a blank-line boundary, the regenerated seed is
byte-identical: `x reseed` leaves selfhost/ludicc.seed.ll unchanged,
`x bootstrap-cfree` still reaches its fixed point, and both `x test` (56)
and `x selfhost-test` (29, incl. golden renders) stay green.

Closes #29

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-31 00:26:02 +03:00
parent f55216af50
commit 23726afa90
51 changed files with 780 additions and 771 deletions

View file

@ -0,0 +1,29 @@
# buf.ludic — a growable byte buffer for building IR text. The emitter writes
# module-level material (types, globals, string constants) into one buffer and
# function bodies into another, then prints them in order.
property Buf { data: pointer = null, len: int = 0, cap: int = 0 }
function buf_new() -> Buf {
let b = new Buf
b.cap = 256
b.data = bytes(b.cap)
b.len = 0
return b
}
function buf_ensure(b: Buf, extra: int) -> void {
if b.len + extra + 1 <= b.cap { return }
while b.len + extra + 1 > b.cap { b.cap = b.cap * 2 }
b.data = resize(b.data, b.cap)
}
function buf_putc(b: Buf, c: int) -> void {
buf_ensure(b, 1)
b.data[b.len] = c
b.len = b.len + 1
}
function buf_puts(b: Buf, s: pointer) -> void {
var i = 0
while s[i] != 0 { buf_putc(b, s[i]); i = i + 1 }
}
function buf_puti(b: Buf, n: int) -> void { buf_puts(b, itoa(n)) }
function buf_str(b: Buf) -> pointer { b.data[b.len] = 0; return b.data }

22
selfhost/support/io.ludic Normal file
View file

@ -0,0 +1,22 @@
# io.ludic — reading the input file and writing the output, plus tiny stdio.
# The compiler reads one .ludic file whole and emits LLVM IR text to stdout.
function read_file(path: string) -> pointer {
let f = file_open(path, "rb")
if (f == null) { return null }
file_seek(f, 0, 2)
let n = file_tell(f)
file_seek(f, 0, 0)
let buf = bytes(n + 1)
file_read(f, buf, n)
buf[n] = 0
file_close(f)
return buf
}
# length of a NUL-terminated buffer
function cstr_len(s: pointer) -> int {
var n = 0
while s[n] != 0 { n = n + 1 }
return n
}

View file

@ -0,0 +1,33 @@
# 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.
# a fresh NUL-terminated copy of src[start .. start+n]
function char_is_digit(c: int) -> bool { return c >= 48 and c <= 57 }
function 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
}
function char_is_alnum(c: int) -> bool { return char_is_alpha(c) or char_is_digit(c) }
# integer -> fresh decimal string
function itoa(v: int) -> pointer {
if v == 0 { let z = bytes(2); z[0] = 48; z[1] = 0; return z }
var neg = false
var x = v
if x < 0 { neg = true; x = 0 - x }
let tmp = bytes(16)
var n = 0
while x > 0 { tmp[n] = 48 + x % 10; x = x / 10; n = n + 1 }
var total = n
if neg { total = total + 1 }
let out = bytes(total + 1)
var k = 0
if neg { out[0] = 45; k = 1 }
var i = 0
while i < n { out[k + i] = tmp[n - 1 - i]; i = i + 1 }
out[total] = 0
return out
}