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 / src / visor / visor_parse_command.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 Visor lane 2: console command parser |
| 3 | note (replaces src/visor/visor_parse_command.c). Depends on visor.om. |
| 4 | |
| 5 | note Command codes: |
| 6 | note 0=help 1=quit 2=inspect 3=type 4=id 5=graph 6=verify 7=machine 8=realize |
| 7 | note 9=cost 10=run 11=evidence 12=bindings 13=clear 14=world 15=effects |
| 8 | note 16=alternatives 17=compare 18=why 19=source 20=empty 21=unknown |
| 9 | |
| 10 | text vpc_c_help "help" |
| 11 | text vpc_c_quit "quit" |
| 12 | text vpc_c_inspect "inspect" |
| 13 | text vpc_c_type "type" |
| 14 | text vpc_c_id "id" |
| 15 | text vpc_c_graph "graph" |
| 16 | text vpc_c_verify "verify" |
| 17 | text vpc_c_machine "machine" |
| 18 | text vpc_c_realize "realize" |
| 19 | text vpc_c_cost "cost" |
| 20 | text vpc_c_run "run" |
| 21 | text vpc_c_evidence "evidence" |
| 22 | text vpc_c_bindings "bindings" |
| 23 | text vpc_c_clear "clear" |
| 24 | text vpc_c_world "world" |
| 25 | text vpc_c_effects "effects" |
| 26 | text vpc_c_alt "alternatives" |
| 27 | text vpc_c_cmp "compare" |
| 28 | text vpc_c_why "why" |
| 29 | text vpc_c_source "source" |
| 30 | text vpc_c_empty "empty" |
| 31 | text vpc_c_unk "unknown" |
| 32 | text vpc_w_for "for" |
| 33 | text vpc_w_cur "current.machine" |
| 34 | text vpc_e_long "line too long" |
| 35 | text vpc_e_ctrl "control character is not allowed" |
| 36 | text vpc_e_utf8 "invalid UTF-8 sequence" |
| 37 | text vpc_e_tok "argument too long" |
| 38 | text vpc_e_arity "wrong number of arguments" |
| 39 | text vpc_e_real "realize only accepts 'for current.machine'" |
| 40 | |
| 41 | global vpc_cmd ulong 1 |
| 42 | global vpc_argc ulong 1 |
| 43 | global vpc_args uchar 1024 |
| 44 | global vpc_alen ulong 4 |
| 45 | global vpc_for_cur ulong 1 |
| 46 | global vpc_line uchar 4096 |
| 47 | global vpc_llen ulong 1 |
| 48 | global vpc_err uchar 256 |
| 49 | global vpc_elen ulong 1 |
| 50 | global vpc_tok0 uchar 256 |
| 51 | global vpc_t0len ulong 1 |
| 52 | |
| 53 | fn vpc_set_err s view uchar * to void |
| 54 | n = len s |
| 55 | if gt n 255 |
| 56 | n = 255 |
| 57 | end |
| 58 | for i ulong n bound 256 |
| 59 | put vpc_err i get s i |
| 60 | end |
| 61 | put vpc_elen 0 n |
| 62 | end |
| 63 | |
| 64 | fn vpc_utf8_step_sbuf n ulong pos ulong to ulong |
| 65 | b = ulong get v_sbuf pos |
| 66 | clen = ulong 0 |
| 67 | cp0 = ulong 0 |
| 68 | cmin = ulong 0 |
| 69 | if eq and b 0xe0 0xc0 |
| 70 | clen = 2 |
| 71 | cp0 = and b 0x1f |
| 72 | cmin = 0x80 |
| 73 | else |
| 74 | if eq and b 0xf0 0xe0 |
| 75 | clen = 3 |
| 76 | cp0 = and b 0x0f |
| 77 | cmin = 0x800 |
| 78 | else |
| 79 | if eq and b 0xf8 0xf0 |
| 80 | clen = 4 |
| 81 | cp0 = and b 0x07 |
| 82 | cmin = 0x10000 |
| 83 | else |
| 84 | ret 0 |
| 85 | end |
| 86 | end |
| 87 | end |
| 88 | if gt add pos clen n |
| 89 | ret 0 |
| 90 | end |
| 91 | cp = cp0 |
| 92 | k = ulong 1 |
| 93 | while lt k clen bound 4 |
| 94 | c = ulong get v_sbuf add pos k |
| 95 | if ne and c 0xc0 0x80 |
| 96 | ret 0 |
| 97 | end |
| 98 | cp = or shl cp 6 and c 0x3f |
| 99 | k = add k 1 |
| 100 | end |
| 101 | if or lt cp cmin gt cp 0x10ffff |
| 102 | ret 0 |
| 103 | end |
| 104 | if and ge cp 0xd800 le cp 0xdfff |
| 105 | ret 0 |
| 106 | end |
| 107 | ret clen |
| 108 | end |
| 109 | |
| 110 | fn vpc_check_bytes_sbuf n ulong to ulong |
| 111 | i = ulong 0 |
| 112 | while lt i n bound 4096 |
| 113 | b = ulong get v_sbuf i |
| 114 | if lt b 0x80 |
| 115 | if and lt b 0x20 ne b 9 |
| 116 | ret 1 |
| 117 | end |
| 118 | if eq b 0x7f |
| 119 | ret 1 |
| 120 | end |
| 121 | i = add i 1 |
| 122 | else |
| 123 | adv = vpc_utf8_step_sbuf n i |
| 124 | if eq adv 0 |
| 125 | ret 2 |
| 126 | end |
| 127 | i = add i adv |
| 128 | end |
| 129 | end |
| 130 | ret 0 |
| 131 | end |
| 132 | |
| 133 | fn vpc_eq_tok0 s view uchar * to bool |
| 134 | n = len s |
| 135 | if ne get vpc_t0len 0 n |
| 136 | ret false |
| 137 | end |
| 138 | for i ulong n bound 64 |
| 139 | if ne get vpc_tok0 i get s i |
| 140 | ret false |
| 141 | end |
| 142 | end |
| 143 | ret true |
| 144 | end |
| 145 | |
| 146 | fn vpc_eq_arg idx ulong s view uchar * to bool |
| 147 | n = len s |
| 148 | if ne get vpc_alen idx n |
| 149 | ret false |
| 150 | end |
| 151 | base = mul idx 256 |
| 152 | for i ulong n bound 64 |
| 153 | if ne get vpc_args add base i get s i |
| 154 | ret false |
| 155 | end |
| 156 | end |
| 157 | ret true |
| 158 | end |
| 159 | |
| 160 | fn vpc_lookup_cmd to ulong |
| 161 | if vpc_eq_tok0 vpc_c_help |
| 162 | ret 0 |
| 163 | end |
| 164 | if vpc_eq_tok0 vpc_c_quit |
| 165 | ret 1 |
| 166 | end |
| 167 | if vpc_eq_tok0 vpc_c_inspect |
| 168 | ret 2 |
| 169 | end |
| 170 | if vpc_eq_tok0 vpc_c_type |
| 171 | ret 3 |
| 172 | end |
| 173 | if vpc_eq_tok0 vpc_c_id |
| 174 | ret 4 |
| 175 | end |
| 176 | if vpc_eq_tok0 vpc_c_graph |
| 177 | ret 5 |
| 178 | end |
| 179 | if vpc_eq_tok0 vpc_c_verify |
| 180 | ret 6 |
| 181 | end |
| 182 | if vpc_eq_tok0 vpc_c_machine |
| 183 | ret 7 |
| 184 | end |
| 185 | if vpc_eq_tok0 vpc_c_realize |
| 186 | ret 8 |
| 187 | end |
| 188 | if vpc_eq_tok0 vpc_c_cost |
| 189 | ret 9 |
| 190 | end |
| 191 | if vpc_eq_tok0 vpc_c_run |
| 192 | ret 10 |
| 193 | end |
| 194 | if vpc_eq_tok0 vpc_c_evidence |
| 195 | ret 11 |
| 196 | end |
| 197 | if vpc_eq_tok0 vpc_c_bindings |
| 198 | ret 12 |
| 199 | end |
| 200 | if vpc_eq_tok0 vpc_c_clear |
| 201 | ret 13 |
| 202 | end |
| 203 | if vpc_eq_tok0 vpc_c_world |
| 204 | ret 14 |
| 205 | end |
| 206 | if vpc_eq_tok0 vpc_c_effects |
| 207 | ret 15 |
| 208 | end |
| 209 | if vpc_eq_tok0 vpc_c_alt |
| 210 | ret 16 |
| 211 | end |
| 212 | if vpc_eq_tok0 vpc_c_cmp |
| 213 | ret 17 |
| 214 | end |
| 215 | if vpc_eq_tok0 vpc_c_why |
| 216 | ret 18 |
| 217 | end |
| 218 | ret 19 |
| 219 | end |
| 220 | |
| 221 | fn vpc_arity_ok cmd ulong ac ulong to bool |
| 222 | if or eq cmd 0 or eq cmd 11 or eq cmd 14 eq cmd 15 |
| 223 | ret le ac 1 |
| 224 | end |
| 225 | if or eq cmd 1 or eq cmd 7 or eq cmd 12 eq cmd 13 |
| 226 | ret eq ac 0 |
| 227 | end |
| 228 | if eq cmd 10 |
| 229 | ret and ge ac 1 le ac 4 |
| 230 | end |
| 231 | if eq cmd 17 |
| 232 | ret eq ac 2 |
| 233 | end |
| 234 | if lt cmd 19 |
| 235 | ret eq ac 1 |
| 236 | end |
| 237 | ret true |
| 238 | end |
| 239 | |
| 240 | fn vpc_parse_sbuf rlen ulong to long |
| 241 | put vpc_cmd 0 21 |
| 242 | put vpc_argc 0 0 |
| 243 | put vpc_for_cur 0 0 |
| 244 | put vpc_llen 0 0 |
| 245 | put vpc_elen 0 0 |
| 246 | n = rlen |
| 247 | if gt n 0 |
| 248 | if eq get v_sbuf sub n 1 uchar 10 |
| 249 | n = sub n 1 |
| 250 | end |
| 251 | end |
| 252 | if gt n 0 |
| 253 | if eq get v_sbuf sub n 1 uchar 13 |
| 254 | n = sub n 1 |
| 255 | end |
| 256 | end |
| 257 | if gt n 4095 |
| 258 | vpc_set_err vpc_e_long |
| 259 | ret -1 |
| 260 | end |
| 261 | chk = vpc_check_bytes_sbuf n |
| 262 | if eq chk 1 |
| 263 | vpc_set_err vpc_e_ctrl |
| 264 | ret -1 |
| 265 | end |
| 266 | if eq chk 2 |
| 267 | vpc_set_err vpc_e_utf8 |
| 268 | ret -1 |
| 269 | end |
| 270 | a = ulong 0 |
| 271 | b = n |
| 272 | while lt a b bound 4096 |
| 273 | c = ulong get v_sbuf a |
| 274 | if or eq c 32 eq c 9 |
| 275 | a = add a 1 |
| 276 | else |
| 277 | break |
| 278 | end |
| 279 | end |
| 280 | while gt b a bound 4096 |
| 281 | c = ulong get v_sbuf sub b 1 |
| 282 | if or eq c 32 eq c 9 |
| 283 | b = sub b 1 |
| 284 | else |
| 285 | break |
| 286 | end |
| 287 | end |
| 288 | tlen = sub b a |
| 289 | for i ulong tlen bound 4096 |
| 290 | put vpc_line i get v_sbuf add a i |
| 291 | end |
| 292 | put vpc_llen 0 tlen |
| 293 | if eq tlen 0 |
| 294 | put vpc_cmd 0 20 |
| 295 | ret 0 |
| 296 | end |
| 297 | if eq get vpc_line 0 uchar 35 |
| 298 | put vpc_cmd 0 20 |
| 299 | ret 0 |
| 300 | end |
| 301 | note Extract first word |
| 302 | p = ulong 0 |
| 303 | while lt p tlen bound 256 |
| 304 | c = ulong get vpc_line p |
| 305 | if or eq c 32 eq c 9 |
| 306 | break |
| 307 | end |
| 308 | put vpc_tok0 p uchar c |
| 309 | p = add p 1 |
| 310 | end |
| 311 | put vpc_t0len 0 p |
| 312 | cmd = vpc_lookup_cmd |
| 313 | if eq cmd 19 |
| 314 | put vpc_cmd 0 19 |
| 315 | ret 0 |
| 316 | end |
| 317 | put vpc_cmd 0 cmd |
| 318 | ac = ulong 0 |
| 319 | while lt p tlen bound 4096 |
| 320 | while lt p tlen bound 4096 |
| 321 | c = ulong get vpc_line p |
| 322 | if or eq c 32 eq c 9 |
| 323 | p = add p 1 |
| 324 | else |
| 325 | break |
| 326 | end |
| 327 | end |
| 328 | if ge p tlen |
| 329 | break |
| 330 | end |
| 331 | if ge ac 4 |
| 332 | vpc_set_err vpc_e_arity |
| 333 | ret -1 |
| 334 | end |
| 335 | start = p |
| 336 | while lt p tlen bound 4096 |
| 337 | c = ulong get vpc_line p |
| 338 | if or eq c 32 eq c 9 |
| 339 | break |
| 340 | end |
| 341 | p = add p 1 |
| 342 | end |
| 343 | al = sub p start |
| 344 | if gt al 255 |
| 345 | vpc_set_err vpc_e_tok |
| 346 | ret -1 |
| 347 | end |
| 348 | vpc_store_arg ac start al |
| 349 | ac = add ac 1 |
| 350 | end |
| 351 | if and eq cmd 8 eq ac 3 |
| 352 | if and vpc_eq_arg 1 vpc_w_for vpc_eq_arg 2 vpc_w_cur |
| 353 | put vpc_for_cur 0 1 |
| 354 | ac = 1 |
| 355 | else |
| 356 | vpc_set_err vpc_e_real |
| 357 | ret -1 |
| 358 | end |
| 359 | end |
| 360 | put vpc_argc 0 ac |
| 361 | if not vpc_arity_ok cmd ac |
| 362 | vpc_set_err vpc_e_arity |
| 363 | ret -1 |
| 364 | end |
| 365 | ret 0 |
| 366 | end |
| 367 | |
| 368 | fn vpc_store_arg ac ulong start ulong al ulong to void |
| 369 | base = mul ac 256 |
| 370 | for i ulong al bound 256 |
| 371 | put vpc_args add base i get vpc_line add start i |
| 372 | end |
| 373 | put vpc_alen ac al |
| 374 | end |
| 375 | |
| 376 | fn vpc_put_cmd_name cmd ulong to void |
| 377 | if eq cmd 0 |
| 378 | ob_puts vpc_c_help |
| 379 | ret |
| 380 | end |
| 381 | if eq cmd 1 |
| 382 | ob_puts vpc_c_quit |
| 383 | ret |
| 384 | end |
| 385 | if eq cmd 2 |
| 386 | ob_puts vpc_c_inspect |
| 387 | ret |
| 388 | end |
| 389 | if eq cmd 3 |
| 390 | ob_puts vpc_c_type |
| 391 | ret |
| 392 | end |
| 393 | if eq cmd 4 |
| 394 | ob_puts vpc_c_id |
| 395 | ret |
| 396 | end |
| 397 | if eq cmd 5 |
| 398 | ob_puts vpc_c_graph |
| 399 | ret |
| 400 | end |
| 401 | if eq cmd 6 |
| 402 | ob_puts vpc_c_verify |
| 403 | ret |
| 404 | end |
| 405 | if eq cmd 7 |
| 406 | ob_puts vpc_c_machine |
| 407 | ret |
| 408 | end |
| 409 | if eq cmd 8 |
| 410 | ob_puts vpc_c_realize |
| 411 | ret |
| 412 | end |
| 413 | if eq cmd 9 |
| 414 | ob_puts vpc_c_cost |
| 415 | ret |
| 416 | end |
| 417 | if eq cmd 10 |
| 418 | ob_puts vpc_c_run |
| 419 | ret |
| 420 | end |
| 421 | if eq cmd 11 |
| 422 | ob_puts vpc_c_evidence |
| 423 | ret |
| 424 | end |
| 425 | if eq cmd 12 |
| 426 | ob_puts vpc_c_bindings |
| 427 | ret |
| 428 | end |
| 429 | if eq cmd 13 |
| 430 | ob_puts vpc_c_clear |
| 431 | ret |
| 432 | end |
| 433 | if eq cmd 14 |
| 434 | ob_puts vpc_c_world |
| 435 | ret |
| 436 | end |
| 437 | if eq cmd 15 |
| 438 | ob_puts vpc_c_effects |
| 439 | ret |
| 440 | end |
| 441 | if eq cmd 16 |
| 442 | ob_puts vpc_c_alt |
| 443 | ret |
| 444 | end |
| 445 | if eq cmd 17 |
| 446 | ob_puts vpc_c_cmp |
| 447 | ret |
| 448 | end |
| 449 | if eq cmd 18 |
| 450 | ob_puts vpc_c_why |
| 451 | ret |
| 452 | end |
| 453 | if eq cmd 19 |
| 454 | ob_puts vpc_c_source |
| 455 | ret |
| 456 | end |
| 457 | if eq cmd 20 |
| 458 | ob_puts vpc_c_empty |
| 459 | ret |
| 460 | end |
| 461 | ob_puts vpc_c_unk |
| 462 | end |