Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/protocol/synapse/board/src/00-util.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / protocol / synapse / board / src / 00-util.om` 344 lines ยท 6837 bytes Raw Blame History 1 note synapse-board src/00-util.om -- shared state, output buffers, escaping, input loading, JSON field access. 2 note Inputs live in globals (host words may name globals); each JSONL line is copied into a caller-local 3 note 64 KiB line buffer before json.om parses it, because a global is never passed to a function. 4 const SBCAP ulong `4194304` 5 const SBOCAP ulong `8388608` 6 const SBSLOT ulong 4096 7 const SBLINES ulong 999999 8 text tbmd "BOARD.md" 9 text tidx "index.html" 10 text emusage "usage: synapse-board render --claims FILE --map FILE [--findings FILE] --out DIR [--now TS] [--head HEX] | serve --dir DIR --bind IP:PORT [--max N]" 11 global gin0 uchar `4194304` 12 global gin1 uchar `4194304` 13 global gin2 uchar `4194304` 14 global glen ulong 3 15 global gmd uchar `8388608` 16 global ght uchar `8388608` 17 global gol ulong 2 18 global gerr ulong 1 19 global gpath uchar 40960 20 global gplen ulong 10 21 global gtmp uchar 4096 22 global gnow uchar 64 23 global gnowl ulong 1 24 global gpkt uchar 4096 25 text tamp "&" 26 text tlt "<" 27 text tgt ">" 28 text tquot """ 29 text tapos "'" 30 text tnl "\n" 31 text tme "synapse-board: " 32 note ======== output: w 0 is BOARD.md, w 1 is index.html 33 fn sbput w ulong c uchar to void 34 n = get gol w 35 if ge n SBOCAP 36 put gerr 0 1 37 ret 38 end 39 if eq w 0 40 put gmd n c 41 else 42 put ght n c 43 end 44 put gol w add n 1 45 end 46 fn sbraw w ulong s view uchar * to void 47 for i ulong len s 48 sbput w get s i 49 end 50 end 51 fn sbr2 s view uchar * to void 52 sbraw 0 s 53 sbraw 1 s 54 end 55 fn sbnum w ulong v ulong to void 56 d = new uchar 20 0 57 i = ulong 20 58 x = v 59 for it ulong 20 60 i = sub i 1 61 put d i uchar add rem x 10 48 62 x = div x 10 63 if eq x 0 64 break 65 end 66 end 67 for k ulong sub 20 i 68 sbput w get d add i k 69 end 70 end 71 fn sbn2 v ulong to void 72 sbnum 0 v 73 sbnum 1 v 74 end 75 note sbesc1: one byte, escaped for Markdown (w 0) or HTML (w 1); control bytes become a space 76 fn sbesc1 w ulong c uchar to void 77 if or lt c 32 eq c 127 78 sbput w 32 79 ret 80 end 81 if eq c 38 82 sbraw w tamp 83 ret 84 end 85 if eq c 60 86 sbraw w tlt 87 ret 88 end 89 if eq c 62 90 sbraw w tgt 91 ret 92 end 93 if eq w 1 94 if eq c 34 95 sbraw w tquot 96 ret 97 end 98 if eq c 39 99 sbraw w tapos 100 ret 101 end 102 sbput w c 103 ret 104 end 105 if or or eq c 124 eq c 92 or eq c 96 or eq c 91 eq c 93 106 sbput w 92 107 end 108 sbput w c 109 end 110 fn sbesc w ulong s view uchar * n ulong to void 111 for i ulong n 112 sbesc1 w get s i 113 end 114 end 115 fn sbe2 s view uchar * n ulong to void 116 sbesc 0 s n 117 sbesc 1 s n 118 end 119 note ======== stderr diagnostics 120 fn sbsay s view uchar * to void 121 r = write 2 tme 0 len tme 122 r = write 2 s 0 len s 123 r = write 2 tnl 0 len tnl 124 end 125 fn sbout s view uchar * n ulong to long 126 p = ulong 0 127 for it ulong 4096 128 if ge p n 129 ret 0 130 end 131 w = write 1 s p sub n p 132 if le w 0 133 ret -5 134 end 135 p = add p ulong w 136 end 137 ret -5 138 end 139 note ======== arguments: slot s of gpath holds the value of one flag 140 fn sbtmpeq n ulong t view uchar * to bool 141 if ne n len t 142 ret false 143 end 144 for i ulong n 145 if ne get gtmp i get t i 146 ret false 147 end 148 end 149 ret true 150 end 151 fn sbslotc s ulong i ulong to uchar 152 ret get gpath add mul s SBSLOT i 153 end 154 fn sbslotl s ulong to ulong 155 ret get gplen s 156 end 157 note sbdecv: slot s as a decimal number, or `4294967296` when not all digits (or empty or too long) 158 fn sbdecv s ulong to ulong 159 n = sbslotl s 160 if or eq n 0 gt n 9 161 ret `4294967296` 162 end 163 v = ulong 0 164 for i ulong n 165 c = sbslotc s i 166 if or lt c 48 gt c 57 167 ret `4294967296` 168 end 169 v = add mul v 10 ulong sub c 48 170 end 171 ret v 172 end 173 note ======== input files: w 0 claims, 1 map, 2 findings 174 fn sbinb w ulong i ulong to uchar 175 if eq w 0 176 ret get gin0 i 177 end 178 if eq w 1 179 ret get gin1 i 180 end 181 ret get gin2 i 182 end 183 fn sbrd w ulong fd int p ulong to long 184 if eq w 0 185 ret read fd gin0 p sub SBCAP p 186 end 187 if eq w 1 188 ret read fd gin1 p sub SBCAP p 189 end 190 ret read fd gin2 p sub SBCAP p 191 end 192 note sbload: read the file named by slot s into input w; 0 ok, -65 too large, else minus errno 193 fn sbload w ulong s ulong to long 194 put glen w 0 195 fd = fopen gpath mul s SBSLOT sbslotl s 524288 0 196 if lt fd 0 197 ret fd 198 end 199 p = ulong 0 200 for it ulong 999999 201 if ge p SBCAP 202 c = fclose int fd 203 ret -65 204 end 205 r = sbrd w int fd p 206 if lt r 0 207 if ne r -4 208 c = fclose int fd 209 ret r 210 end 211 else 212 if eq r 0 213 c = fclose int fd 214 put glen w p 215 ret 0 216 end 217 p = add p ulong r 218 end 219 end 220 c = fclose int fd 221 ret -65 222 end 223 note sbline: copy the line of input w starting at off (without its newline) into lb; byte count or -1 224 fn sbline w ulong off ulong lb edit uchar 65536 to long 225 tot = get glen w 226 n = ulong 0 227 for k ulong 65537 228 p = add off n 229 if ge p tot 230 ret long n 231 end 232 c = sbinb w p 233 if eq c 10 234 ret long n 235 end 236 if ge n 65536 237 ret -1 238 end 239 put lb n c 240 n = add n 1 241 end 242 ret -1 243 end 244 note ======== JSON fields of one parsed line (root object token 0) 245 fn sbkey lb view uchar 65536 tp view uint 16400 key view uchar * to ulong 246 k = js_first tp 0 247 for it ulong 4096 248 if eq k jsnone 249 ret jsnone 250 end 251 if js_streq lb js_off tp k key 252 ret add k 1 253 end 254 k = js_mnext tp k 255 end 256 ret jsnone 257 end 258 note sbgets: decoded string value of key into sb; byte count, or -1 when absent or not a string 259 fn sbgets lb view uchar 65536 tp view uint 16400 sb edit uchar 65536 key view uchar * to long 260 v = sbkey lb tp key 261 if eq v jsnone 262 ret -1 263 end 264 if ne js_type tp v jsstr 265 ret -1 266 end 267 ret js_sget lb js_off tp v sb 268 end 269 fn sbgetu lb view uchar 65536 tp view uint 16400 key view uchar * to ulong 270 v = sbkey lb tp key 271 if eq v jsnone 272 ret 0 273 end 274 if ne js_type tp v jsnum 275 ret 0 276 end 277 ret js_tou lb js_off tp v 278 end 279 fn sbgetb lb view uchar 65536 tp view uint 16400 key view uchar * to bool 280 v = sbkey lb tp key 281 if eq v jsnone 282 ret false 283 end 284 ret eq js_type tp v jstrue 285 end 286 note sbfld: append the escaped string value of key to output w (nothing when absent) 287 fn sbfld w ulong lb view uchar 65536 tp view uint 16400 sb edit uchar 65536 key view uchar * to void 288 n = sbgets lb tp sb key 289 if gt n 0 290 sbesc w sb ulong n 291 end 292 end 293 note Keep the civil-date intermediates within the pinned compiler's register pool. 294 fn sbyear doe ulong to ulong 295 v = sub doe div doe 1460 296 v = add v div doe 36524 297 v = sub v div doe 146096 298 ret div v 365 299 end 300 fn sbdate b edit uchar 64 days ulong to void 301 z = add days 719468 302 era = div z 146097 303 z = sub z mul era 146097 304 yoe = sbyear z 305 y = add yoe mul era 400 306 z = sub z sub add mul 365 yoe div yoe 4 div yoe 100 307 mp = div add mul 5 z 2 153 308 d = add sub z div add mul 153 mp 2 5 1 309 m = add mp 3 310 if ge mp 10 311 m = sub mp 9 312 end 313 if le m 2 314 y = add y 1 315 end 316 sbput4 b 0 y 317 put b 4 45 318 sbput2 b 5 m 319 put b 7 45 320 sbput2 b 8 d 321 end 322 note sbiso: Unix seconds as YYYY-MM-DDTHH:MM:SSZ into b[0..20) (days-to-civil, proleptic Gregorian) 323 fn sbiso b edit uchar 64 secs ulong to ulong 324 sbdate b div secs 86400 325 sod = rem secs 86400 326 put b 10 84 327 sbput2 b 11 div sod 3600 328 put b 13 58 329 sbput2 b 14 rem div sod 60 60 330 put b 16 58 331 sbput2 b 17 rem sod 60 332 put b 19 90 333 ret 20 334 end 335 fn sbput2 b edit uchar 64 p ulong v ulong to void 336 put b p uchar add div rem v 100 10 48 337 put b add p 1 uchar add rem v 10 48 338 end 339 fn sbput4 b edit uchar 64 p ulong v ulong to void 340 put b p uchar add rem div v 1000 10 48 341 put b add p 1 uchar add rem div v 100 10 48 342 put b add p 2 uchar add rem div v 10 10 48 343 put b add p 3 uchar add rem v 10 48 344 end