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/babel/blob/main/src/40-gen.om # babel aien-dev/babel Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `babel / src / 40-gen.om` 595 lines ยท 15529 bytes Raw Blame History 1 note babel 40-gen: target packaging, differential drivers and generated tests. 2 note Adapter outputs are stored verbatim under units/; everything here is babel glue: re-exports 3 note under the source names, drivers that read "id n" then n vectors and print one result per line, 4 note and tests whose expected values are the original C function's outputs. 5 global Cv ulong 1 6 global Co ulong 1 7 text Comma ", " 8 text ArgA "a" 9 text ArgX "x[" 10 text ArgXe "]" 11 text GRs1 "[package]\nname = \"" 12 text GRs2 "\"\nversion = \"0.1.0\"\nedition = \"2021\"\npublish = false\n\n[lib]\npath = \"src/lib.rs\"\n\n[[bin]]\nname = \"babel_diff\"\npath = \"src/bin/babel_diff.rs\"\n" 13 text GRsL "//! Generated by babel. Every `units/.rs` is the verbatim output of the omega-mv\n//! `rust-export` adapter (profile S1, c17-uint64-pure-v1); this file only re-exports\n//! each adapter function `calc` under its source name. See ../REPORT.md.\n\n" 14 text GRsM1 "#[path = \"units/" 15 text GRsM2 ".rs\"]\npub mod " 16 text GRsM3 ";\npub use " 17 text GRsM4 "::calc as " 18 text GRsM5 ";\n\n" 19 text GRsB1 "//! Generated by babel: differential driver.\n//! stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\nuse std::io::Read;\n\nfn take(it: &mut std::str::SplitAsciiWhitespace<'_>) -> u64 {\n it.next().expect(\"vector value\").parse().expect(\"u64\")\n}\n\nfn main() {\n let mut s = String::new();\n std::io::stdin().read_to_string(&mut s).expect(\"stdin\");\n let mut it = s.split_ascii_whitespace();\n let id: u64 = match it.next() {\n Some(t) => t.parse().expect(\"id\"),\n None => return,\n };\n let n = take(&mut it);\n let mut out = String::new();\n for _ in 0..n {\n let r: u64 = match id {\n" 20 text GRsB2 " => {\n" 21 text GRsB3 " let a" 22 text GRsB4 " = take(&mut it);\n" 23 text GRsB5 " " 24 text GRsB6 "\n }\n" 25 text GRsB7 " _ => panic!(\"unknown unit id {id}\"),\n };\n out.push_str(&r.to_string());\n out.push('\\n');\n }\n print!(\"{out}\");\n}\n" 26 text Ind12 " " 27 text Ind8 " " 28 text GRsT0 "//! Generated by babel: the expected values are the outputs of the ORIGINAL C functions\n//! (compiled by cc from the input, verbatim) on the vectors in ../diff//vectors.txt.\n" 29 text GRsT1 "\n#[test]\nfn " 30 text GRsT2 "_matches_original_c() {\n let cases: &[(&[u64], u64)] = &[\n" 31 text GRsT3 " ];\n for (a, want) in cases {\n assert_eq!(" 32 text GRsT4 ", *want, \"inputs {:?}\", a);\n }\n}\n" 33 text GRsC1 "(&[" 34 text GRsC2 "], " 35 text GRsC3 "),\n" 36 text Dc2 "::" 37 text LPar "(" 38 text RPar ")" 39 text GPyI1 "\"\"\"Generated by babel. Each units/.py is the verbatim output of the omega-mv\npython-export adapter (profile S1); this file only re-exports `calc` under the source name.\"\"\"\n\n" 40 text GPyI2 "from .units." 41 text GPyI3 " import calc as " 42 text GPyI4 "\n__all__ = [\n" 43 text GPyI5 " \"" 44 text GPyI6 "\",\n" 45 text GPyI7 "]\n" 46 text GPyU "\"\"\"Verbatim python-export adapter outputs (see ../../REPORT.md).\"\"\"\n" 47 text GPyD1 "\"\"\"Generated by babel: differential driver.\n\nstdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\n\"\"\"\nimport sys\n\nimport " 48 text GPyD2 "\n\nUNITS = {\n" 49 text GPyD3 " " 50 text GPyD4 ": (" 51 text GPyD5 "),\n" 52 text GPyD6 "}\n\n\ndef main():\n data = sys.stdin.read().split()\n if not data:\n return\n fn, k = UNITS[int(data[0])]\n n = int(data[1])\n pos = 2\n out = []\n for _ in range(n):\n args = [int(x) for x in data[pos:pos + k]]\n pos += k\n out.append(\"%d\\n\" % fn(*args))\n sys.stdout.write(\"\".join(out))\n\n\nmain()\n" 53 text Dot "." 54 text GPyT1 "\"\"\"Generated by babel: expected values are the outputs of the ORIGINAL C functions\n(compiled by cc from the input, verbatim) on the vectors in ../diff//vectors.txt.\n\"\"\"\nimport unittest\n\nimport " 55 text GPyT2 "\n\n\nclass Differential(unittest.TestCase):\n" 56 text GPyT3 " def test_" 57 text GPyT4 "(self):\n cases = [\n" 58 text GPyT5 " ]\n for args, want in cases:\n self.assertEqual(" 59 text GPyT6 "(*args), want, args)\n\n" 60 text GPyT7 "\nif __name__ == \"__main__\":\n unittest.main()\n" 61 text GPyC1 " ((" 62 text GPyC2 "), " 63 text GPyC3 "),\n" 64 text CommaO "," 65 text GCh1 "/* Generated by babel: prototypes of the verbatim c-export adapter outputs in units/. */\n#ifndef BABEL_GENERATED_H\n#define BABEL_GENERATED_H\n#include \n\n" 66 text GCh2 "uint64_t " 67 text GCh3 "(" 68 text GChV "void" 69 text GChU "uint64_t" 70 text GCh4 ");\n" 71 text GCh5 "\n#endif\n" 72 text GCd1 "/* Generated by babel: differential driver.\n * stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line. */\n#include \n#include \"" 73 text GCd2 ".h\"\n\nint main(void) {\n unsigned long long id, n, a[6] = {0};\n if (scanf(\"%llu %llu\", &id, &n) != 2) return 0;\n for (unsigned long long i = 0; i < n; i++) {\n uint64_t r;\n switch (id) {\n" 74 text GCd3 " case " 75 text GCd4 ": {\n for (int k = 0; k < " 76 text GCd5 "; k++) if (scanf(\"%llu\", &a[k]) != 1) return 2;\n r = " 77 text GCd6 ");\n break;\n }\n" 78 text GCd7 " default: return 3;\n }\n printf(\"%llu\\n\", (unsigned long long)r);\n }\n return 0;\n}\n" 79 text ArgAk "a[" 80 text GOr1 "/* Generated by babel: differential oracle. The function below is the ORIGINAL C source,\n * verbatim from " 81 text GOr2 ", followed by a driver.\n * stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line. */\n#include \n#include \n\n" 82 text GOr3 "\n\nint main(void) {\n unsigned long long id, n, a[6] = {0};\n if (scanf(\"%llu %llu\", &id, &n) != 2) return 0;\n (void)id;\n for (unsigned long long i = 0; i < n; i++) {\n for (int k = 0; k < " 83 text GOr4 "; k++) if (scanf(\"%llu\", &a[k]) != 1) return 2;\n printf(\"%llu\\n\", (unsigned long long)" 84 text GOr5 "));\n }\n return 0;\n}\n" 85 text GCp1 "// Generated by babel: differential driver for one verbatim cpp-export output.\n// stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\n#include \n#include \"../units/" 86 text GCp2 ".cpp\"\n" 87 text Colon ":" 88 text Dash "-" 89 text Calc "calc" 90 text GZl1 "//! Generated by babel: re-exports the verbatim zig-export outputs in units/ under source names.\n" 91 text GZl2 "pub const " 92 text GZl3 " = @import(\"units/" 93 text GZl4 ".zig\").calc;\n" 94 text GZd1 "//! Generated by babel: differential driver.\n//! stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\nconst lib = @import(\"lib.zig\");\nextern \"c\" fn scanf(fmt: [*:0]const u8, ...) c_int;\nextern \"c\" fn printf(fmt: [*:0]const u8, ...) c_int;\n\npub fn main() void {\n var id: u64 = 0;\n var n: u64 = 0;\n if (scanf(\"%llu %llu\", &id, &n) != 2) return;\n var x: [6]u64 = .{ 0, 0, 0, 0, 0, 0 };\n _ = &x;\n var i: u64 = 0;\n while (i < n) : (i += 1) {\n const r: u64 = switch (id) {\n" 95 text GZd2 " => blk: {\n for (0.." 96 text GZd3 ") |k| {\n if (scanf(\"%llu\", &x[k]) != 1) return;\n }\n break :blk lib." 97 text GZd4 ");\n },\n" 98 text GZd5 " else => return,\n };\n _ = printf(\"%llu\\n\", r);\n }\n}\n" 99 text GGm1 "module " 100 text GGm2 "\n\ngo 1.22\n" 101 text GGd1 "// Code generated by babel. DO NOT EDIT.\n// Differential driver for the verbatim go-export output in calc.go.\n// stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\npackage main\n\nimport (\n\t\"bufio\"\n\t\"fmt\"\n\t\"os\"\n)\n\nfunc main() {\n\tin := bufio.NewReader(os.Stdin)\n\tout := bufio.NewWriter(os.Stdout)\n\tdefer out.Flush()\n\tvar id, n uint64\n\tif _, err := fmt.Fscan(in, &id, &n); err != nil {\n\t\treturn\n\t}\n\tvar x [6]uint64\n\tfor i := uint64(0); i < n; i++ {\n\t\tfor k := 0; k < " 102 text GGd2 "; k++ {\n\t\t\tif _, err := fmt.Fscan(in, &x[k]); err != nil {\n\t\t\t\tos.Exit(2)\n\t\t\t}\n\t\t}\n\t\tfmt.Fprintln(out, calc(" 103 text GGd3 "))\n\t}\n}\n" 104 text GOm1 "\nnote babel differential driver: reads \"id n\" then n vectors of decimal ulong, prints one result per line\nglobal BbIn uchar `1048576`\nglobal BbOut uchar `1048576`\nglobal BbPos ulong 1\nglobal BbLen ulong 1\nglobal BbOl ulong 1\nfn babel_next to ulong\np = get BbPos 0\nn = get BbLen 0\nwhile lt p n bound 999999\nc = get BbIn p\nif ge c 48\nif le c 57\nbreak\nend\nend\np = add p 1\nend\nv = ulong 0\nwhile lt p n bound 64\nc = get BbIn p\nif lt c 48\nbreak\nend\nif gt c 57\nbreak\nend\nv = add mul v 10 ulong sub c 48\np = add p 1\nend\nput BbPos 0 p\nret v\nend\nfn babel_put v ulong to void\nd = new uchar 24 0\nk = ulong 0\nx = v\nwhile true bound 30\nput d k uchar add 48 rem x 10\nk = add k 1\nx = div x 10\nif eq x 0\nbreak\nend\nend\no = get BbOl 0\nwhile gt k 0 bound 30\nk = sub k 1\nput BbOut o get d k\no = add o 1\nend\nput BbOut o 10\nput BbOl 0 add o 1\nend\nfn main to int\nn = ulong 0\nwhile lt n len BbIn bound 999999\nr = read 0 BbIn n sub len BbIn n\nif le r 0\nbreak\nend\nn = add n ulong r\nend\nput BbLen 0 n\nix = babel_next\ncnt = babel_next\nfor j ulong cnt\n" 105 text GOm2 " = babel_next\n" 106 text GOm3 "r = " 107 text GOm4 "babel_put r\nend\nw = write 1 BbOut 0 get BbOl 0\nret 0\nend\n" 108 text Space " " 109 110 note "pre0pre1..." for k arguments, with a closing text after each index 111 note (omc `2c824805` passes a garbage length for an empty text, so there is no empty-post form) 112 fn bargs pre view uchar * post view uchar * k ulong to void 113 for i ulong k 114 if gt i 0 115 bs Comma 116 end 117 bs pre 118 bu i 119 bs post 120 end 121 end 122 123 fn bargs0 pre view uchar * k ulong to void 124 for i ulong k 125 if gt i 0 126 bs Comma 127 end 128 bs pre 129 bu i 130 end 131 end 132 133 fn bpkg to void 134 bpl get Pg0 0 get Pgn 0 135 end 136 137 note write the arena from m to the file /; resets the arena to m 138 fn wout m ulong s view uchar * to long 139 c = mark 140 bouts s 141 p = mark 142 r = wfile m c c p 143 reset m 144 ret r 145 end 146 147 note same with / 148 fn woutn m ulong s view uchar * u ulong t view uchar * to long 149 c = mark 150 bouts s 151 bname u 152 bs t 153 p = mark 154 r = wfile m c c p 155 reset m 156 ret r 157 end 158 159 fn exported u ulong to bool 160 ret eq get Ust u 7 161 end 162 163 text PCargo "rust/Cargo.toml" 164 text PRsLib "rust/src/lib.rs" 165 text PRsBin "rust/src/bin/babel_diff.rs" 166 text PRsTst "rust/tests/differential.rs" 167 168 fn genrust to void 169 m = mark 170 bs GRs1 171 bpkg 172 bs GRs2 173 w = wout m PCargo 174 bs GRsL 175 for u ulong get Nu 0 176 if exported u 177 bs GRsM1 178 bname u 179 bs GRsM2 180 bname u 181 bs GRsM3 182 bname u 183 bs GRsM4 184 bname u 185 bs GRsM5 186 end 187 end 188 w = wout m PRsLib 189 bs GRsB1 190 for u ulong get Nu 0 191 if exported u 192 k = get Uar u 193 bs Ind12 194 bu get Uid u 195 bs GRsB2 196 for i ulong k 197 bs GRsB3 198 bu i 199 bs GRsB4 200 end 201 bs GRsB5 202 bpkg 203 bs Dc2 204 bname u 205 bs LPar 206 bargs0 ArgA k 207 bs RPar 208 bs GRsB6 209 end 210 end 211 bs GRsB7 212 w = wout m PRsBin 213 end 214 215 216 note one generated case per vector: rust "(&[a, b], r),", python "((a, b), r)," 217 fn bcases u ulong py bool to void 218 put Cv 0 get Uv0 u 219 put Co 0 get Uo0 u 220 ve = add get Uv0 u get Uvn u 221 oe = add get Uo0 u get Uon u 222 skipln ve 223 k = get Uar u 224 for j ulong get Unv u 225 bcase k py ve oe 226 end 227 end 228 229 fn bcase k ulong py bool ve ulong oe ulong to void 230 if py 231 bs GPyC1 232 else 233 bs Ind8 234 bs GRsC1 235 end 236 v = get Cv 0 237 e = lnend v ve 238 for i ulong sub e v 239 c = get Pl add v i 240 if eq c 32 241 bs Comma 242 else 243 bc c 244 end 245 end 246 if py 247 if eq k 1 248 bs CommaO 249 end 250 bs GPyC2 251 else 252 bs GRsC2 253 end 254 put Cv 0 add e 1 255 o = get Co 0 256 f = lnend o oe 257 bpl o sub f o 258 put Co 0 add f 1 259 if py 260 bs GPyC3 261 else 262 bs GRsC3 263 end 264 end 265 266 fn lnend o ulong e ulong to ulong 267 i = o 268 while lt i e bound 999999 269 if eq get Pl i 10 270 ret i 271 end 272 i = add i 1 273 end 274 ret e 275 end 276 277 fn skipln e ulong to void 278 put Cv 0 add lnend get Cv 0 e 1 279 end 280 281 note tests may only use vectors whose oracle run succeeded (Uon > 0) 282 fn hascases u ulong to bool 283 ret gt get Uon u 0 284 end 285 286 fn anycase to bool 287 for u ulong get Nu 0 288 if hascases u 289 ret true 290 end 291 end 292 ret false 293 end 294 295 fn genrstest to void 296 m = mark 297 bs GRsT0 298 for u ulong get Nu 0 299 if hascases u 300 bs GRsT1 301 bname u 302 bs GRsT2 303 bcases u false 304 bs GRsT3 305 bpkg 306 bs Dc2 307 bname u 308 bs LPar 309 bargs ArgAk ArgXe get Uar u 310 bs RPar 311 bs GRsT4 312 end 313 end 314 w = wout m PRsTst 315 end 316 317 text PPyMain "python/babel_diff.py" 318 text PPyTst "python/test_differential.py" 319 text PInit "/__init__.py" 320 text PUInit "/units/__init__.py" 321 322 fn genpy to void 323 m = mark 324 bs GPyI1 325 for u ulong get Nu 0 326 if exported u 327 bs GPyI2 328 bname u 329 bs GPyI3 330 bname u 331 bs NL 332 end 333 end 334 bs GPyI4 335 for u ulong get Nu 0 336 if exported u 337 bs GPyI5 338 bname u 339 bs GPyI6 340 end 341 end 342 bs GPyI7 343 pyfile m PInit 344 bs GPyU 345 pyfile m PUInit 346 bs GPyD1 347 bpkg 348 bs GPyD2 349 for u ulong get Nu 0 350 if exported u 351 bs GPyD3 352 bu get Uid u 353 bs GPyD4 354 bpkg 355 bs Dot 356 bname u 357 bs Comma 358 bu get Uar u 359 bs GPyD5 360 end 361 end 362 bs GPyD6 363 w = wout m PPyMain 364 end 365 366 note write the arena from m to /python/ 367 fn pyfile m ulong s view uchar * to void 368 c = mark 369 bouts DPy 370 bpkg 371 bs s 372 p = mark 373 r = wfile m c c p 374 reset m 375 end 376 377 fn genpytest to void 378 m = mark 379 bs GPyT1 380 bpkg 381 bs GPyT2 382 for u ulong get Nu 0 383 if hascases u 384 bs GPyT3 385 bname u 386 bs GPyT4 387 bcases u true 388 bs GPyT5 389 bpkg 390 bs Dot 391 bname u 392 bs GPyT6 393 end 394 end 395 bs GPyT7 396 w = wout m PPyTst 397 end 398 399 text PCMain "c/babel_diff.c" 400 text PCDir "c/" 401 text ExHdr ".h" 402 403 fn genc to void 404 m = mark 405 bs GCh1 406 for u ulong get Nu 0 407 if exported u 408 bs GCh2 409 bpl get Ua0 u get Uan u 410 bs GCh3 411 k = get Uar u 412 if eq k 0 413 bs GChV 414 end 415 for i ulong k 416 if gt i 0 417 bs Comma 418 end 419 bs GChU 420 end 421 bs GCh4 422 end 423 end 424 bs GCh5 425 c = mark 426 bouts PCDir 427 bpkg 428 bs ExHdr 429 p = mark 430 w = wfile m c c p 431 reset m 432 bs GCd1 433 bpkg 434 bs GCd2 435 for u ulong get Nu 0 436 if exported u 437 bs GCd3 438 bu get Uid u 439 bs GCd4 440 bu get Uar u 441 bs GCd5 442 bpl get Ua0 u get Uan u 443 bs LPar 444 bargs ArgAk ArgXe get Uar u 445 bs GCd6 446 end 447 end 448 bs GCd7 449 w = wout m PCMain 450 end 451 452 note per-unit C++ driver cpp/diff/.cpp 453 text PCpD "cpp/diff/" 454 fn gencpp to void 455 for u ulong get Nu 0 456 if exported u 457 m = mark 458 bs GCp1 459 bname u 460 bs GCp2 461 bdrvmain get Uar u Calc 462 w = woutn m PCpD u Ext_cp 463 end 464 end 465 end 466 467 note the shared single-function C/C++ main used by the oracle and the C++ drivers 468 fn bdrvmain k ulong fname view uchar * to void 469 bs GOr3 470 bu k 471 bs GOr4 472 bs fname 473 bs LPar 474 bargs ArgAk ArgXe k 475 bs GOr5 476 end 477 478 text PZLib "zig/lib.zig" 479 text PZMain "zig/babel_diff.zig" 480 fn genzig to void 481 m = mark 482 bs GZl1 483 for u ulong get Nu 0 484 if exported u 485 bs GZl2 486 bname u 487 bs GZl3 488 bname u 489 bs GZl4 490 end 491 end 492 w = wout m PZLib 493 bs GZd1 494 for u ulong get Nu 0 495 if exported u 496 bs Ind12 497 bu get Uid u 498 bs GZd2 499 bu get Uar u 500 bs GZd3 501 bname u 502 bs LPar 503 bargs ArgX ArgXe get Uar u 504 bs GZd4 505 end 506 end 507 bs GZd5 508 w = wout m PZMain 509 end 510 511 text PGoMod "go/go.mod" 512 text PGoMain "/main.go" 513 fn gengo to void 514 m = mark 515 bs GGm1 516 bpkg 517 bs GGm2 518 w = wout m PGoMod 519 for u ulong get Nu 0 520 if exported u 521 bs GGd1 522 bu get Uar u 523 bs GGd2 524 bargs ArgX ArgXe get Uar u 525 bs GGd3 526 w = woutn m UGo u PGoMain 527 end 528 end 529 end 530 531 note diff//oracle.c: the original function text verbatim plus the shared driver 532 text PDiff "diff/" 533 text POrc "/oracle.c" 534 text POmd "/driver.om" 535 fn genoracle u ulong to void 536 m = mark 537 bs GOr1 538 fpath get Ufi u 539 bs Colon 540 bu get Ul0 u 541 bs Dash 542 bu get Ul1 u 543 bs GOr2 544 bpl get Ut0 u get Utn u 545 bdrvmainn u 546 w = woutn m PDiff u POrc 547 end 548 549 fn bdrvmainn u ulong to void 550 k = get Uar u 551 bs GOr3 552 bu k 553 bs GOr4 554 bname u 555 bs LPar 556 bargs ArgAk ArgXe k 557 bs GOr5 558 end 559 560 fn fpath fi ulong to void 561 bpl get F0 fi get Fl fi 562 end 563 564 note diff//driver.om: the lowered unit (omega/.om) followed by a program-mode main 565 fn genomdrv u ulong to bool 566 m = mark 567 bouts DOmega 568 bname u 569 bs ExOm 570 e = mark 571 r = rfile m e 572 reset m 573 if lt r 0 574 ret false 575 end 576 bfb 0 get Fn 0 577 bs GOm1 578 k = get Uar u 579 for i ulong k 580 bs ArgA 581 bu i 582 bs GOm2 583 end 584 bs GOm3 585 bpl get Uw0 u get Uwn u 586 for i ulong k 587 bs Space 588 bs ArgA 589 bu i 590 end 591 bs NL 592 bs GOm4 593 w = woutn m PDiff u POmd 594 ret true 595 end