code wiki / (root) / nx_xbrl_gate.nx

nx_xbrl_gate.nx source

↩ module page · 92 lines · 5198 B

1// nx_xbrl_gate.nx -- F987 INDEPENDENT GATE: XBRL fact parse + accounting-identity validation. 2// Proves namespace/attribute-boundary-aware extraction (never mis-matches a longer concept), integer 3// fact parsing, the balance-identity check, that a MIS-TAGGED filing (does not balance) is flagged, and 4// fail-closed on a missing fact. XBRL uses attributes so tags carry contextRef etc. 5// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 6 7import "nx_xbrl_lib.nx" 8 9func xg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func xg_putn(v: i64) -> i64 { 11 let t: *u8 = sys_mmap(32) 12 var o: i64 = 0 13 var m: i64 = v 14 if m < 0 { t[o] = 45 as u8; o = o + 1; m = 0 - m } 15 let d: *u8 = sys_mmap(32) 16 var k: i64 = 0 17 if m == 0 { d[0] = 48 as u8; k = 1 } 18 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 19 var i: i64 = 0 20 while i < k { t[o] = d[k - 1 - i]; o = o + 1; i = i + 1 } 21 sys_write(1, t, o) 22 return 0 23} 24func xg_ck(cnt: *i64, name: *u8, got: i64, want: i64) -> i64 { 25 if got == want { 26 cnt[0] = cnt[0] + 1 27 xg_puts(" PASS " as *u8); xg_puts(name); xg_puts(" = " as *u8); xg_putn(got); xg_puts("\n" as *u8) 28 return 1 29 } 30 cnt[1] = cnt[1] + 1 31 xg_puts(" FAIL " as *u8); xg_puts(name); xg_puts(" got " as *u8); xg_putn(got) 32 xg_puts(" want " as *u8); xg_putn(want); xg_puts("\n" as *u8) 33 return 0 34} 35 36func main(argc: i64, argv: *i64) -> i64 { 37 let cnt: *i64 = sys_mmap(16) as *i64 38 cnt[0] = 0 39 cnt[1] = 0 40 let out: *i64 = sys_mmap(8 * 4) as *i64 41 42 xg_puts("NISHI-XBRL-GATE (F987 filing facts cross-checked against the accounting identity)\n" as *u8) 43 44 // a BALANCED filing: Assets 1,000,000 = Liabilities 400,000 + Equity 600,000. 45 // AssetsCurrent is present too, to prove the boundary check does not mis-match Assets. 46 let filing: *u8 = "<xbrl><us-gaap:AssetsCurrent contextRef=c1 unitRef=usd decimals=0>250000</us-gaap:AssetsCurrent><us-gaap:Assets contextRef=c1 unitRef=usd decimals=0>1000000</us-gaap:Assets><us-gaap:Liabilities contextRef=c1 unitRef=usd decimals=0>400000</us-gaap:Liabilities><us-gaap:StockholdersEquity contextRef=c1 unitRef=usd decimals=0>600000</us-gaap:StockholdersEquity></xbrl>" as *u8 47 let fl: i64 = iso_len(filing) 48 49 // ---- X1: attribute-aware extraction of Assets (NOT AssetsCurrent) ---- 50 xg_ck(cnt, "X1 us-gaap:Assets parsed = 1000000 (not AssetsCurrent 250000)" as *u8, 51 xbrl_fact_int(filing, fl, "us-gaap:Assets" as *u8), 1000000) 52 xg_ck(cnt, "X1a us-gaap:AssetsCurrent parsed = 250000 (distinct concept)" as *u8, 53 xbrl_fact_int(filing, fl, "us-gaap:AssetsCurrent" as *u8), 250000) 54 55 // ---- X2: the other two facts ---- 56 xg_ck(cnt, "X2 Liabilities = 400000" as *u8, xbrl_fact_int(filing, fl, "us-gaap:Liabilities" as *u8), 400000) 57 xg_ck(cnt, "X2a StockholdersEquity = 600000" as *u8, xbrl_fact_int(filing, fl, "us-gaap:StockholdersEquity" as *u8), 600000) 58 59 // ---- X3: the accounting identity BALANCES (variance 0) ---- 60 xg_ck(cnt, "X3 balanced filing -> variance 0" as *u8, xbrl_check_balance(filing, fl, out), 0) 61 xg_ck(cnt, "X3a out captures assets" as *u8, out[0], 1000000) 62 63 // ---- X4: a MIS-TAGGED filing that does NOT balance is flagged (Equity understated by 50,000) ---- 64 let badfiling: *u8 = "<xbrl><us-gaap:Assets contextRef=c1>1000000</us-gaap:Assets><us-gaap:Liabilities contextRef=c1>400000</us-gaap:Liabilities><us-gaap:StockholdersEquity contextRef=c1>550000</us-gaap:StockholdersEquity></xbrl>" as *u8 65 xg_ck(cnt, "X4 mis-tagged filing -> variance 50000 (flagged, not fed blindly)" as *u8, 66 xbrl_check_balance(badfiling, iso_len(badfiling), out), 50000) 67 68 // ---- X5: FAIL-CLOSED on a missing fact (no StockholdersEquity tag) ---- 69 let missing: *u8 = "<xbrl><us-gaap:Assets contextRef=c1>1000000</us-gaap:Assets><us-gaap:Liabilities contextRef=c1>400000</us-gaap:Liabilities></xbrl>" as *u8 70 xg_ck(cnt, "X5 missing Equity fact -> XB_NO_FACT (fail-closed)" as *u8, 71 xbrl_check_balance(missing, iso_len(missing), out), XB_NO_FACT) 72 73 // ---- X6: an absent concept returns XB_NO_FACT, not a garbage number ---- 74 xg_ck(cnt, "X6 absent concept -> XB_NO_FACT" as *u8, 75 xbrl_fact_int(filing, fl, "us-gaap:Goodwill" as *u8), XB_NO_FACT) 76 77 // ---- X7: a negative-value fact parses (accumulated deficit) ---- 78 let negf: *u8 = "<xbrl><us-gaap:RetainedEarningsAccumulatedDeficit contextRef=c1>-125000</us-gaap:RetainedEarningsAccumulatedDeficit></xbrl>" as *u8 79 xg_ck(cnt, "X7 negative fact -125000 parses" as *u8, 80 xbrl_fact_int(negf, iso_len(negf), "us-gaap:RetainedEarningsAccumulatedDeficit" as *u8), 0 - 125000) 81 82 xg_puts("nx_xbrl_gate: pass=" as *u8); xg_putn(cnt[0]) 83 xg_puts(" fail=" as *u8); xg_putn(cnt[1]); xg_puts("\n" as *u8) 84 if cnt[1] == 0 { 85 xg_puts("F987 nx_xbrl: VERDICT=GREEN (namespace-aware fact parse; a filing that does not balance is flagged; fail-closed on a missing fact)\n" as *u8) 86 sys_exit(0) 87 return 0 88 } 89 xg_puts("F987 nx_xbrl: VERDICT=RED\n" as *u8) 90 sys_exit(1) 91 return 1 92}