code wiki / _hdl_build / nx_carflip_lib.nx

nx_carflip_lib.nx source

↩ module page · 91 lines · 3097 B

1// nx_carflip_lib.nx -- shared Car-Flip lane library: integer parse/print + the P10-gated deal calc. 2// Extracted from runtime/nx_carflip_roi.nx (v1 stays FROZEN as the CLI equivalence ORACLE -- the 3// sts_load_slow precedent: same 10 figures MUST yield the same verdict). DRY home for the lane's 4// organs (deal scorer now; R1 complaints-ingest + R7 taxonomy organs next). 5// license_tier: ORIGINAL No hw writes (Rule 26). 6import "nx_syscalls.nx" 7 8const CF_BPS: i64 = 10000 // ROI reported in basis points (10000 = 100%) 9 10func cf_puts(s: *u8) -> i64 { 11 var n: i64 = 0 12 while s[n] != (0 as u8) { n = n + 1 } 13 return sys_write(1, s, n) 14} 15 16func cf_atoi(s: *u8) -> i64 { 17 var i: i64 = 0 18 var neg: i64 = 0 19 if s[0] == (45 as u8) { neg = 1; i = 1 } 20 var v: i64 = 0 21 var done: i64 = 0 22 while done == 0 { 23 let c: i64 = s[i] as i64 24 if c < 48 { done = 1 } 25 if c > 57 { done = 1 } 26 if done == 0 { v = v * 10 + (c - 48); i = i + 1 } 27 } 28 if neg == 1 { return 0 - v } 29 return v 30} 31 32func cf_putn(n: i64) -> i64 { 33 if n == 0 { return sys_write(1, "0" as *u8, 1) } 34 var x: i64 = n 35 var neg: i64 = 0 36 if n < 0 { neg = 1; x = 0 - n } 37 let tmp: *u8 = sys_mmap(32) 38 var t: i64 = 0 39 while x > 0 { 40 let d: i64 = x - (x / 10) * 10 41 tmp[t] = (48 + d) as u8 42 t = t + 1 43 x = x / 10 44 } 45 let out: *u8 = sys_mmap(40) 46 var i: i64 = 0 47 if neg == 1 { out[0] = 45 as u8; i = 1 } 48 var k: i64 = t - 1 49 while k >= 0 { 50 out[i] = tmp[k] 51 i = i + 1 52 k = k - 1 53 } 54 return sys_write(1, out, i) 55} 56 57func cf_kv(key: *u8, val: i64) -> i64 { 58 cf_puts(key) 59 cf_puts("=" as *u8) 60 cf_putn(val) 61 cf_puts("\n" as *u8) 62 return 0 63} 64 65// The P10-gated verdict calc: prints the KEY=VALUE block + verdict lines; returns 1=BUY 0=PASS. 66// BUY iff profit_p10 >= 0 (bad case: LOW resale + HIGH repair) -- price the downside, not the hope. 67func cf_score(asking: i64, resale_typ: i64, resale_low: i64, repair_typ: i64, repair_high: i64, recon: i64, transport: i64, fees: i64, sell_cost: i64, holding: i64) -> i64 { 68 let base: i64 = asking + recon + transport + fees + holding 69 let allin_typ: i64 = base + repair_typ 70 let allin_bad: i64 = base + repair_high 71 let profit_typ: i64 = resale_typ - sell_cost - allin_typ 72 let profit_p10: i64 = resale_low - sell_cost - allin_bad 73 var roi_typ: i64 = 0 74 if allin_typ > 0 { roi_typ = profit_typ * CF_BPS / allin_typ } 75 var roi_p10: i64 = 0 76 if allin_bad > 0 { roi_p10 = profit_p10 * CF_BPS / allin_bad } 77 cf_kv("allin_typ" as *u8, allin_typ) 78 cf_kv("allin_bad" as *u8, allin_bad) 79 cf_kv("profit_typ" as *u8, profit_typ) 80 cf_kv("profit_p10" as *u8, profit_p10) 81 cf_kv("roi_typ_bps" as *u8, roi_typ) 82 cf_kv("roi_p10_bps" as *u8, roi_p10) 83 if profit_p10 >= 0 { 84 cf_puts("verdict=BUY\n" as *u8) 85 cf_puts("rule=P10>=0 downside-positive\n" as *u8) 86 return 1 87 } 88 cf_puts("verdict=PASS\n" as *u8) 89 cf_puts("rule=P10<0 downside-negative walk-away\n" as *u8) 90 return 0 91}