compiler: ludic schema gains a "code" section (R7) - the code map from the compiler, so the studio's scan is only a fallback: modules (package, layer, uses and where, friend, files), states, actions, reducers, every dispatch, events (cancellable, net) with every emit and their @On listeners, ports (members with fn types, defaults, required) and binds (port, member, fn, value, at), handlers (phase, hook and target, @Public, @Server / @Predicted, @Queries with filters as written, scene and layer), models (@Owned, @Sync), prefabs, scenes (start, public, shows, lasts / then, loads, enter / exit, layers) and fn_refs - every fn name in code or a resource file with the slot it fills (registry field and row, port member or default, record field, emit field, call argument, assignment, let). Places are "file:line:col"; sorted by name, sites by place; schema_version stays 1 (additive). The parser records emits, fn refs, handlers and scenes' layers / lasts / loads for it (emit_schema_code.ludic, emit_schema_code_game.ludic); LANGUAGE.md and the changeset say so; schema_case lines added (not run); reseeded, bootstrap-cfree fixpoint holds

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-30 00:19:24 +03:00
parent 4ccbc12b48
commit bfd99cdda1
12 changed files with 112243 additions and 92761 deletions

View file

@ -12,6 +12,9 @@
# stripped (the runtime supplies them), written as a field's type is ("fn(A,B)->R")
# components every UI component: props, state, derived fields, functions, events (emit_schema_ui)
# natives every native tag registered with ui_native / ui_native_input by a literal
# units, lang @Unit's spellings; the text keys and the languages (emit_schema_lang)
# code the code map: modules, states, actions, reducers, dispatch, events, ports, binds,
# handlers, models, prefabs, scenes and fn_refs (emit_schema_code)
#
# Deterministic: each list sorted by name (then file and line), an entry list in index order, the
# paths as the compiler read them, and nothing about the run. The engine's runtime is left out.
@ -47,6 +50,8 @@ function schema_buf() -> Buf {
sc_units(b)
buf_puts(b, "],\n \"lang\": ")
sc_lang(b) # emit_schema_lang.ludic: the keys and the languages
buf_puts(b, ",\n \"code\": ")
sc_code(b) # emit_schema_code.ludic: the code map
buf_puts(b, "\n}\n")
return b
}

View file

