code wiki / _hdl_build / nx_explodelab_bomdims.nx

nx_explodelab_bomdims.nx source

↩ module page · 105 lines · 3817 B

1// nx_explodelab_bomdims.nx -- append DUAL-DIMENSION labels (TRUE mm + thou, from the baked exm_dims tables) to 2// the /explodelab BOM text (knowledge/explodelab_bom.txt). Token match uses the BOM's double-space delimiters 3// (" nut " never matches " nut-bolt-assembly "). IDEMPOTENT: if the marker " mm | " is already present the 4// file is left untouched (safe to run twice). expect_exit: 0 license_tier: ORIGINAL 5import "nx_explodelab_meshes.nx" 6import "nx_step_write.nx" 7import "nx_step_parse.nx" 8const K_MAGIC_65536: i64 = 65536 9const K_MAGIC_131072: i64 = 131072 10const K_MAGIC_1000000: i64 = 1000000 11 12func bd_find(buf: *u8, n: i64, pat: *u8) -> i64 { 13 var pl: i64 = 0 14 while pat[pl] != (0 as u8) { pl = pl + 1 } 15 var i: i64 = 0 16 while i + pl <= n { 17 var j: i64 = 0 18 var ok: i64 = 1 19 while j < pl { 20 if buf[i + j] != pat[j] { ok = 0; j = pl } else { j = j + 1 } 21 } 22 if ok == 1 { return i } 23 i = i + 1 24 } 25 return 0 - 1 26} 27// does the line starting at s (len L) contain the token " <name> "? 28func bd_line_kind(buf: *u8, s: i64, L: i64) -> i64 { 29 let tmp: *u8 = ((buf as i64) + s) as *u8 30 if bd_find(tmp, L, " nut " as *u8) >= 0 { return 1 } 31 if bd_find(tmp, L, " bolt " as *u8) >= 0 { return 2 } 32 if bd_find(tmp, L, " rod " as *u8) >= 0 { return 3 } 33 if bd_find(tmp, L, " plate " as *u8) >= 0 { return 4 } 34 if bd_find(tmp, L, " l-bracket " as *u8) >= 0 { return 5 } 35 return 0 36} 37func bd_mm2(b: *i64, v: i64) -> i64 { 38 let cents: i64 = (v * 100 + 128) / 256 39 swb_n(b, cents / 100) 40 swb_c(b, 46) 41 let r: i64 = cents % 100 42 if r < 10 { swb_c(b, 48) } 43 swb_n(b, r) 44 return 0 45} 46 47func main() -> i64 { 48 sp_puts("=== nx_explodelab_bomdims -- dual-dimension the BOM (mm + thou) ===\n" as *u8) 49 let buf: *u8 = sys_mmap(K_MAGIC_65536) 50 let n: i64 = sp_read_file("knowledge/explodelab_bom.txt" as *u8, buf, K_MAGIC_65536) 51 if n <= 0 { sp_puts("read fail\nRED\n" as *u8); return 1 } 52 if bd_find(buf, n, " mm | " as *u8) >= 0 { 53 sp_puts("already dimensioned -- idempotent no-op\nGREEN\n" as *u8) 54 return 0 55 } 56 let b: *i64 = sys_mmap(64) as *i64 57 swb_init(b, K_MAGIC_131072) 58 let out4: *i64 = sys_mmap(32) as *i64 59 var i: i64 = 0 60 var lines: i64 = 0 61 var dimmed: i64 = 0 62 while i < n { 63 var e: i64 = i 64 while e < n { 65 if buf[e] == (10 as u8) { e = e + K_MAGIC_1000000 } else { e = e + 1 } 66 } 67 e = e - K_MAGIC_1000000 68 if e < i { e = n } 69 // copy the line body 70 var w: i64 = i 71 while w < e { 72 swb_c(b, buf[w] as i64) 73 w = w + 1 74 } 75 let kind: i64 = bd_line_kind(buf, i, e - i) 76 if kind > 0 { 77 if exm_dims(kind, out4) == 1 { 78 swb_s(b, " -- " as *u8) 79 bd_mm2(b, out4[0]) 80 swb_c(b, 120) 81 bd_mm2(b, out4[1]) 82 swb_c(b, 120) 83 bd_mm2(b, out4[2]) 84 swb_s(b, " mm | " as *u8) 85 swb_n(b, out4[3]) 86 swb_s(b, " thou" as *u8) 87 dimmed = dimmed + 1 88 } 89 } 90 swb_c(b, 10) 91 lines = lines + 1 92 i = e + 1 93 } 94 let fd: i64 = sys_openat_wr("knowledge/explodelab_bom.txt" as *u8, 420) 95 if fd < 0 { sp_puts("open fail\nRED\n" as *u8); return 1 } 96 sys_write(fd, b[0] as *u8, b[1]) 97 sys_close(fd) 98 sp_puts("lines=" as *u8); sp_putn(lines) 99 sp_puts(" dimensioned=" as *u8); sp_putn(dimmed) 100 sp_puts(" bytes=" as *u8); sp_putn(b[1]) 101 sp_puts("\n" as *u8) 102 if dimmed >= 16 { sp_puts("GREEN -- BOM dual-dimensioned (mm + thou, from baked exm_dims)\n" as *u8); return 0 } 103 sp_puts("RED -- too few lines dimensioned\n" as *u8) 104 return 1 105}