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 / train / aien-train / native / omegese45 / translate / en2goal.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note en2goal: deterministic rule-based English request -> R45 goal line G. |
| 3 | note No model. The grammar is the rule table otrules below, documented |
| 4 | note verbatim in docs/omegese45/TRANSLATOR.md. Needs otio.om before it. |
| 5 | note |
| 6 | note Rule table format: one rule per line, `<class> <pattern word> ...`. |
| 7 | note class m = one mm job, p = a pair, x = has more than one reading (23). |
| 8 | note A pattern word is literal alternatives joined by `/`, optional when it |
| 9 | note starts with `?`, or a number slot `#m #k #j #n #s #t` (m, k of the left |
| 10 | note matrix, j, n of the right matrix, seed, second seed). A rule matches when |
| 11 | note the whole normalized token sequence matches the whole pattern. |
| 12 | text otrules "m ?please multiply a/an #m by #k matrix by/with/times a/an #j by #n matrix with seed #s\nm ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seed #s\nm goal for review one matmul_u32 job multiplying a/an #m by #k matrix by a/an #j by #n matrix with seed #s\np ?please ?run/do/compute/submit two matmuls/matmul/matmul_u32s/matmul_u32 ?jobs ?of ?size #m x #k x #n ?with seeds #s and #t\np matmuls #m x #k x #n ?with seeds #s ?and #t\np ?please multiply a/an #m by #k matrix by/with/times a/an #j by #n matrix twice with seeds #s and #t\np goal for review two matmul_u32 jobs each multiplying a/an #m by #k matrix by a/an #j by #n matrix with seeds #s and #t in that order\nx ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seeds #s ?and #t\nx ?please ?run/do/compute/submit two matmuls/matmul/matmul_u32s/matmul_u32 ?jobs ?of ?size #m x #k x #n ?with seed #s\nx ?please multiply a/an #m by #k matrix by/with/times a/an #j by #n matrix with seeds #s and #t\nx ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seed #s and/or ?seed #t\nx ?please ?run/do/compute/submit ?a/an/one matmul/matmul_u32 ?job ?of ?size #m x #k x #n ?with seed #s seed #t\n" |
| 13 | text otfr "goal |mm |pair |ushort 0x|ulong 0x|end\n|EN_UNSUPPORTED\n|EN_RANGE\n|EN_SHAPE\n|EN_AMBIG\n|usage: en2goal < request\n" |
| 14 | |
| 15 | note Normalized request: lowercase words joined by single spaces. |
| 16 | global otn uchar 2048 |
| 17 | global otnl ulong 1 |
| 18 | global ottb ulong 1024 |
| 19 | global ottl ulong 1024 |
| 20 | global otnt ulong 1 |
| 21 | note Slot values and set flags for the rule being matched (m k j n s t). |
| 22 | global otsv ulong 6 |
| 23 | global otss ulong 6 |
| 24 | note Accepted reading: kind (1 mm, 2 pair), code, slots m k n s t. |
| 25 | global otrd ulong 8 |
| 26 | |
| 27 | const OTBAD ulong 1099511627776 |
| 28 | |
| 29 | fn otisdg c uchar to bool |
| 30 | ret and ge c 48 le c 57 |
| 31 | end |
| 32 | |
| 33 | note Raw input byte i of otin[0..n) is a digit; false outside the range. |
| 34 | fn otdgat i ulong n ulong to bool |
| 35 | if ge i n |
| 36 | ret false |
| 37 | end |
| 38 | ret otisdg get otin i |
| 39 | end |
| 40 | |
| 41 | fn otnemit c uchar to void |
| 42 | o = get otnl 0 |
| 43 | put otn o c |
| 44 | put otnl 0 add o 1 |
| 45 | end |
| 46 | |
| 47 | note Emit a separator: one space, never leading, never doubled. |
| 48 | fn otnsep to void |
| 49 | o = get otnl 0 |
| 50 | if eq o 0 |
| 51 | ret |
| 52 | end |
| 53 | if eq get otn sub o 1 32 |
| 54 | ret |
| 55 | end |
| 56 | otnemit 32 |
| 57 | end |
| 58 | |
| 59 | note Normalize otin[0..n) into otn. Returns false for a byte outside the |
| 60 | note request alphabet (TAB and 0x20..0x7e). Rules: A-Z lowercased; space, |
| 61 | note TAB and , ; : ? ! ( ) " separate words; `.` separates words unless a |
| 62 | note digit is on both sides; `x` next to a digit is a word of its own. |
| 63 | fn otnorm n ulong to bool |
| 64 | put otnl 0 0 |
| 65 | for i ulong n |
| 66 | c = get otin i |
| 67 | if or lt c 32 gt c 126 |
| 68 | if ne c 9 |
| 69 | ret false |
| 70 | end |
| 71 | end |
| 72 | if and ge c 65 le c 90 |
| 73 | c = add c 32 |
| 74 | end |
| 75 | sep = false |
| 76 | if or eq c 9 eq c 32 |
| 77 | sep = true |
| 78 | end |
| 79 | if or or eq c 44 eq c 59 or eq c 58 eq c 63 |
| 80 | sep = true |
| 81 | end |
| 82 | if or or eq c 33 eq c 40 or eq c 41 eq c 34 |
| 83 | sep = true |
| 84 | end |
| 85 | if eq c 46 |
| 86 | pd = otdgat sub i 1 n |
| 87 | nd = otdgat add i 1 n |
| 88 | if not and pd nd |
| 89 | sep = true |
| 90 | end |
| 91 | end |
| 92 | if eq c 120 |
| 93 | pd = otdgat sub i 1 n |
| 94 | nd = otdgat add i 1 n |
| 95 | if or pd nd |
| 96 | otnsep |
| 97 | otnemit c |
| 98 | otnsep |
| 99 | continue |
| 100 | end |
| 101 | end |
| 102 | if sep |
| 103 | otnsep |
| 104 | else |
| 105 | otnemit c |
| 106 | end |
| 107 | end |
| 108 | o = get otnl 0 |
| 109 | if gt o 0 |
| 110 | if eq get otn sub o 1 32 |
| 111 | put otnl 0 sub o 1 |
| 112 | end |
| 113 | end |
| 114 | ret true |
| 115 | end |
| 116 | |
| 117 | note Split otn into tokens (start, length). |
| 118 | fn ottoken to void |
| 119 | n = get otnl 0 |
| 120 | t = ulong 0 |
| 121 | b = ulong 0 |
| 122 | for i ulong n |
| 123 | if eq get otn i 32 |
| 124 | put ottb t b |
| 125 | put ottl t sub i b |
| 126 | t = add t 1 |
| 127 | b = add i 1 |
| 128 | end |
| 129 | end |
| 130 | if gt n 0 |
| 131 | put ottb t b |
| 132 | put ottl t sub n b |
| 133 | t = add t 1 |
| 134 | end |
| 135 | put otnt 0 t |
| 136 | end |
| 137 | |
| 138 | note Token ti is a number word: starts with a digit, or + or - then a digit. |
| 139 | fn otisnum ti ulong to bool |
| 140 | b = get ottb ti |
| 141 | l = get ottl ti |
| 142 | c = get otn b |
| 143 | if otisdg c |
| 144 | ret true |
| 145 | end |
| 146 | if and gt l 1 or eq c 43 eq c 45 |
| 147 | ret otisdg get otn add b 1 |
| 148 | end |
| 149 | ret false |
| 150 | end |
| 151 | |
| 152 | note Decimal value of number token ti; OTBAD when malformed (a sign, a |
| 153 | note non-digit, or more than 9 digits). OTBAD is outside every range. |
| 154 | fn otnumv ti ulong to ulong |
| 155 | b = get ottb ti |
| 156 | l = get ottl ti |
| 157 | if gt l 9 |
| 158 | ret OTBAD |
| 159 | end |
| 160 | v = ulong 0 |
| 161 | for i ulong l |
| 162 | c = get otn add b i |
| 163 | if not otisdg c |
| 164 | ret OTBAD |
| 165 | end |
| 166 | v = add mul v 10 ulong sub c 48 |
| 167 | end |
| 168 | ret v |
| 169 | end |
| 170 | |
| 171 | note Token ti equals rule text bytes [ps, pe). |
| 172 | fn oteqw ti ulong ps ulong pe ulong to bool |
| 173 | b = get ottb ti |
| 174 | l = get ottl ti |
| 175 | if ne l sub pe ps |
| 176 | ret false |
| 177 | end |
| 178 | for i ulong l |
| 179 | if ne get otn add b i get otrules add ps i |
| 180 | ret false |
| 181 | end |
| 182 | end |
| 183 | ret true |
| 184 | end |
| 185 | |
| 186 | note Token ti equals one of the `/`-separated alternatives in [ps, pe). |
| 187 | fn otalt ti ulong ps ulong pe ulong to bool |
| 188 | a = ps |
| 189 | p = ps |
| 190 | while le p pe bound 4096 |
| 191 | e = eq p pe |
| 192 | if not e |
| 193 | e = eq get otrules p 47 |
| 194 | end |
| 195 | if e |
| 196 | if oteqw ti a p |
| 197 | ret true |
| 198 | end |
| 199 | a = add p 1 |
| 200 | end |
| 201 | p = add p 1 |
| 202 | end |
| 203 | ret false |
| 204 | end |
| 205 | |
| 206 | fn otslix c uchar to ulong |
| 207 | if eq c 109 |
| 208 | ret 0 |
| 209 | end |
| 210 | if eq c 107 |
| 211 | ret 1 |
| 212 | end |
| 213 | if eq c 106 |
| 214 | ret 2 |
| 215 | end |
| 216 | if eq c 110 |
| 217 | ret 3 |
| 218 | end |
| 219 | if eq c 115 |
| 220 | ret 4 |
| 221 | end |
| 222 | ret 5 |
| 223 | end |
| 224 | |
| 225 | note Match the rule pattern from byte pi against tokens from ti to the end. |
| 226 | fn otmat pi ulong ti ulong to bool |
| 227 | if eq get otrules pi 10 |
| 228 | ret eq ti get otnt 0 |
| 229 | end |
| 230 | pe = pi |
| 231 | while true bound 4096 |
| 232 | c = get otrules pe |
| 233 | if or eq c 32 eq c 10 |
| 234 | break |
| 235 | end |
| 236 | pe = add pe 1 |
| 237 | end |
| 238 | np = pe |
| 239 | if eq get otrules pe 32 |
| 240 | np = add pe 1 |
| 241 | end |
| 242 | ps = pi |
| 243 | if eq get otrules pi 63 |
| 244 | ps = add pi 1 |
| 245 | if otmat np ti |
| 246 | ret true |
| 247 | end |
| 248 | end |
| 249 | if ge ti get otnt 0 |
| 250 | ret false |
| 251 | end |
| 252 | if eq get otrules ps 35 |
| 253 | if not otisnum ti |
| 254 | ret false |
| 255 | end |
| 256 | si = otslix get otrules add ps 1 |
| 257 | put otsv si otnumv ti |
| 258 | put otss si 1 |
| 259 | ret otmat np add ti 1 |
| 260 | end |
| 261 | if not otalt ti ps pe |
| 262 | ret false |
| 263 | end |
| 264 | ret otmat np add ti 1 |
| 265 | end |
| 266 | |
| 267 | note A dim is representable in D (0x000..0xfff). Dim 0 and 257..4095 reach |
| 268 | note the core, which refuses them with `bound` (interface v1.2 section 4). |
| 269 | fn otdimok v ulong to bool |
| 270 | ret le v 4095 |
| 271 | end |
| 272 | |
| 273 | note Decide one matched m/p rule: 0 ok, 21 range, 22 shape. |
| 274 | fn otdecide pair bool to ulong |
| 275 | bad = false |
| 276 | if not otdimok get otsv 0 |
| 277 | bad = true |
| 278 | end |
| 279 | if not otdimok get otsv 1 |
| 280 | bad = true |
| 281 | end |
| 282 | if not otdimok get otsv 3 |
| 283 | bad = true |
| 284 | end |
| 285 | if gt get otsv 4 65535 |
| 286 | bad = true |
| 287 | end |
| 288 | if pair |
| 289 | if gt get otsv 5 65535 |
| 290 | bad = true |
| 291 | end |
| 292 | end |
| 293 | if eq get otss 2 1 |
| 294 | if not otdimok get otsv 2 |
| 295 | bad = true |
| 296 | end |
| 297 | end |
| 298 | if bad |
| 299 | ret 21 |
| 300 | end |
| 301 | if eq get otss 2 1 |
| 302 | if ne get otsv 2 get otsv 1 |
| 303 | ret 22 |
| 304 | end |
| 305 | end |
| 306 | ret 0 |
| 307 | end |
| 308 | |
| 309 | note Parse the normalized tokens. Returns the exit code; on 0 the reading |
| 310 | note is in otrd (kind, m, k, n, s, t). |
| 311 | fn otparse to ulong |
| 312 | nr = ulong 0 |
| 313 | amb = false |
| 314 | r = ulong 0 |
| 315 | while lt r len otrules bound 64 |
| 316 | cl = get otrules r |
| 317 | for i ulong 6 |
| 318 | put otsv i 0 |
| 319 | put otss i 0 |
| 320 | end |
| 321 | hit = otmat add r 2 0 |
| 322 | if hit |
| 323 | if eq cl 120 |
| 324 | amb = true |
| 325 | else |
| 326 | pair = eq cl 112 |
| 327 | code = otdecide pair |
| 328 | kind = ulong 1 |
| 329 | if pair |
| 330 | kind = 2 |
| 331 | end |
| 332 | if eq nr 0 |
| 333 | put otrd 0 kind |
| 334 | put otrd 1 code |
| 335 | put otrd 2 get otsv 0 |
| 336 | put otrd 3 get otsv 1 |
| 337 | put otrd 4 get otsv 3 |
| 338 | put otrd 5 get otsv 4 |
| 339 | put otrd 6 get otsv 5 |
| 340 | else |
| 341 | if not otsame kind code |
| 342 | amb = true |
| 343 | end |
| 344 | end |
| 345 | nr = add nr 1 |
| 346 | end |
| 347 | end |
| 348 | while ne get otrules r 10 bound 4096 |
| 349 | r = add r 1 |
| 350 | end |
| 351 | r = add r 1 |
| 352 | end |
| 353 | if amb |
| 354 | ret 23 |
| 355 | end |
| 356 | if eq nr 0 |
| 357 | ret 20 |
| 358 | end |
| 359 | ret get otrd 1 |
| 360 | end |
| 361 | |
| 362 | note The current match gives the same reading as the stored one. |
| 363 | fn otsame kind ulong code ulong to bool |
| 364 | if ne kind get otrd 0 |
| 365 | ret false |
| 366 | end |
| 367 | if ne code get otrd 1 |
| 368 | ret false |
| 369 | end |
| 370 | if ne get otsv 0 get otrd 2 |
| 371 | ret false |
| 372 | end |
| 373 | if ne get otsv 1 get otrd 3 |
| 374 | ret false |
| 375 | end |
| 376 | if ne get otsv 3 get otrd 4 |
| 377 | ret false |
| 378 | end |
| 379 | if ne get otsv 4 get otrd 5 |
| 380 | ret false |
| 381 | end |
| 382 | if eq kind 2 |
| 383 | if ne get otsv 5 get otrd 6 |
| 384 | ret false |
| 385 | end |
| 386 | end |
| 387 | ret true |
| 388 | end |
| 389 | |
| 390 | fn otemd v ulong to void |
| 391 | otpfr 3 |
| 392 | otphex v 3 |
| 393 | otpb 32 |
| 394 | end |
| 395 | |
| 396 | fn otems v ulong to void |
| 397 | otpfr 4 |
| 398 | otphex v 4 |
| 399 | otpb 32 |
| 400 | end |
| 401 | |
| 402 | note Append the G line for the reading in otrd. |
| 403 | fn otemitg to void |
| 404 | mv = get otrd 2 |
| 405 | kv = get otrd 3 |
| 406 | nv = get otrd 4 |
| 407 | otpfr 0 |
| 408 | if eq get otrd 0 2 |
| 409 | otpfr 2 |
| 410 | else |
| 411 | otpfr 1 |
| 412 | end |
| 413 | otemd mv |
| 414 | otemd kv |
| 415 | otemd nv |
| 416 | otems get otrd 5 |
| 417 | if eq get otrd 0 2 |
| 418 | otems get otrd 6 |
| 419 | end |
| 420 | otpfr 5 |
| 421 | end |
| 422 | |
| 423 | note en2goal on otin[0..n): fills otob with the G line or the typed refusal |
| 424 | note line and returns the exit code (0, 20, 21, 22, 23). |
| 425 | fn otengoal n ulong to int |
| 426 | if gt n 0 |
| 427 | if eq get otin sub n 1 10 |
| 428 | n = sub n 1 |
| 429 | end |
| 430 | end |
| 431 | code = ulong 20 |
| 432 | if le n 512 |
| 433 | if otnorm n |
| 434 | ottoken |
| 435 | if gt get otnt 0 0 |
| 436 | code = otparse |
| 437 | end |
| 438 | end |
| 439 | end |
| 440 | if eq code 0 |
| 441 | otemitg |
| 442 | ret 0 |
| 443 | end |
| 444 | otpfr sub code 14 |
| 445 | ret int code |
| 446 | end |