@ -0,0 +1,459 @@
# emit_schema_code.ludic — the schema's "code" (emit_schema.ludic): the program's code map as the
# compiler read it, so an editor needs no scan of its own. Every place is "file:line:col".
#
# modules name, package, layer, uses (null without a `uses` line) and where, friend, files
# states name, module, at actions name, module, at
# reducers state, action, module, at dispatch action, module, at
# events name, module, at, cancellable, net (@ToServer / @ToClients), emits (sites) and
# listeners (the @On handlers: handler, module, at)
# ports name, module, at, bound, members (name, type, default as written, required)
# binds port, member, fn (the function a `fn name` names, else null), value as written, at
# handlers (emit_schema_code_game.ludic) name, module, at, phase, hook, target, public, net,
# queries, scene, layer
# models, prefabs, scenes, fn_refs (emit_schema_code_game.ludic)
#
# Each list is sorted by name (then file and line) or, for sites, by place. What the runtime
# declares, and what the compiler writes itself, is left out.
# ---- what the parser records for it ------------------------------------------------------------
var g_cx_emits: []Node = new []Node # every `emit E(...)` read
var g_cx_fnrefs: []Node = new []Node # every `fn name` read, in code or data
var g_cx_h: []Node = new []Node # every handler written: its node (name, phase, net role)
var g_cx_hname: []pointer = new []pointer # its name as written (a scene's are qualified later)
var g_cx_hhook: []pointer = new []pointer # null, "On", "OnSpawn", "OnDespawn", ..., "OnStart", "OnQuit"
var g_cx_htarget: []pointer = new []pointer # the event, model or property the hook names
var g_cx_hq: []Node = new []Node # its @Queries (an S_QUERY), or null
var g_cx_hpub: []bool = new []bool # @Public
var g_cx_hscene: []pointer = new []pointer # the scene and layer it is written in, or null
var g_cx_hlayer: []pointer = new []pointer
var g_cx_hlisten: []Node = new []Node # an @On handler's listener (g_onlisten), whose event is resolved
var g_cx_slayers: []pointer = new []pointer # per g_scenes entry: ",a,b," its layers
var g_cx_sthen: []pointer = new []pointer # `lasts N then T`'s T, or null (N is the scene's sp0..sp1)
var g_cx_sloads: []pointer = new []pointer # `loads then T`'s T, or null
var g_cx_spub: []bool = new []bool
function cx_emit(n: Node) -> void { push(g_cx_emits, n) }
function cx_fnref(n: Node) -> void { push(g_cx_fnrefs, n) }
function cx_handler(h: Node, name: pointer, hook: pointer, target: pointer, q: Node, pub: bool, scene: pointer, layer: pointer) -> void {
push(g_cx_h, h)
push(g_cx_hname, name)
push(g_cx_hhook, hook)
push(g_cx_htarget, target)
push(g_cx_hq, q)
push(g_cx_hpub, pub)
push(g_cx_hscene, scene)
push(g_cx_hlayer, layer)
push(g_cx_hlisten, null)
}
# the handler just recorded is an @On listener, the one just registered
function cx_listened() -> void {
let k = len(g_cx_hlisten) - 1
if k >= 0 and len(g_onlisten) > 0 { g_cx_hlisten[k] = g_onlisten[len(g_onlisten) - 1] }
}
function cx_scene(layers: pointer, then_scene: pointer, loads: pointer, pub: bool) -> void {
push(g_cx_slayers, layers)
push(g_cx_sthen, then_scene)
push(g_cx_sloads, loads)
push(g_cx_spub, pub)
}
# ---- the section ----------------------------------------------------------------------------------
function sc_code(b: Buf) -> void {
buf_puts(b, "{\n \"modules\": [")
cx_modules(b)
buf_puts(b, "\n ],\n \"states\": [")
cx_decls(b, 2)
buf_puts(b, "\n ],\n \"actions\": [")
cx_decls(b, 3)
buf_puts(b, "\n ],\n \"reducers\": [")
cx_reducers(b)
buf_puts(b, "\n ],\n \"dispatch\": [")
cx_dispatch(b)
buf_puts(b, "\n ],\n \"events\": [")
cx_events(b)
buf_puts(b, "\n ],\n \"ports\": [")
cx_ports(b)
buf_puts(b, "\n ],\n \"binds\": [")
cx_binds(b)
buf_puts(b, "\n ],\n \"handlers\": [")
cx_handlers(b)
buf_puts(b, "\n ],\n \"models\": [")
cx_models(b)
buf_puts(b, "\n ],\n \"prefabs\": [")
cx_prefabs(b)
buf_puts(b, "\n ],\n \"scenes\": [")
cx_scenes(b)
buf_puts(b, "\n ],\n \"fn_refs\": [")
cx_fn_refs(b)
buf_puts(b, "\n ]\n }")
}
# ---- places ---------------------------------------------------------------------------------------
function cx_at_text(n: Node) -> pointer {
return `{n.file}:{itoa(n.line)}:{itoa(n.col)}`
}
function cx_at(b: Buf, n: Node) -> void {
buf_puts(b, "\"at\": ")
if n == null or n.file == null { buf_puts(b, "null") } else { jq_put(b, cx_at_text(n)) }
}
# a key that sorts by place
function cx_at_key(n: Node) -> pointer {
var f = n.file
if f == null { f = "" }
return `{f}\t{itoa(1000000 + n.line)}\t{itoa(1000000 + n.col)}`
}
# written by the program, in a file it read (not the runtime's, not the compiler's own)
function cx_written(n: Node) -> bool {
return n != null and n.file != null and n.pos >= 0 and not is_runtime_file(n.file)
}
# or read from a resource file, whose tokens carry no offset (lex_at)
function cx_written_or_data(n: Node) -> bool {
if cx_written(n) { return true }
if n == null or n.file == null { return false }
let k = len(n.file)
return k > 5 and (n.file[k - 5 .. k] == ".lres")
}
function cx_module(b: Buf, file: pointer) -> void {
buf_puts(b, "\"module\": ")
sc_str(b, sc_mod(file))
}
# ",a,b," as a JSON array, each name once
function cx_list(b: Buf, list: pointer) -> void {
buf_puts(b, "[")
let seen = ss_new(64)
var n = 0
var i = 1
while i < len(list) {
var j = i
while j < len(list) and list[j] != ',' { j += 1 }
if j > i {
let name = list[i .. j]
if ss_add(seen, name) {
if n > 0 { buf_puts(b, ", ") }
jq_put(b, name)
n += 1
}
}
i = j + 1
}
buf_puts(b, "]")
}
# the indices 0..n-1 sorted by keys
function cx_order(keys: []pointer) -> []int {
let xs = new []int
var i = 0
while i < len(keys) {
if keys[i] != null { push(xs, i) }
i += 1
}
sc_sort(xs, keys)
return xs
}
# ---- modules --------------------------------------------------------------------------------------
function cx_mod_names(names: []pointer, seen: StrSet, from: []pointer) -> void {
var i = 0
while i < len(from) {
if from[i] != null and len(from[i]) > 0 and ss_add(seen, from[i]) { push(names, from[i]) }
i += 1
}
}
function cx_modules(b: Buf) -> void {
let names = new []pointer
let seen = ss_new(256)
cx_mod_names(names, seen, g_mod_name)
cx_mod_names(names, seen, g_mu_mod)
cx_mod_names(names, seen, g_mod_friends)
let xs = cx_order(names)
var a = 0
while a < len(xs) {
let m = names[xs[a]]
sc_sep(b, a == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, m)
let files = new []pointer
var i = 0
while i < len(g_mod_file) {
if (g_mod_name[i] == m) and not is_runtime_file(g_mod_file[i]) { push(files, g_mod_file[i]) }
i += 1
}
var pkg: pointer = null
if len(files) > 0 and len(pkg_of_file(files[0])) > 0 { pkg = pkg_of_file(files[0]) }
buf_puts(b, ", \"package\": ")
sc_str(b, pkg)
let ly = mod_layer(m)
buf_puts(b, ", \"layer\": ")
if len(ly) == 0 { buf_puts(b, "null") } else { jq_put(b, ly) }
let u = mod_find_uses(m)
buf_puts(b, ", \"uses\": ")
if u < 0 { buf_puts(b, "null") } else { cx_list(b, g_mu_list[u]) }
buf_puts(b, ", \"uses_at\": ")
if u < 0 { buf_puts(b, "null") } else { jq_put(b, `{g_mu_file[u]}:{itoa(g_mu_line[u])}`) }
buf_puts(b, ", \"friend\": ")
cx_friend(b, m)
buf_puts(b, ", \"files\": [")
let fx = cx_order(files)
i = 0
while i < len(fx) {
if i > 0 { buf_puts(b, ", ") }
jq_put(b, files[fx[i]])
i += 1
}
buf_puts(b, "]}")
a += 1
}
}
# null, or {"of": null} for a friend of every module, else {"of": [the modules]}
function cx_friend(b: Buf, m: pointer) -> void {
var all = false
var list: pointer = null
var i = 0
while i < len(g_mod_friends) {
if (g_mod_friends[i] == m) {
if len(g_fr_scope[i]) == 0 { all = true } else if list == null { list = g_fr_scope[i] } else { list = list + g_fr_scope[i] }
}
i += 1
}
if not all and list == null {
buf_puts(b, "null")
return
}
buf_puts(b, "{\"of\": ")
if all { buf_puts(b, "null") } else { cx_list(b, list) }
buf_puts(b, "}")
}
# ---- states, actions, reducers, dispatch ----------------------------------------------------------
# the records of one kind (2 a state, 3 an action) in prog
function cx_decls(b: Buf, uns: int) -> void {
let keys = new []pointer
var i = 0
while i < len(prog) {
let d = prog[i]
if d.kind == N_COMP and d.uns == uns and d.s != null and cx_written(d) { push(keys, sc_key(sc_name(d.s), d.file, d.line)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let d = prog[xs[i]]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, sc_name(d.s))
buf_puts(b, ", ")
cx_module(b, d.file)
buf_puts(b, ", ")
cx_at(b, d)
buf_puts(b, "}")
i += 1
}
}
function cx_reducers(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(g_red_nodes) {
let f = g_red_nodes[i]
if cx_written(f) { push(keys, `{g_red_state[i]}\t{g_red_action[i]}\t{cx_at_key(f)}`) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let k = xs[i]
let f = g_red_nodes[k]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"state\": ")
jq_put(b, g_red_state[k])
buf_puts(b, ", \"action\": ")
jq_put(b, g_red_action[k])
buf_puts(b, ", ")
cx_module(b, f.file)
buf_puts(b, ", ")
cx_at(b, f)
buf_puts(b, "}")
i += 1
}
}
function cx_dispatch(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(g_dsp_at) {
let n = g_dsp_at[i]
if cx_written(n) { push(keys, cx_at_key(n)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let n = g_dsp_at[xs[i]]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"action\": ")
jq_put(b, sc_name(g_dsp_names[xs[i]]))
buf_puts(b, ", ")
cx_module(b, n.file)
buf_puts(b, ", ")
cx_at(b, n)
buf_puts(b, "}")
i += 1
}
}
# ---- events ---------------------------------------------------------------------------------------
function cx_events(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(g_events) {
let e = g_events[i]
if e.s != null and cx_written(e) { push(keys, sc_key(sc_name(e.s), e.file, e.line)) } else { push(keys, null) }
i += 1
}
let ekeys = new []pointer # the emits, by place
i = 0
while i < len(g_cx_emits) {
if cx_written(g_cx_emits[i]) { push(ekeys, cx_at_key(g_cx_emits[i])) } else { push(ekeys, null) }
i += 1
}
let ex = cx_order(ekeys)
let hkeys = new []pointer # the listeners, by place
i = 0
while i < len(g_cx_h) {
if g_cx_hlisten[i] != null and cx_written(g_cx_h[i]) { push(hkeys, cx_at_key(g_cx_h[i])) } else { push(hkeys, null) }
i += 1
}
let hx = cx_order(hkeys)
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let e = g_events[xs[i]]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, sc_name(e.s))
buf_puts(b, ", ")
cx_module(b, e.file)
buf_puts(b, ", ")
cx_at(b, e)
buf_puts(b, ", \"cancellable\": ")
sc_bool(b, e.ival == 1)
buf_puts(b, ", \"net\": ")
sc_str(b, e.ty)
buf_puts(b, ", \"emits\": [")
var n = 0
var k = 0
while k < len(ex) {
let s = g_cx_emits[ex[k]]
if (s.s == e.s) {
if n > 0 { buf_puts(b, ", ") }
jq_put(b, cx_at_text(s))
n += 1
}
k += 1
}
buf_puts(b, "], \"listeners\": [")
n = 0
k = 0
while k < len(hx) {
let h = hx[k]
if (g_cx_hlisten[h].s == e.s) {
if n > 0 { buf_puts(b, ", ") }
buf_puts(b, "{\"handler\": ")
jq_put(b, g_cx_hname[h])
buf_puts(b, ", ")
cx_module(b, g_cx_h[h].file)
buf_puts(b, ", ")
cx_at(b, g_cx_h[h])
buf_puts(b, "}")
n += 1
}
k += 1
}
buf_puts(b, "]}")
i += 1
}
}
# ---- ports and binds ------------------------------------------------------------------------------
function cx_ports(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(g_pt_name) {
let c = g_pt_comp[i]
if cx_written(c) { push(keys, sc_key(g_pt_name[i], c.file, c.line)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let k = xs[i]
let c = g_pt_comp[k]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, g_pt_name[k])
buf_puts(b, ", ")
cx_module(b, c.file)
buf_puts(b, ", ")
cx_at(b, c)
buf_puts(b, ", \"bound\": ")
sc_bool(b, g_pt_bind[k] != null)
buf_puts(b, ", \"members\": [")
var f = 0
while f < len(c.kids) {
let fi = c.kids[f]
if f > 0 { buf_puts(b, ", ") }
buf_puts(b, "{\"name\": ")
jq_put(b, fi.s)
buf_puts(b, ", \"type\": ")
jq_put(b, sc_ty(fi.ty))
buf_puts(b, ", \"default\": ")
sc_str(b, sc_text(fi.file, fi.sp0, fi.sp1))
buf_puts(b, ", \"required\": ")
sc_bool(b, fi.a == null)
buf_puts(b, "}")
f += 1
}
buf_puts(b, "]}")
i += 1
}
}
# one row per member a bind gives
function cx_binds(b: Buf) -> void {
let keys = new []pointer
let rows = new []Node
let ports = new []pointer
var i = 0
while i < len(g_bd) {
let bd = g_bd[i]
if cx_written(bd) and bd.a != null {
var k = 0
while k < len(bd.a.kids) {
let fi = bd.a.kids[k]
if fi.kind == E_FINIT {
push(keys, cx_at_key(fi))
push(rows, fi)
push(ports, bd.s)
}
k += 1
}
}
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let fi = rows[xs[i]]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"port\": ")
jq_put(b, ports[xs[i]])
buf_puts(b, ", \"member\": ")
jq_put(b, fi.s)
buf_puts(b, ", \"fn\": ")
if fi.a != null and fi.a.kind == E_FNREF and fi.a.pos >= 0 { jq_put(b, sc_name(fi.a.s)) } else { buf_puts(b, "null") }
buf_puts(b, ", \"value\": ")
sc_str(b, sc_text(fi.file, fi.sp0, fi.sp1))
buf_puts(b, ", ")
cx_module(b, fi.file)
buf_puts(b, ", ")
cx_at(b, fi)
buf_puts(b, "}")
i += 1
}
}

