nx_lib_std.nx source
↩ module page · 132 lines · 3368 B
1// nx_lib_std.nx -- THE SOVEREIGN STANDARD LIBRARY (seed, v0 2026-07-12).
2// NishiLang has syscalls (nx_syscalls.nx) but no libc: forge-bench measured 809 hand-rolled
3// slen definitions across 807 runtime/ files. This module is the consolidation floor -- the
4// C-standard-library analog, grown sovereignly (operator directive 2026-07-12: own libc so the
5// language can design OS/browser/apps/AI on all hardware with SOTA capability + efficiency).
6// Map: string.h -> std_slen/std_streq/std_scopy/std_find/std_memcpy/std_memset
7// stdlib.h -> std_atoi/std_itoa stdio.h -> std_puts/std_putln/std_pdec
8// Pure functions, no main, no statics (gotcha-safe), single-line statements, hoisted calls.
9// Print buffers are one mmap per call (run-once-organ grade; arena variant = a later rung).
10// Numeric contract: |n| < 2^63-1 (i64-min not handled in v0).
11// license_tier: ORIGINAL
12import "nx_syscalls.nx"
13
14// ---- string.h floor ----
15
16func std_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
17
18func std_streq(a: *u8, b: *u8) -> i64 {
19 var i: i64 = 0
20 while a[i] != (0 as u8) {
21 if a[i] != b[i] { return 0 }
22 i = i + 1
23 }
24 if b[i] != (0 as u8) { return 0 }
25 return 1
26}
27
28func std_scopy(src: *u8, dst: *u8) -> i64 {
29 var i: i64 = 0
30 while src[i] != (0 as u8) {
31 dst[i] = src[i]
32 i = i + 1
33 }
34 dst[i] = 0 as u8
35 return i
36}
37
38func std_find(s: *u8, c: i64) -> i64 {
39 var i: i64 = 0
40 while s[i] != (0 as u8) {
41 let cur: i64 = s[i] as i64
42 if cur == c { return i }
43 i = i + 1
44 }
45 return 0 - 1
46}
47
48func std_memcpy(dst: *u8, src: *u8, n: i64) -> i64 {
49 var i: i64 = 0
50 while i < n {
51 dst[i] = src[i]
52 i = i + 1
53 }
54 return n
55}
56
57func std_memset(dst: *u8, v: i64, n: i64) -> i64 {
58 var i: i64 = 0
59 while i < n {
60 dst[i] = v as u8
61 i = i + 1
62 }
63 return n
64}
65
66// ---- stdlib.h floor ----
67
68func std_atoi(s: *u8) -> i64 {
69 var i: i64 = 0
70 var neg: i64 = 0
71 if s[0] == (45 as u8) { neg = 1; i = 1 }
72 var r: i64 = 0
73 var done: i64 = 0
74 while done == 0 {
75 let c: i64 = s[i] as i64
76 if c < 48 { done = 1 }
77 if c > 57 { done = 1 }
78 if done == 0 {
79 r = r * 10
80 r = r + c
81 r = r - 48
82 i = i + 1
83 }
84 }
85 if neg == 1 { return 0 - r }
86 return r
87}
88
89func std_itoa(n: i64, dst: *u8) -> i64 {
90 let tmp: *u8 = sys_mmap(32) as *u8
91 var w: i64 = 0
92 var v: i64 = n
93 if n < 0 { v = 0 - n }
94 if v == 0 {
95 dst[0] = 48 as u8
96 dst[1] = 0 as u8
97 return 1
98 }
99 var t: i64 = 31
100 while v > 0 {
101 let q: i64 = v / 10
102 let d: i64 = v - q * 10
103 tmp[t] = (48 + d) as u8
104 v = q
105 t = t - 1
106 }
107 if n < 0 {
108 dst[w] = 45 as u8
109 w = w + 1
110 }
111 var r: i64 = t + 1
112 while r < 32 {
113 dst[w] = tmp[r]
114 w = w + 1
115 r = r + 1
116 }
117 dst[w] = 0 as u8
118 return w
119}
120
121// ---- stdio.h floor ----
122
123func std_puts(s: *u8) -> i64 { let n: i64 = std_slen(s); sys_write(1, s, n); return 0 }
124
125func std_putln(s: *u8) -> i64 { std_puts(s); std_puts("\n" as *u8); return 0 }
126
127func std_pdec(n: i64) -> i64 {
128 let buf: *u8 = sys_mmap(32) as *u8
129 let len: i64 = std_itoa(n, buf)
130 sys_write(1, buf, len)
131 return 0
132}