code wiki / _hdl_build / nx_pattern_emit3.nx
nx_pattern_emit3.nx source
↩ module page · 61 lines · 3701 B
1// nx_pattern_emit3.nx -- PATTERN EMITTER: STATE_FLOW (shape 5 of 10 hands-off). The Builder authors a
2// transitions-table state machine CORE from a small spec: <name>_step(state, ev) -> next state, or -1
3// (ILLEGAL -- the transition is refused, never silently coerced). This is the CMS draft->publish->rollback
4// shape (also the print-job lifecycle and the login-session flow). The table is DATA (from,event,to
5// triples), so the authored core is data-driven per global rule 11 -- no transition logic lives anywhere
6// but the table. Test emission derives REAL KATs from the table itself: every legal row asserted, plus a
7// mechanically-derived illegal pair (max-event+1) asserted refused. Extends nx_pattern_emit (CAPREG231)
8// and nx_pattern_emit2 (CAPREG235). LAWS: struct-free, integer-only, no else-chains (flat ifs), no Claude
9// core logic in the AUTHORED module. license_tier: ORIGINAL
10import "nx_pattern_emit.nx"
11import "nx_syscalls.nx"
12
13// ============ STATE_FLOW ============
14// emit <name>_step(state, ev) from a flattened triple table t[(from,ev,to) x nt].
15func pe3_emit_state_flow(fd: i64, name: *u8, t: *i64, nt: i64) -> i64 {
16 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STATE_FLOW) -- transitions-table state machine, no Claude logic\n" as *u8)
17 pe_w(fd, "// -1 = ILLEGAL transition (refused, caller must handle). Table is the single source of truth.\n" as *u8)
18 pe_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
19 pe_w(fd, "func " as *u8); pe_w(fd, name); pe_w(fd, "_step(state: i64, ev: i64) -> i64 {\n" as *u8)
20 var i: i64 = 0
21 while i < nt {
22 pe_w(fd, " if state == " as *u8); pe_wn(fd, t[i*3])
23 pe_w(fd, " { if ev == " as *u8); pe_wn(fd, t[i*3+1])
24 pe_w(fd, " { return " as *u8); pe_wn(fd, t[i*3+2]); pe_w(fd, " } }\n" as *u8)
25 i = i + 1
26 }
27 pe_w(fd, " return 0 - 1\n}\n" as *u8)
28 return 1
29}
30
31// emit the test: every table row is a KAT; one mechanically-derived illegal pair (from=t[0], ev=maxev+1)
32// must be refused. Flat fail-counter style (no deep if-nesting -- the 4-deep nested-if miscompile landmine).
33func pe3_emit_state_flow_test(fd: i64, name: *u8, t: *i64, nt: i64) -> i64 {
34 var maxev: i64 = 0
35 var j: i64 = 0
36 while j < nt { if t[j*3+1] > maxev { maxev = t[j*3+1] } j = j + 1 }
37 pe_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: STATE_FLOW test) -- KATs derived from the table itself\n" as *u8)
38 pe_w(fd, "import \"" as *u8); pe_w(fd, name); pe_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
39 pe_w(fd, "func main() -> i64 {\n var bad: i64 = 0\n" as *u8)
40 var i: i64 = 0
41 while i < nt {
42 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_step(" as *u8); pe_wn(fd, t[i*3])
43 pe_w(fd, ", " as *u8); pe_wn(fd, t[i*3+1]); pe_w(fd, ") != " as *u8); pe_wn(fd, t[i*3+2])
44 pe_w(fd, " { bad = bad + 1 }\n" as *u8)
45 i = i + 1
46 }
47 // illegal pair: +1 == 0 dodges emitting a negative literal comparison
48 pe_w(fd, " if " as *u8); pe_w(fd, name); pe_w(fd, "_step(" as *u8); pe_wn(fd, t[0])
49 pe_w(fd, ", " as *u8); pe_wn(fd, maxev + 1); pe_w(fd, ") + 1 != 0 { bad = bad + 1 }\n" as *u8)
50 pe_w(fd, " if bad == 0 { sys_exit(0) }\n sys_exit(1)\n return 1\n}\n" as *u8)
51 return 1
52}
53
54// author a STATE_FLOW module+test to disk from the table spec.
55func pe3_author_state_flow(name: *u8, modpath: *u8, testpath: *u8, t: *i64, nt: i64) -> i64 {
56 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
57 pe3_emit_state_flow(mf, name, t, nt); sys_close(mf)
58 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
59 pe3_emit_state_flow_test(tf, name, t, nt); sys_close(tf)
60 return 1
61}