View file

@ -0,0 +1,415 @@
# emit_schema_code_game.ludic — the "code" section's handlers, models, prefabs, scenes and fn_refs
# (emit_schema_code.ludic says the shape of the rest).
#
# handlers name (as written: a scene's without its scene), module, at, phase (null for a hook),
# hook (null, "On", "OnSpawn", "OnDespawn", "OnAttach", "OnDetach", "OnEnable",
# "OnDisable", "OnStart", "OnQuit"), target (the hook's event, model or property),
# public (@Public), net ("server" for @Server, "predicted" for @Predicted, else null),
# queries (null, or {"these": [{"property", "filter"}], "on": model or null}), scene, layer
# models name, module, at, owned (@Owned), properties [{"name", "sync"}]
# prefabs name, model, module, at, components [{"property", "fields": [{"name", "value"}]}]
# scenes name, module, at, start, public, shows, lasts (as written) and then, loads (the scene it
# goes on to), enter, exit (has one), layers
# fn_refs every `fn name` written, in code or data: fn, module, at, and the slot it fills -
# slot_kind "registry" (slot "Registry.field", entry its key), "port" (a bind's
# "Port.member"), "port_default", "default" (a record field's), "record" (a `new` or a
# spawn's "Type.field"), "event" (an emit's "Event.field"), "arg" ("callee(i)", the i-th
# argument written), "assign" (`s.tick = fn f`: "s.tick"), "let" (the name) or "value"
# (anywhere else, slot null)
# ---- handlers -------------------------------------------------------------------------------------
function cx_handlers(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(g_cx_h) {
let h = g_cx_h[i]
if cx_written(h) { push(keys, sc_key(g_cx_hname[i], h.file, h.line)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let k = xs[i]
let h = g_cx_h[k]
let hook = g_cx_hhook[k]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, g_cx_hname[k])
buf_puts(b, ", ")
cx_module(b, h.file)
buf_puts(b, ", ")
cx_at(b, h)
buf_puts(b, ", \"phase\": ")
if hook == null or (hook == "OnStart") or (hook == "OnQuit") { sc_str(b, h.ty) } else { buf_puts(b, "null") }
buf_puts(b, ", \"hook\": ")
sc_str(b, hook)
buf_puts(b, ", \"target\": ")
sc_str(b, g_cx_htarget[k])
buf_puts(b, ", \"public\": ")
sc_bool(b, g_cx_hpub[k])
buf_puts(b, ", \"net\": ")
if h.ival == 1 { jq_put(b, "server") } else if h.ival == 2 { jq_put(b, "predicted") } else { buf_puts(b, "null") }
buf_puts(b, ", \"queries\": ")
cx_queries(b, g_cx_hq[k])
buf_puts(b, ", \"scene\": ")
sc_str(b, g_cx_hscene[k])
buf_puts(b, ", \"layer\": ")
sc_str(b, g_cx_hlayer[k])
buf_puts(b, "}")
i += 1
}
}
# @Queries(these: [P {filter}, ...], on: M): its terms, a filter as written (parse_queries_anno)
function cx_queries(b: Buf, q: Node) -> void {
if q == null or q.c == null {
buf_puts(b, "null")
return
}
buf_puts(b, "{\"these\": [")
var on: pointer = null
var n = 0
var i = 0
while i < len(q.c.kids) {
let t = q.c.kids[i]
if t.ival == 1 {
on = t.s
} else {
if n > 0 { buf_puts(b, ", ") }
buf_puts(b, "{\"property\": ")
jq_put(b, t.s)
buf_puts(b, ", \"filter\": ")
sc_str(b, sc_text(t.file, t.sp0, t.sp1))
buf_puts(b, "}")
n += 1
}
i += 1
}
buf_puts(b, "], \"on\": ")
sc_str(b, on)
buf_puts(b, "}")
}
# ---- models and prefabs ---------------------------------------------------------------------------
function cx_models(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(prog) {
let d = prog[i]
if d.kind == N_ARCH and d.s != null and cx_written(d) { push(keys, sc_key(sc_name(d.s), d.file, d.line)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let d = prog[xs[i]]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, sc_name(d.s))
buf_puts(b, ", ")
cx_module(b, d.file)
buf_puts(b, ", ")
cx_at(b, d)
buf_puts(b, ", \"owned\": ")
sc_bool(b, d.ival == 1)
buf_puts(b, ", \"properties\": [")
var k = 0
while k < len(d.kids) {
if k > 0 { buf_puts(b, ", ") }
buf_puts(b, "{\"name\": ")
jq_put(b, sc_name(d.kids[k].s))
buf_puts(b, ", \"sync\": ")
sc_bool(b, d.kids[k].ival == 1)
buf_puts(b, "}")
k += 1
}
buf_puts(b, "]}")
i += 1
}
}
function cx_prefabs(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(prog) {
let d = prog[i]
if d.kind == N_PREFAB and d.s != null and cx_written(d) { push(keys, sc_key(sc_name(d.s), d.file, d.line)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let d = prog[xs[i]]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, sc_name(d.s))
buf_puts(b, ", \"model\": ")
sc_str(b, sc_name(d.ty))
buf_puts(b, ", ")
cx_module(b, d.file)
buf_puts(b, ", ")
cx_at(b, d)
buf_puts(b, ", \"components\": [")
var k = 0
while k < len(d.kids) {
let ci = d.kids[k]
if k > 0 { buf_puts(b, ", ") }
buf_puts(b, "{\"property\": ")
jq_put(b, sc_name(ci.s))
buf_puts(b, ", \"fields\": [")
var f = 0
while ci.a != null and f < len(ci.a.kids) {
let fi = ci.a.kids[f]
if f > 0 { buf_puts(b, ", ") }
buf_puts(b, "{\"name\": ")
jq_put(b, fi.s)
buf_puts(b, ", \"value\": ")
sc_str(b, sc_text(fi.file, fi.sp0, fi.sp1))
buf_puts(b, "}")
f += 1
}
buf_puts(b, "]}")
k += 1
}
buf_puts(b, "]}")
i += 1
}
}
# ---- scenes ---------------------------------------------------------------------------------------
function cx_scenes(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(g_scenes) {
let n = g_scenes[i]
if cx_written(n) and i < len(g_cx_slayers) { push(keys, sc_key(sc_name(n.s), n.file, n.line)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let k = xs[i]
let n = g_scenes[k]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"name\": ")
jq_put(b, sc_name(n.s))
buf_puts(b, ", ")
cx_module(b, n.file)
buf_puts(b, ", ")
cx_at(b, n)
buf_puts(b, ", \"start\": ")
sc_bool(b, n.ival == g_start_scene)
buf_puts(b, ", \"public\": ")
sc_bool(b, g_cx_spub[k])
buf_puts(b, ", \"shows\": ")
sc_str(b, n.ty)
buf_puts(b, ", \"lasts\": ")
sc_str(b, sc_text(n.file, n.sp0, n.sp1))
buf_puts(b, ", \"then\": ")
sc_str(b, g_cx_sthen[k])
buf_puts(b, ", \"loads\": ")
sc_str(b, g_cx_sloads[k])
buf_puts(b, ", \"enter\": ")
sc_bool(b, n.a != null)
buf_puts(b, ", \"exit\": ")
sc_bool(b, n.b != null)
buf_puts(b, ", \"layers\": ")
cx_list(b, g_cx_slayers[k])
buf_puts(b, "}")
i += 1
}
}
# ---- fn refs --------------------------------------------------------------------------------------
# the walk's findings: each `fn name` node met, with the slot it was met in (the first meeting wins)
var g_cx_wn: []Node = new []Node
var g_cx_wkind: []pointer = new []pointer
var g_cx_wslot: []pointer = new []pointer
var g_cx_wentry: []pointer = new []pointer
function cx_fn_refs(b: Buf) -> void {
cx_walk_all()
let keys = new []pointer
var i = 0
while i < len(g_cx_fnrefs) {
let n = g_cx_fnrefs[i]
if cx_written_or_data(n) and n.s != null { push(keys, `{cx_at_key(n)}\t{n.s}`) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
var first = true
var last: pointer = null
while i < len(xs) {
let n = g_cx_fnrefs[xs[i]]
let key = keys[xs[i]]
if last == null or not (key == last) { # a node read twice is one site
last = key
var w = 0
while w < len(g_cx_wn) and g_cx_wn[w] != n { w += 1 }
sc_sep(b, first, " ")
first = false
buf_puts(b, "{\"fn\": ")
jq_put(b, sc_name(n.s))
buf_puts(b, ", ")
cx_module(b, n.file)
buf_puts(b, ", ")
cx_at(b, n)
buf_puts(b, ", \"slot_kind\": ")
if w < len(g_cx_wn) { jq_put(b, g_cx_wkind[w]) } else { jq_put(b, "value") }
buf_puts(b, ", \"slot\": ")
if w < len(g_cx_wn) { sc_str(b, g_cx_wslot[w]) } else { buf_puts(b, "null") }
buf_puts(b, ", \"entry\": ")
if w < len(g_cx_wn) { sc_str(b, g_cx_wentry[w]) } else { buf_puts(b, "null") }
buf_puts(b, "}")
}
i += 1
}
}
# every place a `fn name` can be written, the narrowest slots first: a registry's rows, the binds, the
# ports' defaults, then the program's declarations and the bodies kept outside them
function cx_walk_all() -> void {
var i = 0
while i < len(g_df_rec) {
cx_walk_rec(g_df_rec[i], "registry", g_df_reg[i], g_df_key[i], 0)
i += 1
}
i = 0
while i < len(g_bd) {
if g_bd[i].a != null { cx_walk_rec(g_bd[i].a, "port", g_bd[i].s, null, 0) }
i += 1
}
i = 0
while i < len(g_pt_comp) {
let c = g_pt_comp[i]
var f = 0
while f < len(c.kids) {
cx_walk(c.kids[f].a, "port_default", `{g_pt_name[i]}.{c.kids[f].s}`, null, 0)
f += 1
}
i += 1
}
i = 0
while i < len(prog) {
if prog[i].file == null or not is_runtime_file(prog[i].file) { cx_walk(prog[i], "value", null, null, 0) }
i += 1
}
cx_walk_list(g_tests)
cx_walk_list(g_onlisten)
cx_walk_list(g_onspawn)
cx_walk_list(g_ondespawn)
cx_walk_list(g_onattach)
cx_walk_list(g_ondetach)
cx_walk_list(g_onenable)
cx_walk_list(g_ondisable)
cx_walk_list(g_scenes)
}
function cx_walk_list(xs: []Node) -> void {
var i = 0
while i < len(xs) {
cx_walk(xs[i], "value", null, null, 0)
i += 1
}
}
# a record literal's values, each in the slot owner.field
function cx_walk_rec(rec: Node, kind: pointer, owner: pointer, entry: pointer, depth: int) -> void {
if rec == null or depth > 400 { return }
var i = 0
while i < len(rec.kids) {
let fi = rec.kids[i]
if fi.kind == E_FINIT { cx_walk(fi.a, kind, `{owner}.{fi.s}`, entry, depth + 1) } else { cx_walk(fi, "value", null, null, depth + 1) }
i += 1
}
}
function cx_callee(c: Node) -> pointer {
if c == null { return null }
if c.kind == E_ID and c.s != null { return sc_name(c.s) }
if c.kind == E_MEMBER and c.a != null and c.a.kind == E_ID and c.a.s != null and c.s != null { return `{c.a.s}.{c.s}` }
return null
}
function cx_walk(n: Node, kind: pointer, slot: pointer, entry: pointer, depth: int) -> void {
if n == null or depth > 400 { return }
let k = n.kind
let d = depth + 1
if k == E_FNREF {
push(g_cx_wn, n)
push(g_cx_wkind, kind)
push(g_cx_wslot, slot)
push(g_cx_wentry, entry)
return
}
if k == E_NEW and n.a != null and n.a.kind == E_REC and n.s != null {
cx_walk_rec(n.a, "record", sc_name(n.s), null, d)
cx_walk(n.b, "value", null, null, d)
return
}
if k == S_EMIT and n.a != null and n.s != null {
cx_walk_rec(n.a, "event", sc_name(n.s), null, d)
return
}
if (k == S_SPAWN or k == N_PREFAB) {
var i = 0
while i < len(n.kids) {
let ci = n.kids[i]
if ci.kind == E_FINIT and ci.a != null and ci.a.kind == E_REC { cx_walk_rec(ci.a, "record", sc_name(ci.s), null, d) } else { cx_walk(ci, "value", null, null, d) }
i += 1
}
return
}
if k == E_CALL {
let callee = cx_callee(n.a)
cx_walk(n.a, "value", null, null, d)
var i = 0
var arg = 0
while i < len(n.kids) {
let a = n.kids[i]
if a != null and a.pos >= 0 {
if callee != null { cx_walk(a, "arg", `{callee}({itoa(arg)})`, null, d) } else { cx_walk(a, "value", null, null, d) }
arg += 1
} else {
cx_walk(a, "value", null, null, d)
}
i += 1
}
return
}
if k == N_COMP {
var i = 0
while i < len(n.kids) {
if n.s != null and n.kids[i].s != null { cx_walk(n.kids[i].a, "default", `{sc_name(n.s)}.{n.kids[i].s}`, null, d) }
i += 1
}
return
}
if k == S_ASSIGN and n.b != null and n.b.kind == E_FNREF { # `s.tick = fn f`: the slot as written
cx_walk(n.b, "assign", cx_callee(n.a), null, d)
return
}
if k == S_LET and n.a != null and n.a.kind == E_FNREF and n.s != null {
cx_walk(n.a, "let", n.s, null, d)
return
}
if k == E_LIST { # a list of fn values fills the slot it is in
var i = 0
while i < len(n.kids) {
cx_walk(n.kids[i], kind, slot, entry, d)
i += 1
}
return
}
if k == N_SYS { # its .b and .c are its layer's and scene's tags
cx_walk(n.a, "value", null, null, d)
return
}
if k == N_SCENE {
cx_walk(n.a, "value", null, null, d)
cx_walk(n.b, "value", null, null, d)
return
}
cx_walk(n.a, "value", null, null, d)
cx_walk(n.b, "value", null, null, d)
cx_walk(n.c, "value", null, null, d)
var i = 0
while i < len(n.kids) {
cx_walk(n.kids[i], "value", null, null, d)
i += 1
}
}