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
omega-mv / protocol / synapse / map / src / 20-infer.om
| 1 | note ======== infer.tsv rules, topics.tsv terms, parent resolution ======== |
| 2 | note infer.tsv: pattern<TAB>parent<TAB>why[<TAB>layer]; '#' lines and blank lines are skipped. |
| 3 | note pattern: a case-insensitive glob (* ?) on the bare repo name, or desc:<substring> of the purpose. |
| 4 | note parent: ROOT, NONE, repo, owner/repo, optionally @ref. The first matching rule wins; a rule that |
| 5 | note would make a repo its own parent is skipped. |
| 6 | |
| 7 | note rules: (pattern, parent, why, layer) offset/length pairs, and the file line number |
| 8 | global grule ulong 4096 |
| 9 | global grln ulong 512 |
| 10 | global gtf ulong 8 |
| 11 | |
| 12 | text tsroot "ROOT" |
| 13 | text tsnone "NONE" |
| 14 | text tsconf "CONFIRMED" |
| 15 | text tsinf "INFERRED" |
| 16 | text tcrumb "crumb" |
| 17 | text tunknown "unknown" |
| 18 | text tdescp "desc:" |
| 19 | |
| 20 | fn sm_pfile p ulong n ulong to ulong |
| 21 | o = get gst SPOOL |
| 22 | for i ulong n |
| 23 | sm_pb get gfile add p i |
| 24 | end |
| 25 | ret o |
| 26 | end |
| 27 | |
| 28 | note sm_tsvsplit: split the row gfile[p, p + m) at tabs; the first 4 fields go to gtf as pool slices; |
| 29 | note returns the number of fields |
| 30 | fn sm_tsvsplit p ulong m ulong to ulong |
| 31 | nf = ulong 0 |
| 32 | s = p |
| 33 | for i ulong add m 1 |
| 34 | de = eq i m |
| 35 | if not de |
| 36 | de = eq get gfile add p i 9 |
| 37 | end |
| 38 | if de |
| 39 | if lt nf 4 |
| 40 | put gtf mul nf 2 sm_pfile s sub add p i s |
| 41 | put gtf add mul nf 2 1 sub add p i s |
| 42 | end |
| 43 | nf = add nf 1 |
| 44 | s = add add p i 1 |
| 45 | end |
| 46 | end |
| 47 | ret nf |
| 48 | end |
| 49 | |
| 50 | note sm_tsv: split gfile[0, SFLEN) into tab-separated rows of kind 1 (rules) or 2 (topics) |
| 51 | fn sm_tsv kind ulong to long |
| 52 | n = get gst SFLEN |
| 53 | p = ulong 0 |
| 54 | ln = ulong 0 |
| 55 | put gst SBAD 0 |
| 56 | while lt p n bound 999999 |
| 57 | e = p |
| 58 | while lt e n bound 999999 |
| 59 | if eq get gfile e 10 |
| 60 | break |
| 61 | end |
| 62 | e = add e 1 |
| 63 | end |
| 64 | ln = add ln 1 |
| 65 | m = sub e p |
| 66 | if gt m 0 |
| 67 | if eq get gfile sub e 1 13 |
| 68 | m = sub m 1 |
| 69 | end |
| 70 | end |
| 71 | skip = sm_blank p m |
| 72 | if not skip |
| 73 | skip = eq get gfile p 35 |
| 74 | end |
| 75 | if not skip |
| 76 | nf = sm_tsvsplit p m |
| 77 | x = sm_tsvrow kind nf ln |
| 78 | if lt x 0 |
| 79 | put gst SBAD ln |
| 80 | ret x |
| 81 | end |
| 82 | end |
| 83 | p = add e 1 |
| 84 | end |
| 85 | ret 0 |
| 86 | end |
| 87 | |
| 88 | fn sm_tsvrow kind ulong nf ulong ln ulong to long |
| 89 | if eq kind 2 |
| 90 | ret sm_toprow nf |
| 91 | end |
| 92 | if lt nf 3 |
| 93 | ret -65 |
| 94 | end |
| 95 | if or eq get gtf 1 0 eq get gtf 3 0 |
| 96 | ret -65 |
| 97 | end |
| 98 | i = get gst SNRUL |
| 99 | if ge i 512 |
| 100 | ret -12 |
| 101 | end |
| 102 | b = mul i 8 |
| 103 | for f ulong 6 |
| 104 | put grule add b f get gtf f |
| 105 | end |
| 106 | put grule add b 6 0 |
| 107 | put grule add b 7 0 |
| 108 | if gt nf 3 |
| 109 | put grule add b 6 get gtf 6 |
| 110 | put grule add b 7 get gtf 7 |
| 111 | end |
| 112 | put grln i ln |
| 113 | put gst SNRUL add i 1 |
| 114 | ret 0 |
| 115 | end |
| 116 | |
| 117 | note sm_toprow: repo<TAB>terms<TAB>evidence; terms are appended to the record's topic field |
| 118 | fn sm_toprow nf ulong to long |
| 119 | if lt nf 2 |
| 120 | ret -65 |
| 121 | end |
| 122 | r = sm_findany get gtf 0 get gtf 1 |
| 123 | if eq r SNONE |
| 124 | ret 0 |
| 125 | end |
| 126 | o = sm_pcp sm_fo r FTOP sm_fl r FTOP |
| 127 | sm_pb 32 |
| 128 | x = sm_pcp get gtf 2 get gtf 3 |
| 129 | sm_fset r FTOP o add add sm_fl r FTOP 1 get gtf 3 |
| 130 | ret 0 |
| 131 | end |
| 132 | |
| 133 | note sm_findany: record by owner/repo, or by bare name when the slice has no '/' |
| 134 | fn sm_findany o ulong n ulong to ulong |
| 135 | if lt sm_pfind o n 47 n |
| 136 | ret sm_find o n |
| 137 | end |
| 138 | for r ulong get gst SNREC |
| 139 | if sm_pieq sm_fo r FNAME sm_fl r FNAME o n |
| 140 | ret r |
| 141 | end |
| 142 | end |
| 143 | ret SNONE |
| 144 | end |
| 145 | |
| 146 | fn sm_gat o ulong n ulong i ulong to uchar |
| 147 | if lt i n |
| 148 | ret sm_lc get gpool add o i |
| 149 | end |
| 150 | ret 0 |
| 151 | end |
| 152 | |
| 153 | note sm_glob: case-insensitive wildcard match of pattern (po, pn) against (so, sn); * any run, ? one byte |
| 154 | fn sm_glob po ulong pn ulong so ulong sn ulong to bool |
| 155 | p = ulong 0 |
| 156 | s = ulong 0 |
| 157 | st = SNONE |
| 158 | mk = ulong 0 |
| 159 | while lt s sn bound 999999 |
| 160 | pc = sm_gat po pn p |
| 161 | sc = sm_lc get gpool add so s |
| 162 | one = false |
| 163 | if and ne pc 42 ne pc 0 |
| 164 | one = or eq pc 63 eq pc sc |
| 165 | end |
| 166 | if one |
| 167 | p = add p 1 |
| 168 | s = add s 1 |
| 169 | else |
| 170 | if eq pc 42 |
| 171 | st = p |
| 172 | mk = s |
| 173 | p = add p 1 |
| 174 | else |
| 175 | if eq st SNONE |
| 176 | ret false |
| 177 | end |
| 178 | p = add st 1 |
| 179 | mk = add mk 1 |
| 180 | s = mk |
| 181 | end |
| 182 | end |
| 183 | end |
| 184 | while eq sm_gat po pn p 42 bound 999999 |
| 185 | p = add p 1 |
| 186 | end |
| 187 | ret eq p pn |
| 188 | end |
| 189 | |
| 190 | note sm_isub: case-insensitive substring test of needle (no, nn) in haystack (ho, hn) |
| 191 | fn sm_isub ho ulong hn ulong no ulong nn ulong to bool |
| 192 | if gt nn hn |
| 193 | ret false |
| 194 | end |
| 195 | for i ulong add sub hn nn 1 |
| 196 | m = true |
| 197 | for j ulong nn |
| 198 | if ne sm_lc get gpool add ho add i j sm_lc get gpool add no j |
| 199 | m = false |
| 200 | break |
| 201 | end |
| 202 | end |
| 203 | if m |
| 204 | ret true |
| 205 | end |
| 206 | end |
| 207 | ret false |
| 208 | end |
| 209 | |
| 210 | fn sm_rmatch i ulong r ulong to bool |
| 211 | po = get grule mul i 8 |
| 212 | pn = get grule add mul i 8 1 |
| 213 | if gt pn 5 |
| 214 | if sm_pteq po 5 tdescp |
| 215 | ret sm_isub sm_fo r FPUR sm_fl r FPUR add po 5 sub pn 5 |
| 216 | end |
| 217 | end |
| 218 | ret sm_glob po pn sm_fo r FNAME sm_fl r FNAME |
| 219 | end |
| 220 | |
| 221 | note sm_pfull: owner/repo[@ref] for a parent slice, prefixing the catalog org when no owner is given |
| 222 | fn sm_pfull o ulong n ulong to ulong |
| 223 | s = get gst SPOOL |
| 224 | q = sm_pfind o n 64 |
| 225 | if ge sm_pfind o q 47 q |
| 226 | x = sm_pcp get gst SORGO get gst SORGL |
| 227 | sm_pb 47 |
| 228 | end |
| 229 | y = sm_pcp o n |
| 230 | put gst SSLEN sub get gst SPOOL s |
| 231 | ret s |
| 232 | end |
| 233 | |
| 234 | note sm_isself: parent slice (o, n) names record r itself |
| 235 | fn sm_isself r ulong o ulong n ulong to bool |
| 236 | q = sm_pfind o n 64 |
| 237 | ret sm_pieq o q sm_fo r FREPO sm_fl r FREPO |
| 238 | end |
| 239 | |
| 240 | fn sm_setrule r ulong i ulong to void |
| 241 | o = get gst SPOOL |
| 242 | sm_pb 76 |
| 243 | sm_pnum get grln i |
| 244 | sm_pb 32 |
| 245 | x = sm_pcp get grule mul i 8 get grule add mul i 8 1 |
| 246 | sm_fset r FRULE o sub get gst SPOOL o |
| 247 | sm_ks r KRUL add i 1 |
| 248 | end |
| 249 | |
| 250 | note sm_rlayer: record r takes the layer of the rule at grule[b] when it has none |
| 251 | fn sm_rlayer r ulong b ulong to void |
| 252 | if and eq sm_fl r FLAY 0 gt get grule add b 7 0 |
| 253 | sm_fset r FLAY get grule add b 6 get grule add b 7 |
| 254 | end |
| 255 | end |
| 256 | |
| 257 | note sm_infer: apply the first matching rule to record r; a crumb-confirmed record only takes the layer |
| 258 | fn sm_infer r ulong to void |
| 259 | cf = ne sm_kg r KCONF 0 |
| 260 | for i ulong get gst SNRUL |
| 261 | b = mul i 8 |
| 262 | if sm_rmatch i r |
| 263 | po = get grule add b 2 |
| 264 | pn = get grule add b 3 |
| 265 | skip = false |
| 266 | if not or cf or sm_pteq po pn tsroot sm_pteq po pn tsnone |
| 267 | o = sm_pfull po pn |
| 268 | skip = sm_isself r o get gst SSLEN |
| 269 | end |
| 270 | if not skip |
| 271 | sm_rlayer r b |
| 272 | if cf |
| 273 | ret |
| 274 | end |
| 275 | sm_setrule r i |
| 276 | if sm_pteq po pn tsroot |
| 277 | sm_fsett r FPST tsroot |
| 278 | sm_ks r KPNUL 1 |
| 279 | ret |
| 280 | end |
| 281 | if sm_pteq po pn tsnone |
| 282 | sm_fsett r FPST tsnone |
| 283 | sm_ks r KPNUL 1 |
| 284 | ret |
| 285 | end |
| 286 | o = sm_pfull po pn |
| 287 | sm_fset r FPAR o get gst SSLEN |
| 288 | sm_fsett r FPST tsinf |
| 289 | sm_ks r KPNUL 0 |
| 290 | ret |
| 291 | end |
| 292 | end |
| 293 | end |
| 294 | if not cf |
| 295 | sm_fsett r FPST tsnone |
| 296 | sm_ks r KPNUL 1 |
| 297 | sm_fset r FRULE 0 0 |
| 298 | end |
| 299 | end |
| 300 | |
| 301 | note sm_resolve: crumb parent (CONFIRMED), else rules; purpose and layer fall back to the crumb |
| 302 | fn sm_resolve r ulong to void |
| 303 | sm_ks r KCONF 0 |
| 304 | sm_ks r KPNUL 1 |
| 305 | sm_fset r FPAR 0 0 |
| 306 | sm_fset r FRULE 0 0 |
| 307 | if gt sm_fl r FCPAR 0 |
| 308 | o = sm_pfull sm_fo r FCPAR sm_fl r FCPAR |
| 309 | n = get gst SSLEN |
| 310 | if not sm_isself r o n |
| 311 | sm_fset r FPAR o n |
| 312 | sm_fsett r FPST tsconf |
| 313 | sm_fsett r FRULE tcrumb |
| 314 | sm_ks r KCONF 1 |
| 315 | sm_ks r KPNUL 0 |
| 316 | end |
| 317 | end |
| 318 | sm_infer r |
| 319 | if eq sm_fl r FLAY 0 |
| 320 | sm_fset r FLAY sm_fo r FCLAY sm_fl r FCLAY |
| 321 | end |
| 322 | if eq sm_fl r FPUR 0 |
| 323 | sm_fset r FPUR sm_fo r FCPUR sm_fl r FCPUR |
| 324 | end |
| 325 | end |
| 326 | |
| 327 | note sm_refs: pin every parent without @ref to the parent's default branch, or @unknown |
| 328 | fn sm_refs to void |
| 329 | for r ulong get gst SNREC |
| 330 | if eq sm_kg r KPNUL 0 |
| 331 | po = sm_fo r FPAR |
| 332 | pn = sm_fl r FPAR |
| 333 | if eq sm_pfind po pn 64 pn |
| 334 | p = sm_find po pn |
| 335 | o = sm_pcp po pn |
| 336 | sm_pb 64 |
| 337 | if ne p SNONE |
| 338 | if gt sm_fl p FDEF 0 |
| 339 | x = sm_pcp sm_fo p FDEF sm_fl p FDEF |
| 340 | else |
| 341 | x = sm_pt tunknown |
| 342 | end |
| 343 | else |
| 344 | x = sm_pt tunknown |
| 345 | end |
| 346 | sm_fset r FPAR o sub get gst SPOOL o |
| 347 | end |
| 348 | end |
| 349 | end |
| 350 | end |
| 351 | |
| 352 | note sm_link: parent record index + 1 into KPIDX (0 when null or not in the map) |
| 353 | fn sm_link to void |
| 354 | for r ulong get gst SNREC |
| 355 | sm_ks r KPIDX 0 |
| 356 | if eq sm_kg r KPNUL 0 |
| 357 | p = sm_find sm_fo r FPAR sm_parref r |
| 358 | if ne p SNONE |
| 359 | sm_ks r KPIDX add p 1 |
| 360 | end |
| 361 | end |
| 362 | end |
| 363 | end |