code wiki / _hdl_build / nx_code_archscan.nx

nx_code_archscan.nx source

↩ module page · 161 lines · 9832 B

1// nx_code_archscan.nx -- Analyzes a repository to count source files by language, detect library fingerprints, and categorize technical stack components. 2import "nx_gate_gn.nx" 3import "nx_gate_base.nx" 4// nx_code_archscan.nx -- NISHI GAME RESEARCH, rung 3: ARCHITECTURE REVIEWER. Given an ingested repo dir, lists 5// its source files (nx_dir), tallies LANGUAGE by extension, and FINGERPRINTS the stack by scanning content for 6// library signatures -> a "WHAT DOES WHAT" report across language / engine / graphics / UI / scripting. This is 7// the review brain the catalog (rung 1) + fetch+ingest (rung 2) feed. usage: nx_code_archscan <repo-dir> 8// license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_dir.nx" 11const K_MAGIC_8192: i64 = 8192 12const K_MAGIC_2097152: i64 = 2097152 13const K_MAGIC_4096: i64 = 4096 14const K_MAGIC_262144: i64 = 262144 15 16func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 17" as *u8); return ok } 18 19func ends_with(name: *u8, nl: i64, suf: *u8) -> i64 { 20 var sl: i64 = 0; while suf[sl] != (0 as u8) { sl = sl + 1 } 21 if sl > nl { return 0 } 22 var i: i64 = 0 23 while i < sl { if name[nl - sl + i] != suf[i] { return 0 } i = i + 1 } 24 return 1 25} 26// substring search in buf[0..n) 27func has_sig(buf: *u8, n: i64, pat: *u8) -> i64 { 28 var pl: i64 = 0; while pat[pl] != (0 as u8) { pl = pl + 1 } 29 if pl == 0 { return 0 } 30 var i: i64 = 0 31 while i + pl <= n { 32 var j: i64 = 0 33 while j < pl { if buf[i + j] != pat[j] { break } j = j + 1 } 34 if j == pl { return 1 } 35 i = i + 1 36 } 37 return 0 38} 39func line_cat(label: *u8, flag: i64) -> i64 { if flag == 1 { gw(" [x] "); gw(label); gw("\n") } return 0 } 40 41func main(argc: i64, argv: *i64) -> i64 { 42 if argc < 2 { gw("usage: nx_code_archscan <repo-dir>\n"); return 2 } 43 let dir: *u8 = argv[1] as *u8 44 45 let rows: *NxDirRow = sys_mmap(K_MAGIC_8192 * NX_DIR_ROW_BYTES) as *NxDirRow 46 let arena: *u8 = sys_mmap(K_MAGIC_2097152) 47 let res_raw: *u8 = sys_mmap(NX_DIR_RESULT_BYTES) 48 let res: *NxDirResult = res_raw as *NxDirResult 49 let v: i64 = nx_dir_list(dir, rows, K_MAGIC_8192, arena, K_MAGIC_2097152, 0, res) 50 if v != NX_DIR_OK { if v != NX_DIR_TRUNCATED { gw("ARCHSCAN: dir list failed ("); gn(v); gw(")\n"); return 1 } } 51 let nfiles: i64 = res.n_filled 52 53 // language buckets 54 var c_cpp: i64=0; var c_h: i64=0; var c_c: i64=0; var c_cs: i64=0; var c_rs: i64=0; var c_py: i64=0 55 var c_js: i64=0; var c_ts: i64=0; var c_qml: i64=0; var c_gd: i64=0; var c_lua: i64=0; var c_java: i64=0; var c_go: i64=0 56 // stack flags 57 var f_qt: i64=0; var f_sdl: i64=0; var f_gl: i64=0; var f_vk: i64=0; var f_sfml: i64=0; var f_raylib: i64=0 58 var f_godot: i64=0; var f_unity: i64=0; var f_unreal: i64=0; var f_lua: i64=0; var f_imgui: i64=0; var f_bevy: i64=0 59 var total_loc: i64=0; var total_bytes: i64=0 // metrics (vs cloc/tokei) 60 var lic_mit: i64=0; var lic_gpl: i64=0; var lic_apache: i64=0; var lic_bsd: i64=0; var lic_mpl: i64=0; var lic_spdx: i64=0 // license (vs licensee) 61 62 let lenp: *i64 = sys_mmap(16) as *i64 63 let path: *u8 = sys_mmap(K_MAGIC_4096) 64 var dl: i64 = 0; while dir[dl] != (0 as u8) { dl = dl + 1 } 65 var idx: i64 = 0 66 while idx < nfiles { 67 let row: *NxDirRow = nx_dir_row_at(rows, idx) 68 if nx_dir_row_is_regular_file(row) == 1 { 69 let nm: *u8 = row.name_ptr; let nl: i64 = row.name_len 70 if ends_with(nm, nl, ".cpp" as *u8)==1 { c_cpp=c_cpp+1 } else { if ends_with(nm, nl, ".cc" as *u8)==1 { c_cpp=c_cpp+1 } else { 71 if ends_with(nm, nl, ".h" as *u8)==1 { c_h=c_h+1 } else { if ends_with(nm, nl, ".hpp" as *u8)==1 { c_h=c_h+1 } else { 72 if ends_with(nm, nl, ".c" as *u8)==1 { c_c=c_c+1 } else { if ends_with(nm, nl, ".cs" as *u8)==1 { c_cs=c_cs+1 } else { 73 if ends_with(nm, nl, ".rs" as *u8)==1 { c_rs=c_rs+1 } else { if ends_with(nm, nl, ".py" as *u8)==1 { c_py=c_py+1 } else { 74 if ends_with(nm, nl, ".js" as *u8)==1 { c_js=c_js+1 } else { if ends_with(nm, nl, ".ts" as *u8)==1 { c_ts=c_ts+1 } else { 75 if ends_with(nm, nl, ".qml" as *u8)==1 { c_qml=c_qml+1 } else { if ends_with(nm, nl, ".gd" as *u8)==1 { c_gd=c_gd+1 } else { 76 if ends_with(nm, nl, ".lua" as *u8)==1 { c_lua=c_lua+1 } else { if ends_with(nm, nl, ".java" as *u8)==1 { c_java=c_java+1 } else { 77 if ends_with(nm, nl, ".go" as *u8)==1 { c_go=c_go+1 } } } } } } } } } } } } } } } 78 // build path = dir/name, read + scan (bounded) 79 var p: i64 = 0; while p < dl { path[p] = dir[p]; p = p + 1 } 80 path[p] = 47 as u8; p = p + 1 81 var q: i64 = 0; while q < nl { path[p] = nm[q]; p = p + 1; q = q + 1 } 82 path[p] = 0 as u8 83 let fb: *u8 = sys_read_file(path, lenp) 84 if (fb as i64) != 0 { 85 var sl: i64 = lenp[0]; if sl > K_MAGIC_262144 { sl = K_MAGIC_262144 } // scan the head 86 total_bytes = total_bytes + lenp[0] // metrics 87 var li: i64 = 0; while li < lenp[0] { if fb[li] == 10 as u8 { total_loc = total_loc + 1 } li = li + 1 } 88 if has_sig(fb, sl, "MIT License" as *u8)==1 { lic_mit=1 } 89 if has_sig(fb, sl, "GNU GENERAL PUBLIC" as *u8)==1 { lic_gpl=1 } 90 if has_sig(fb, sl, "Apache License" as *u8)==1 { lic_apache=1 } 91 if has_sig(fb, sl, "Redistribution and use in source" as *u8)==1 { lic_bsd=1 } 92 if has_sig(fb, sl, "Mozilla Public License" as *u8)==1 { lic_mpl=1 } 93 if has_sig(fb, sl, "SPDX-License-Identifier" as *u8)==1 { lic_spdx=1 } 94 if has_sig(fb, sl, "QtQuick" as *u8)==1 { f_qt=1 } if has_sig(fb, sl, "#include <Q" as *u8)==1 { f_qt=1 } if has_sig(fb, sl, "import QtQuick" as *u8)==1 { f_qt=1 } 95 if has_sig(fb, sl, "SDL.h" as *u8)==1 { f_sdl=1 } if has_sig(fb, sl, "SDL_Init" as *u8)==1 { f_sdl=1 } if has_sig(fb, sl, "<SDL2" as *u8)==1 { f_sdl=1 } 96 if has_sig(fb, sl, "GL/gl" as *u8)==1 { f_gl=1 } if has_sig(fb, sl, "glClear" as *u8)==1 { f_gl=1 } if has_sig(fb, sl, "glDrawArrays" as *u8)==1 { f_gl=1 } 97 if has_sig(fb, sl, "vulkan" as *u8)==1 { f_vk=1 } if has_sig(fb, sl, "VK_VERSION" as *u8)==1 { f_vk=1 } 98 if has_sig(fb, sl, "SFML/" as *u8)==1 { f_sfml=1 } 99 if has_sig(fb, sl, "raylib.h" as *u8)==1 { f_raylib=1 } 100 if has_sig(fb, sl, "godot" as *u8)==1 { f_godot=1 } if has_sig(fb, sl, "extends Node" as *u8)==1 { f_godot=1 } 101 if has_sig(fb, sl, "UnityEngine" as *u8)==1 { f_unity=1 } 102 if has_sig(fb, sl, "CoreMinimal.h" as *u8)==1 { f_unreal=1 } 103 if has_sig(fb, sl, "lua_State" as *u8)==1 { f_lua=1 } if has_sig(fb, sl, "lua_pcall" as *u8)==1 { f_lua=1 } 104 if has_sig(fb, sl, "imgui" as *u8)==1 { f_imgui=1 } if has_sig(fb, sl, "ImGui::" as *u8)==1 { f_imgui=1 } 105 if has_sig(fb, sl, "use bevy" as *u8)==1 { f_bevy=1 } 106 sys_munmap(fb, lenp[0]) 107 } 108 } 109 idx = idx + 1 110 } 111 if c_qml > 0 { f_qt = 1 } 112 if c_gd > 0 { f_godot = 1 } 113 if c_lua > 0 { f_lua = 1 } 114 115 gw("=== ARCHSCAN: "); gw(dir); gw(" ===\n") 116 gw(" source files: "); gn(nfiles); gw("\n") 117 gw(" LANGUAGE (by file):\n") 118 if c_cpp>0 { gw(" C++ ("); gn(c_cpp); gw(" .cpp/.cc)\n") } 119 if c_h>0 { gw(" C/C++ headers ("); gn(c_h); gw(")\n") } 120 if c_c>0 { gw(" C ("); gn(c_c); gw(")\n") } 121 if c_cs>0 { gw(" C# ("); gn(c_cs); gw(")\n") } 122 if c_rs>0 { gw(" Rust ("); gn(c_rs); gw(")\n") } 123 if c_py>0 { gw(" Python ("); gn(c_py); gw(")\n") } 124 if c_js>0 { gw(" JavaScript ("); gn(c_js); gw(")\n") } 125 if c_ts>0 { gw(" TypeScript ("); gn(c_ts); gw(")\n") } 126 if c_qml>0 { gw(" QML ("); gn(c_qml); gw(")\n") } 127 if c_gd>0 { gw(" GDScript ("); gn(c_gd); gw(")\n") } 128 if c_lua>0 { gw(" Lua ("); gn(c_lua); gw(")\n") } 129 if c_java>0{ gw(" Java ("); gn(c_java); gw(")\n") } 130 if c_go>0 { gw(" Go ("); gn(c_go); gw(")\n") } 131 gw(" STACK FINGERPRINT (what does what):\n") 132 line_cat("Qt / Qt Quick (UI + scene graph)" as *u8, f_qt) 133 line_cat("SDL (windowing/input/graphics)" as *u8, f_sdl) 134 line_cat("OpenGL (graphics)" as *u8, f_gl) 135 line_cat("Vulkan (graphics)" as *u8, f_vk) 136 line_cat("SFML (graphics)" as *u8, f_sfml) 137 line_cat("raylib (graphics)" as *u8, f_raylib) 138 line_cat("Godot (engine)" as *u8, f_godot) 139 line_cat("Unity (engine)" as *u8, f_unity) 140 line_cat("Unreal (engine)" as *u8, f_unreal) 141 line_cat("Bevy (Rust engine)" as *u8, f_bevy) 142 line_cat("Lua (scripting)" as *u8, f_lua) 143 line_cat("Dear ImGui (UI)" as *u8, f_imgui) 144 let any: i64 = f_qt + f_sdl + f_gl + f_vk + f_sfml + f_raylib + f_godot + f_unity + f_unreal + f_bevy + f_lua + f_imgui 145 if any == 0 { gw(" (no known engine/graphics signature detected)\n") } 146 gw(" METRICS (vs cloc/tokei):\n") 147 gw(" lines of code: "); gn(total_loc); gw("\n") 148 gw(" source bytes: "); gn(total_bytes); gw("\n") 149 gw(" LICENSE (vs licensee):\n") 150 let anylic: i64 = lic_mit + lic_gpl + lic_apache + lic_bsd + lic_mpl + lic_spdx 151 if lic_mit==1 { gw(" [x] MIT\n") } 152 if lic_gpl==1 { gw(" [x] GPL\n") } 153 if lic_apache==1 { gw(" [x] Apache\n") } 154 if lic_bsd==1 { gw(" [x] BSD\n") } 155 if lic_mpl==1 { gw(" [x] MPL\n") } 156 if lic_spdx==1 { gw(" [x] SPDX-License-Identifier tags\n") } 157 if anylic==0 { gw(" (no license signature detected)\n") } 158 gw("ARCHSCAN-OK files="); gn(nfiles); gw(" stack_signals="); gn(any); gw(" loc="); gn(total_loc); gw(" licenses="); gn(anylic); gw("\n") 159 if nfiles > 0 { return 0 } 160 return 3 161}