@fraczak/k 7.0.2 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +75 -0
- package/DOCS/CODECS.md +37 -2
- package/DOCS/CODEC_ARCHITECTURE.md +12 -5
- package/DOCS/FILE_FORMATS.md +238 -15
- package/DOCS/POLYMORPHIC_IR.md +104 -0
- package/DOCS/REPL.md +11 -3
- package/DOCS/WEB_REPL_REQUIREMENTS.md +1 -1
- package/JOURNAL.md +288 -0
- package/README.md +12 -3
- package/Value.mjs +28 -3
- package/access-table.mjs +343 -0
- package/backend-api.mjs +23 -1
- package/backends/llvm/README.md +20 -21
- package/backends/llvm/bin/k-llvm-build.mjs +105 -32
- package/backends/llvm/bin/k-llvm-compile.mjs +63 -24
- package/backends/llvm/bin/k-llvm.mjs +292 -0
- package/backends/llvm/runtime/krt.c +8 -0
- package/backends/llvm/runtime/krt.h +1 -0
- package/backends/llvm/src/cli.mjs +95 -17
- package/backends/llvm/src/executable.mjs +128 -11
- package/backends/llvm/src/kvm2llvm.mjs +10 -5
- package/backends/llvm/src/llvm.mjs +121 -9
- package/backends/llvm/tests/integration.sh +11 -11
- package/backends/llvm/{bin → tests}/k-llvm-run.mjs +48 -22
- package/backends/llvm/tests/perf-support.mjs +24 -3
- package/backends/llvm/tests/test-llvm.mjs +22 -1
- package/backends/wasm/bin/k-wasm-compile.mjs +46 -20
- package/backends/wasm/bin/k-wasm-run.mjs +61 -20
- package/backends/wasm/bin/k-wasm.mjs +47 -13
- package/backends/wasm/src/cli.mjs +141 -19
- package/backends/wasm/src/wasm.mjs +130 -23
- package/browser/repl-browser.mjs +16 -5
- package/browser/tree-view.mjs +47 -8
- package/browser/vfs-data.mjs +6 -5
- package/codecs/README.md +1 -0
- package/codecs/djson.mjs +270 -0
- package/codecs/int.mjs +2 -1
- package/codecs/json.mjs +7 -2
- package/codecs/k-parse.mjs +36 -9
- package/codecs/k-print.mjs +26 -6
- package/codecs/runtime/codec-sdk.mjs +24 -6
- package/codecs/runtime/pattern-json.mjs +80 -1
- package/codecs/runtime/prefix-codec.mjs +629 -67
- package/codecs/runtime/show-value.mjs +93 -51
- package/codecs/runtime/unicode-string.mjs +29 -6
- package/codecs/show.mjs +24 -6
- package/codecs/utf16.mjs +2 -1
- package/k.mjs +56 -16
- package/kir.mjs +58 -19
- package/kvm.mjs +49 -12
- package/objects/compile.mjs +37 -23
- package/objects/decompile.mjs +40 -10
- package/objects/extract-aliases.mjs +44 -14
- package/objects/inspect.mjs +19 -14
- package/objects/validate.mjs +18 -11
- package/package.json +11 -3
- package/pkir.mjs +772 -0
- package/repl.mjs +92 -23
- package/scripts/build-repl-html.mjs +22 -3
- package/scripts/perf-ieee.mjs +264 -13
- package/scripts/perf-int.mjs +315 -22
- package/scripts/perf-multiplication.mjs +58 -7
- package/scripts/perf-poly.mjs +306 -24
- package/scripts/run-tests.mjs +3 -0
- package/tests/test-access-table.mjs +213 -0
- package/tests/test-codecs.mjs +168 -0
- package/tests/test-pkir.mjs +530 -0
- package/tests/test-polymorphic-tdd.mjs +196 -0
- package/tests/test-repl-html.mjs +16 -2
- package/tests/test-repl.mjs +63 -1
- package/backends/llvm/bin/k-llvm-jit.mjs +0 -160
package/browser/vfs-data.mjs
CHANGED
|
@@ -10,9 +10,10 @@ export const vfsData = {
|
|
|
10
10
|
"Examples/poly.k": "-- Examples/poly.k: Polymorphic list functions in k\n-- Uses arbitrary-precision integers from Examples/arithmetics.k.\n\n-- List constructors and destructors\nlist? = ?< {} nil, {X car, Y cdr} cons > = Y;\nnil = {} | nil list?;\nsingleton = {() car, nil cdr} | cons list?;\ncons = {.car car, .cdr cdr} | cons list?;\nnil? = list? / nil nil;\ncar = list? /cons .car;\ncdr = list? /cons .cdr;\n\n-- Decrement an integer and normalize (stripping leading zeros)\npred_int = dec /'+' nat | '+';\n\n-- Concatenate two lists: {xs, ys} -> list\nconcat = {.xs reverse xs, .ys acc} _reverse;\n\n-- Reverse a list: xs -> list\nreverse = {() xs, nil acc} _reverse;\n_reverse = <\n {.xs cdr xs, {.xs car car, .acc cdr} cons acc} _reverse,\n .acc\n>;\n\n-- Length of a list: xs -> int\nlength = {() xs, 0 int acc} _length;\n_length = <\n {.xs /nil if, .acc then} .then,\n {.xs cdr xs, .acc inc acc} _length\n>;\n\n-- Get N-th element of a list (0-indexed): {n, xs} -> element\nget_nth = <\n {.n zero_int? if, .xs car then} .then,\n {.n pred_int n, .xs cdr xs} get_nth\n>;\n\n-- Split list at index N: {n, xs} -> {first, second}\nsplit_by = {.n n, .xs xs, nil acc} _split_by;\n_split_by = <\n {.n zero_int? if, {.acc reverse first, .xs second} then} .then,\n {.xs /nil if, {.acc reverse first, nil second} then} .then,\n {.n pred_int n, .xs cdr xs, {.xs car car, .acc cdr} cons acc} _split_by\n>;\n\n-- Take the first N elements of a list: {n, xs} -> list\ntake = split_by .first;\n\n-- Drop the first N elements of a list: {n, xs} -> list\ndrop = <\n {.n zero_int? if, .xs then} .then,\n {.xs /nil if, nil then} .then,\n {.n pred_int n, .xs cdr xs} drop\n>;\n\n-- Repeat element X, N times: {n, x} -> list\nrepeat = {.n n, .x x, nil acc} _repeat;\n_repeat = <\n {.n zero_int? if, .acc then} .then,\n {.n pred_int n, .x x, {.x car, .acc cdr} cons acc} _repeat\n>;\n\n-- Append an element to the end of a list: {x, xs} -> list\nappend = {.xs xs, {.x car, nil cdr} cons ys} concat;\n\n-- Zip two lists into a list of {x, y} pairs: {xs, ys} -> list\nzip = <\n {.xs /nil if, nil then} .then,\n {.ys /nil if, nil then} .then,\n {\n {.xs car x, .ys car y} car,\n {.xs cdr xs, .ys cdr ys} zip cdr\n } cons\n>;\n\n-- Gen list of intgers: n -> [0, 1, ..., n]\ngen_list = {() n, nil list} gen_list_; \ngen_list_ = <\n {.n neg? if, .list then} .then, \n {.n dec n, {.n car, .list cdr} cons list} gen_list_\n>;\n\n-- Identity filter on {x, y} when x is greater than y: ?{X x, X y}=Y -> ?Y\ngt? =\n <\n {.x/- if1, .y/+ if2, () then} .then,\n {{.x/- y, .y/- x} GT if, () then} .then,\n {{.x/+ x, .y/+ y} GT if, () then} .then\n >\n;\n\n-- Split list into two halves by alternating elements:\n-- xs -> {first, second}\nsplit = {() xs, nil first, nil second} _split;\n_split = <\n {.xs /nil if, {.first first, .second second} then} .then,\n {.xs cdr /nil if, {{.xs car car, .first cdr} cons first, .second second} then} .then,\n {.xs cdr cdr xs, {.xs car car, .first cdr} cons first, {.xs cdr car car, .second cdr} cons second} _split\n>;\n\n-- Merge two sorted lists: {xs, ys} -> list\nmerge = <\n {.xs /nil if, .ys then} .then,\n {.ys /nil if, .xs then} .then,\n {\n {.xs car x, .ys car y} gt? if,\n {.ys car car, {.xs xs, .ys cdr ys} merge cdr} cons then\n } .then,\n {.xs car car, {.xs cdr xs, .ys ys} merge cdr} cons\n>;\n\n-- Sort a list in ascending order (merge sort): xs -> list\nsort = list? <\n /nil nil,\n /cons <\n {.cdr /nil if, .car singleton then} .then,\n |cons split {.first sort xs, .second sort ys} merge\n >\n> list?;\n\n\n",
|
|
11
11
|
"karatsuba-mult.k": "-- Karatsuba multiplication O(n^1.585)\n-- Self-contained and minimal implementation\n-- for arbitrary-precision integers\n\nyes = {} | yes;\nno = {} | no;\n\nbits? = ?< {} _, bits? 0, bits? 1 > = bits?;\n0b = {} | _;\n0 = 0b |0;\n1 = 0b |1;\n\nunat? = ?< {} _, unat? 1 > = unat?;\n0u = {} | _;\n\nempty = < /_ yes, no >;\nempty? = {empty /yes if, () then} .then;\n\ninv = {()x, 0b r} _inv;\n_inv = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .r then} .then,\n <\n {.x /0 x, .r |0 r},\n {.x /1 x, .r |1 r}\n > _inv\n>;\n\nconcat = {.x inv x, .y y} concat_;\nconcat_ = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .y then} .then,\n <\n {.x /0 x, .y |0 y},\n {.x /1 x, .y |1 y}\n > concat_\n>;\n\nremove_leading_zeros = < /0 remove_leading_zeros, () >;\nnat = remove_leading_zeros <empty? 0, ()>;\n\nint = nat | '+';\nabs = </'+', /'-'>;\n\nsign_plus = {} | '+';\nsign_minus = {} | '-';\nswap_sign = < /'+' | '-', /'-' | '+' >;\n\nc0 = {} | 0;\nc1 = {} | 1;\n\n_plus = {.x x, .y y, .c c, 0b r} _plus_;\n_plus_ = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,\n {.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,\n {.x empty? if, .c/0 and, {.r x, .y r} _inv then} .then,\n <\n {.x y, .y empty? x, .c c, .r r},\n {<.c/0 0, 1> x, .y y, .x empty? c0 c, .r r},\n {.x /0 x, .y /0 y, .c/0 c0 c, .r |0 r},\n {.x /0 x, .y /0 y, .c/1 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/0 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /0 y, .c/0 c0 c, .r |1 r},\n {.x /1 x, .y /0 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/0 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/1 c1 c, .r |1 r}\n > _plus_\n>;\n\n_minus = {.x x, .y y, .c c, 0b r} _minus_;\n_minus_ = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,\n <\n {.x/0 x, .y empty? y, .c/1 c1 c, .r |1 r},\n {.x/1 x, .y empty? y, .c/1 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/0 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/1 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/0 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/1 c1 c, .r |0 r},\n {.x/1 x, .y/0 y, .c/0 c0 c, .r |1 r},\n {.x/1 x, .y/0 y, .c/1 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/0 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/1 c1 c, .r |1 r}\n > _minus_\n>;\n\nplus_bits = {.x inv x, .y inv y, c0 c} _plus inv nat;\nminus_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;\n\n_times = ?{ <Y 0, Y 1, {} _>=Y y, ... }\n<\n {.y empty? if, .result then} .then,\n <\n {.result |0 result, ._x _x, .y /0 y},\n {{._x x, .result |0 y, c0 c} _plus result, ._x _x, .y /1 y}\n > _times\n>;\ntimes_bits = {0 result, .x inv _x, .y y} _times inv nat;\n\nunat_to_zeros = ?< {} _, X 1 >=X < /_ 0b, /1 unat_to_zeros |0 > ?< {} _, Z 0, Z 1 >=Z;\nunat_to_double_zeros = ?< {} _, X 1 >=X\n < /_ 0b, /1 unat_to_double_zeros |0 |0 > ?< {} _, Z 0, Z 1 >=Z;\n\n_peel2 = < </0,/1> </0,/1>, </0,/1>, () >;\n_half_unat = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .y empty? and, .m then} .then,\n {.x _peel2 x, .y _peel2 y, .m |1 m} _half_unat\n>;\nhalf_unat = {.x x, .y y, 0u m} _half_unat;\n\n_split_rev_u = ?{ <X 0, X 1, {} _>=X x_rev, <M 1, {} _>=M m, ... }\n<\n {.m /_ if, {.low nat low, .x_rev inv nat high} then} .then,\n {.x_rev empty? if, {.low nat low, 0 high} then} .then,\n <\n {.x_rev /0 x_rev, .m /1 m, .low |0 low},\n {.x_rev /1 x_rev, .m /1 m, .low |1 low}\n > _split_rev_u\n>;\nsplit_at = {.x inv x_rev, .m m, 0b low} _split_rev_u;\n\nat_least_256_u? = <\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n yes, no\n>;\n\nshift_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_zeros y} concat\n>;\n\nshift2_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_double_zeros y} concat\n>;\n\n_karatsuba = ?{ <X 0, X 1, {} _>=X x, <Y 0, Y 1, {} _>=Y y }\n<\n -- Base case: if m < 256 (< 512 bits), fall back to times_bits\n {{.x x, .y y} half_unat m, .x x, .y y}\n <\n {\n .m at_least_256_u? /no if,\n {.x x, .y y} times_bits then\n } .then,\n -- Recursive Karatsuba:\n {\n {.x x, .m m} split_at sx,\n {.y x, .m m} split_at sy,\n .m m\n }\n {\n {.sx.high x, .sy.high y} _karatsuba z2,\n {.sx.low x, .sy.low y} _karatsuba z0,\n {\n {.sx.high x, .sx.low y} plus_bits x,\n {.sy.high x, .sy.low y} plus_bits y\n } _karatsuba z_sum,\n .m m\n }\n {\n .z2 z2,\n .z0 z0,\n {{.z_sum x, .z2 y} minus_bits x, .z0 y} minus_bits z1,\n .m m\n }\n {\n {.z2 val, .m m} shift2_bits_u z2_s,\n {.z1 val, .m m} shift_bits_u z1_s,\n .z0 z0\n }\n {{.z2_s x, .z1_s y} plus_bits x, .z0 y} plus_bits\n >\n>;\n\nkaratsuba =\n{\n <\n {.x/'+' if, .y /'+' and} sign_plus,\n {.x/'-' if, .y /'-' and} sign_plus,\n sign_minus\n > sign,\n {.x abs x, .y abs y} _karatsuba result\n}\n<\n {.sign/'+' if, .result int then} .then,\n {.sign/'-' if, .result int swap_sign then} .then\n>;\n\n()\n",
|
|
12
12
|
"Examples/karatsuba-mult.k": "-- Karatsuba multiplication O(n^1.585)\n-- Self-contained and minimal implementation\n-- for arbitrary-precision integers\n\nyes = {} | yes;\nno = {} | no;\n\nbits? = ?< {} _, bits? 0, bits? 1 > = bits?;\n0b = {} | _;\n0 = 0b |0;\n1 = 0b |1;\n\nunat? = ?< {} _, unat? 1 > = unat?;\n0u = {} | _;\n\nempty = < /_ yes, no >;\nempty? = {empty /yes if, () then} .then;\n\ninv = {()x, 0b r} _inv;\n_inv = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .r then} .then,\n <\n {.x /0 x, .r |0 r},\n {.x /1 x, .r |1 r}\n > _inv\n>;\n\nconcat = {.x inv x, .y y} concat_;\nconcat_ = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .y then} .then,\n <\n {.x /0 x, .y |0 y},\n {.x /1 x, .y |1 y}\n > concat_\n>;\n\nremove_leading_zeros = < /0 remove_leading_zeros, () >;\nnat = remove_leading_zeros <empty? 0, ()>;\n\nint = nat | '+';\nabs = </'+', /'-'>;\n\nsign_plus = {} | '+';\nsign_minus = {} | '-';\nswap_sign = < /'+' | '-', /'-' | '+' >;\n\nc0 = {} | 0;\nc1 = {} | 1;\n\n_plus = {.x x, .y y, .c c, 0b r} _plus_;\n_plus_ = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,\n {.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,\n {.x empty? if, .c/0 and, {.r x, .y r} _inv then} .then,\n <\n {.x y, .y empty? x, .c c, .r r},\n {<.c/0 0, 1> x, .y y, .x empty? c0 c, .r r},\n {.x /0 x, .y /0 y, .c/0 c0 c, .r |0 r},\n {.x /0 x, .y /0 y, .c/1 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/0 c0 c, .r |1 r},\n {.x /0 x, .y /1 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /0 y, .c/0 c0 c, .r |1 r},\n {.x /1 x, .y /0 y, .c/1 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/0 c1 c, .r |0 r},\n {.x /1 x, .y /1 y, .c/1 c1 c, .r |1 r}\n > _plus_\n>;\n\n_minus = {.x x, .y y, .c c, 0b r} _minus_;\n_minus_ = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,\n <\n {.x/0 x, .y empty? y, .c/1 c1 c, .r |1 r},\n {.x/1 x, .y empty? y, .c/1 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/0 c0 c, .r |0 r},\n {.x/0 x, .y/0 y, .c/1 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/0 c1 c, .r |1 r},\n {.x/0 x, .y/1 y, .c/1 c1 c, .r |0 r},\n {.x/1 x, .y/0 y, .c/0 c0 c, .r |1 r},\n {.x/1 x, .y/0 y, .c/1 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/0 c0 c, .r |0 r},\n {.x/1 x, .y/1 y, .c/1 c1 c, .r |1 r}\n > _minus_\n>;\n\nplus_bits = {.x inv x, .y inv y, c0 c} _plus inv nat;\nminus_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;\n\n_times = ?{ <Y 0, Y 1, {} _>=Y y, ... }\n<\n {.y empty? if, .result then} .then,\n <\n {.result |0 result, ._x _x, .y /0 y},\n {{._x x, .result |0 y, c0 c} _plus result, ._x _x, .y /1 y}\n > _times\n>;\ntimes_bits = {0 result, .x inv _x, .y y} _times inv nat;\n\nunat_to_zeros = ?< {} _, X 1 >=X < /_ 0b, /1 unat_to_zeros |0 > ?< {} _, Z 0, Z 1 >=Z;\nunat_to_double_zeros = ?< {} _, X 1 >=X\n < /_ 0b, /1 unat_to_double_zeros |0 |0 > ?< {} _, Z 0, Z 1 >=Z;\n\n_peel2 = < </0,/1> </0,/1>, </0,/1>, () >;\n_half_unat = ?{ <X 0, X 1, {} _>=X x, ... }\n<\n {.x empty? if, .y empty? and, .m then} .then,\n {.x _peel2 x, .y _peel2 y, .m |1 m} _half_unat\n>;\nhalf_unat = {.x x, .y y, 0u m} _half_unat;\n\n_split_rev_u = ?{ <X 0, X 1, {} _>=X x_rev, <M 1, {} _>=M m, ... }\n<\n {.m /_ if, {.low nat low, .x_rev inv nat high} then} .then,\n {.x_rev empty? if, {.low nat low, 0 high} then} .then,\n <\n {.x_rev /0 x_rev, .m /1 m, .low |0 low},\n {.x_rev /1 x_rev, .m /1 m, .low |1 low}\n > _split_rev_u\n>;\nsplit_at = {.x inv x_rev, .m m, 0b low} _split_rev_u;\n\nat_least_256_u? = <\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1\n yes, no\n>;\n\nshift_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_zeros y} concat\n>;\n\nshift2_bits_u =\n<\n {.val empty? if, 0b then} .then,\n {.val x, .m unat_to_double_zeros y} concat\n>;\n\n_karatsuba = ?{ <X 0, X 1, {} _>=X x, <Y 0, Y 1, {} _>=Y y }\n<\n -- Base case: if m < 256 (< 512 bits), fall back to times_bits\n {{.x x, .y y} half_unat m, .x x, .y y}\n <\n {\n .m at_least_256_u? /no if,\n {.x x, .y y} times_bits then\n } .then,\n -- Recursive Karatsuba:\n {\n {.x x, .m m} split_at sx,\n {.y x, .m m} split_at sy,\n .m m\n }\n {\n {.sx.high x, .sy.high y} _karatsuba z2,\n {.sx.low x, .sy.low y} _karatsuba z0,\n {\n {.sx.high x, .sx.low y} plus_bits x,\n {.sy.high x, .sy.low y} plus_bits y\n } _karatsuba z_sum,\n .m m\n }\n {\n .z2 z2,\n .z0 z0,\n {{.z_sum x, .z2 y} minus_bits x, .z0 y} minus_bits z1,\n .m m\n }\n {\n {.z2 val, .m m} shift2_bits_u z2_s,\n {.z1 val, .m m} shift_bits_u z1_s,\n .z0 z0\n }\n {{.z2_s x, .z1_s y} plus_bits x, .z0 y} plus_bits\n >\n>;\n\nkaratsuba =\n{\n <\n {.x/'+' if, .y /'+' and} sign_plus,\n {.x/'-' if, .y /'-' and} sign_plus,\n sign_minus\n > sign,\n {.x abs x, .y abs y} _karatsuba result\n}\n<\n {.sign/'+' if, .result int then} .then,\n {.sign/'-' if, .result int swap_sign then} .then\n>;\n\n()\n",
|
|
13
|
-
"json.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n return { [this.tag]: this.value.toJSON() };\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT);\nvar BIT1 = Value.variant(\"1\", UNIT);\nfunction bitValue(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n let out = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct(list.value, \"string.nil\");\n return out;\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct(list.value, \"string.cons\");\n out += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n cursor = pair.cdr;\n }\n}\n\n// codecs/string-codec.mjs\nvar STRING_PATTERN_PROPERTY_LIST = patternFromFilter(`\n? <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6\n }=Bits7 ascii,\n {\n <\n { Bit 0, Bit 1, Bit 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n { Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7 }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit 0, Bit 1, Bit 2, Bit 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit 0, Bit 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n> = Str\n`);\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT2 = Value.product({});\nfunction bitValue2(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT2);\n}\nfunction bitsProduct2(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue2(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT2),\n exponent: bitsProduct2(11, exponent),\n fraction: bitsProduct2(52, fraction)\n });\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant2(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct2(value, \"float64\");\n const sign = requireVariant2(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct2(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct2(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/json-codec.mjs\nvar UNIT3 = Value.product({});\nvar BOOL_PATTERN = patternFromFilter(\"?< {} false, {} true >\");\nvar NULL_PATTERN = patternFromFilter(\"?< {} null >\");\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(value ? \"true\" : \"false\", UNIT3);\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return encodeNumberToValue(value);\n }\n if (typeof value === \"string\") {\n return textToStringValue(value);\n }\n if (Array.isArray(value)) {\n return Value.product(\n value.reduce((product, item, index) => {\n product[String(index)] = fromJsonValue(item);\n return product;\n }, {})\n );\n }\n if (value && typeof value === \"object\") {\n return Value.product(\n Object.keys(value).reduce((product, key) => {\n product[key] = fromJsonValue(value[key]);\n return product;\n }, {})\n );\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction composePattern(kind, entries) {\n const result = [[kind, []]];\n for (const [label, childPattern] of entries) {\n const offset = result.length;\n result[0][1].push([label, offset]);\n for (const [childKind, childEdges] of childPattern) {\n result.push([\n childKind,\n childEdges.map(([edgeLabel, target]) => [edgeLabel, target + offset])\n ]);\n }\n }\n return result;\n}\nfunction patternFromJsonValue(value) {\n if (value === null) return NULL_PATTERN;\n if (typeof value === \"boolean\") return BOOL_PATTERN;\n if (typeof value === \"number\") return FLOAT64_PATTERN;\n if (typeof value === \"string\") return STRING_PATTERN_PROPERTY_LIST;\n if (Array.isArray(value)) {\n return composePattern(\n \"closed-product\",\n value.map((item, index) => [String(index), patternFromJsonValue(item)])\n );\n }\n if (value && typeof value === \"object\") {\n return composePattern(\n \"closed-product\",\n Object.keys(value).sort((a, b) => a < b ? -1 : a > b ? 1 : 0).map((key) => [key, patternFromJsonValue(value[key])])\n );\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (isVariant(value)) {\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"json.null\");\n return null;\n }\n if (value.tag === \"true\") {\n requireProduct3(value.value, \"json.true\");\n return true;\n }\n if (value.tag === \"false\") {\n requireProduct3(value.value, \"json.false\");\n return false;\n }\n return stringValueToText(value);\n }\n const product = requireProduct3(value, \"json.value\");\n const keys = Object.keys(product);\n const isArray = keys.every((key, index) => key === String(index));\n if (isArray) {\n return keys.map((key) => {\n const child = product[key];\n if (isVariant(child)) return toJsonValue(child);\n if (isProduct(child)) {\n try {\n return decodeValueToNumber(child);\n } catch {\n }\n }\n return toJsonValue(child);\n });\n }\n const numberKeys = [\"exponent\", \"fraction\", \"sign\"];\n if (keys.length === numberKeys.length && numberKeys.every((key) => key in product)) {\n return decodeValueToNumber(value);\n }\n try {\n return stringValueToText(value);\n } catch {\n }\n return keys.reduce((result, key) => {\n result[key] = toJsonValue(product[key]);\n return result;\n }, {});\n}\n\n// codecs/json.mjs\nvar doc2 = `\nJSON codec (RFC 8259).\n\nTranslates JSON data to/from self-describing enveloped k values.\n`;\nfunction parse2(text) {\n const json = JSON.parse(text);\n const value = fromJsonValue(json);\n const pattern = patternFromJsonValue(json);\n return withPattern(value, pattern);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\nexport {\n doc2 as doc,\n parse2 as parse,\n print2 as print\n};\n",
|
|
14
|
-
"utf8.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n return { [this.tag]: this.value.toJSON() };\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT);\nvar BIT1 = Value.variant(\"1\", UNIT);\nfunction bitValue(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n let out = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct(list.value, \"string.nil\");\n return out;\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct(list.value, \"string.cons\");\n out += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n cursor = pair.cdr;\n }\n}\n\n// codecs/string-codec.mjs\nvar STRING_PATTERN_PROPERTY_LIST = patternFromFilter(`\n? <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6\n }=Bits7 ascii,\n {\n <\n { Bit 0, Bit 1, Bit 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n { Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7 }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit 0, Bit 1, Bit 2, Bit 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit 0, Bit 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n> = Str\n`);\n\n// codecs/utf8.mjs\nvar doc = `\nUTF-8 text codec.\n\nEncodes/decodes text strings to/from the canonical k string pattern.\n`;\nfunction parse(text) {\n const value = textToStringValue(text);\n return withPattern(value, STRING_PATTERN_PROPERTY_LIST);\n}\nfunction print(value) {\n return stringValueToText(value);\n}\nrunCodecCLI(import.meta.url, { parse, print, doc, addNewline: false });\nexport {\n doc,\n parse,\n print\n};\n",
|
|
15
|
-
"int.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n return { [this.tag]: this.value.toJSON() };\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/int.mjs\nvar doc = `\nDecimal integers and integer lists.\n\nSyntax:\n Decimal integer (e.g. 42, -15) or bracketed integer list (e.g. [0, 1, 2], []).\n`;\nvar INT_PATTERN = patternFromFilter('?< <bits 0, bits 1, {} _>=bits \"+\", bits \"-\">');\nvar INT_LIST_PATTERN = patternFromFilter(`\n ?<\n {} nil,\n {\n < <bits 0, bits 1, {} _>=bits \"+\", bits \"-\" > car,\n list cdr\n } cons\n > = list\n`);\nfunction buildBits(n) {\n const bitChars = n === 0n ? [\"0\"] : n.toString(2).split(\"\");\n let v = Value.variant(\"_\", Value.product({}));\n for (let i = bitChars.length - 1; i >= 0; i--) {\n v = Value.variant(bitChars[i], v);\n }\n return v;\n}\nfunction parseIntStr(str) {\n str = str.trim();\n let sign = \"+\";\n if (str[0] === \"-\") {\n sign = \"-\";\n str = str.slice(1).replace(/\\s+/g, \"\");\n } else if (str[0] === \"+\") {\n str = str.slice(1).replace(/\\s+/g, \"\");\n }\n if (!/^(0|[1-9][0-9]*)$/.test(str)) {\n throw new Error(`Invalid integer syntax: ${JSON.stringify(str)}`);\n }\n const n = BigInt(str);\n if (n === 0n) sign = \"+\";\n return Value.variant(sign, buildBits(n));\n}\nfunction parseIntListStr(str) {\n str = str.trim();\n if (!str.startsWith(\"[\") || !str.endsWith(\"]\")) {\n throw new Error(`Invalid integer list syntax: ${JSON.stringify(str)}`);\n }\n const inner = str.slice(1, -1).trim();\n if (inner === \"\") {\n return Value.variant(\"nil\", Value.product({}));\n }\n const parts = inner.split(\",\");\n const items = parts.map((part) => parseIntStr(part));\n let result = Value.variant(\"nil\", Value.product({}));\n for (let i = items.length - 1; i >= 0; i--) {\n result = Value.variant(\"cons\", Value.product({\n car: items[i],\n cdr: result\n }));\n }\n return result;\n}\nfunction parse(str) {\n str = str.trim();\n const raw = str.startsWith(\"[\") ? parseIntListStr(str) : parseIntStr(str);\n const pattern = isVariant(raw) && (raw.tag === \"nil\" || raw.tag === \"cons\") ? INT_LIST_PATTERN : INT_PATTERN;\n return withPattern(raw, pattern);\n}\nfunction printSingleInt(value) {\n if (!isVariant(value) || value.tag !== \"+\" && value.tag !== \"-\") {\n throw new Error(\"Not a valid k int value\");\n }\n const sign = value.tag;\n let bits = \"\";\n let node = value.value;\n while (isVariant(node) && node.tag !== \"_\") {\n bits += node.tag;\n node = node.value;\n }\n const n = bits === \"\" ? 0n : BigInt(\"0b\" + bits);\n const digits = n.toString(10);\n if (n === 0n) return \"0\";\n return (sign === \"-\" ? \"-\" : \"\") + digits;\n}\nfunction printIntList(value) {\n const items = [];\n let node = value;\n while (isVariant(node) && node.tag === \"cons\") {\n const product = isProduct(node.value) ? node.value.product : null;\n if (!product || !product.car || !product.cdr) {\n throw new Error(\"Invalid list cons node\");\n }\n items.push(printSingleInt(product.car));\n node = product.cdr;\n }\n if (!isVariant(node) || node.tag !== \"nil\") {\n throw new Error(\"Not a valid k int list: does not terminate in nil\");\n }\n return `[${items.join(\",\")}]`;\n}\nfunction print(value) {\n if (isVariant(value)) {\n if (value.tag === \"nil\" || value.tag === \"cons\") {\n return printIntList(value);\n }\n if (value.tag === \"+\" || value.tag === \"-\") {\n return printSingleInt(value);\n }\n }\n throw new Error(\"Not a valid k int or int list value\");\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\nexport {\n INT_LIST_PATTERN,\n INT_PATTERN,\n doc,\n parse,\n parseIntListStr,\n parseIntStr,\n print\n};\n",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
13
|
+
"json.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT);\nvar BIT1 = Value.variant(\"1\", UNIT);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/string-codec.mjs\nvar STRING_PATTERN_PROPERTY_LIST = patternFromFilter(`\n? <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6\n }=Bits7 ascii,\n {\n <\n { Bit 0, Bit 1, Bit 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n { Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7 }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit 0, Bit 1, Bit 2, Bit 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit 0, Bit 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n> = Str\n`);\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT2 = Value.product({});\nfunction bitValue2(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT2);\n}\nfunction bitsProduct2(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue2(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT2),\n exponent: bitsProduct2(11, exponent),\n fraction: bitsProduct2(52, fraction)\n });\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant2(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct2(value, \"float64\");\n const sign = requireVariant2(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct2(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct2(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/json-codec.mjs\nvar UNIT3 = Value.product({});\nvar BOOL_PATTERN = patternFromFilter(\"?< {} false, {} true >\");\nvar NULL_PATTERN = patternFromFilter(\"?< {} null >\");\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(value ? \"true\" : \"false\", UNIT3);\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return encodeNumberToValue(value);\n }\n if (typeof value === \"string\") {\n return textToStringValue(value);\n }\n if (Array.isArray(value)) {\n return Value.product(\n value.reduce((product, item, index) => {\n product[String(index)] = fromJsonValue(item);\n return product;\n }, {})\n );\n }\n if (value && typeof value === \"object\") {\n return Value.product(\n Object.keys(value).reduce((product, key) => {\n product[key] = fromJsonValue(value[key]);\n return product;\n }, {})\n );\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction composePattern(kind, entries) {\n const result = [[kind, []]];\n for (const [label, childPattern] of entries) {\n const offset = result.length;\n result[0][1].push([label, offset]);\n for (const [childKind, childEdges] of childPattern) {\n result.push([\n childKind,\n childEdges.map(([edgeLabel, target]) => [edgeLabel, target + offset])\n ]);\n }\n }\n return result;\n}\nfunction patternFromJsonValue(value) {\n if (value === null) return NULL_PATTERN;\n if (typeof value === \"boolean\") return BOOL_PATTERN;\n if (typeof value === \"number\") return FLOAT64_PATTERN;\n if (typeof value === \"string\") return STRING_PATTERN_PROPERTY_LIST;\n if (Array.isArray(value)) {\n return composePattern(\n \"closed-product\",\n value.map((item, index) => [String(index), patternFromJsonValue(item)])\n );\n }\n if (value && typeof value === \"object\") {\n return composePattern(\n \"closed-product\",\n Object.keys(value).sort((a, b) => a < b ? -1 : a > b ? 1 : 0).map((key) => [key, patternFromJsonValue(value[key])])\n );\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (isVariant(value)) {\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"json.null\");\n return null;\n }\n if (value.tag === \"true\") {\n requireProduct3(value.value, \"json.true\");\n return true;\n }\n if (value.tag === \"false\") {\n requireProduct3(value.value, \"json.false\");\n return false;\n }\n return stringValueToText(value);\n }\n const product = requireProduct3(value, \"json.value\");\n const keys = Object.keys(product);\n const isArray = keys.every((key, index) => key === String(index));\n if (isArray) {\n return keys.map((key) => {\n const child = product[key];\n if (isVariant(child)) return toJsonValue(child);\n if (isProduct(child)) {\n try {\n return decodeValueToNumber(child);\n } catch {\n }\n }\n return toJsonValue(child);\n });\n }\n const numberKeys = [\"exponent\", \"fraction\", \"sign\"];\n if (keys.length === numberKeys.length && numberKeys.every((key) => key in product)) {\n return decodeValueToNumber(value);\n }\n try {\n return stringValueToText(value);\n } catch {\n }\n return keys.reduce((result, key) => {\n result[key] = toJsonValue(product[key]);\n return result;\n }, {});\n}\n\n// codecs/json.mjs\nvar doc2 = `\nSchema-specializing JSON codec (RFC 8259).\n\nTranslates JSON data to/from self-describing enveloped k values where\neach JSON document derives a document-specific pattern:\n - objects map to products { field: value, ... }\n - arrays map to indexed products { \"0\": a0, \"1\": a1, ... }\n - numbers, strings, booleans, and null map to canonical k values\n(See djson.mjs for the universal singleton-pattern dynamic JSON codec.)\n`;\nfunction parse2(text) {\n const json = JSON.parse(text);\n const value = fromJsonValue(json);\n const pattern = patternFromJsonValue(json);\n return withPattern(value, pattern);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\nexport {\n doc2 as doc,\n parse2 as parse,\n print2 as print\n};\n",
|
|
14
|
+
"djson.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT = Value.product({});\nfunction bitValue(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT);\n}\nfunction bitsProduct(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT),\n exponent: bitsProduct(11, exponent),\n fraction: bitsProduct(52, fraction)\n });\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct(value, \"float64\");\n const sign = requireVariant(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT2 = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT2);\nvar BIT1 = Value.variant(\"1\", UNIT2);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue2(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct2(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant2(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct2(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue2(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct2(value, maxBit, where) {\n const product = requireProduct2(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant2(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct2(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct2(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct2(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct2(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct2(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct2(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT2);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct2(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct2(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct2(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant2(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct2(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct2(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct2(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct2(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct2(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct2(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct2(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct2(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT2);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant2(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct2(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct2(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct2(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct2(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct2(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT2);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant2(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct2(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct2(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct2(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct2(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct2(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant2(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct2(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct2(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct2(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct2(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct2(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct2(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT2);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant2(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct2(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct2(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/djson.mjs\nvar doc2 = `\nDynamic JSON (djson) codec with universal singleton pattern.\n\nTranslates any JSON value to/from self-describing enveloped k values\nconforming to the universal recursive JSON AST type:\n JSON = <\n float64 number,\n str string,\n <{} true, {} false> bool,\n {} null,\n <{} nil, {JSON car, LIST cdr} cons> = LIST list,\n <{} nil, {{str key, JSON val} car, OBJ cdr} cons> = OBJ object\n >\n`;\nvar DJSON_PATTERN = patternFromFilter(`\n? <\n {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5,\n Bit 6, Bit 7, Bit 8, Bit 9, Bit 10, Bit 11,\n Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17,\n Bit 18, Bit 19, Bit 20, Bit 21, Bit 22, Bit 23,\n Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35,\n Bit 36, Bit 37, Bit 38, Bit 39, Bit 40, Bit 41,\n Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47,\n Bit 48, Bit 49, Bit 50, Bit 51\n } fraction\n } number,\n\n <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit7 0,\n Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5, Bit7 6\n }=Bits7 ascii,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit7 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3,\n Bit7 4, Bit7 5, Bit7 6, Bit7 7\n }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit7 0, Bit7 1, Bit7 2, Bit7 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit7 0, Bit7 1, Bit7 2, Bit7 3, Bit7 4, Bit7 5,\n Bit7 6, Bit7 7, Bit7 8, Bit7 9, Bit7 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit7 0, Bit7 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n > = Str string,\n\n < {} true, {} false > bool,\n {} null,\n\n < {} nil, { JSON car, LIST cdr } cons > = LIST list,\n\n < {} nil, { { Str key, JSON val } car, OBJ cdr } cons > = OBJ object\n> = JSON\n`);\nvar UNIT3 = Value.product({});\nfunction requireProduct3(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction listToK(items) {\n let result = Value.variant(\"nil\", UNIT3);\n for (let i = items.length - 1; i >= 0; i--) {\n result = Value.variant(\"cons\", Value.product({\n car: items[i],\n cdr: result\n }));\n }\n return result;\n}\nfunction listFromK(value, where) {\n const result = [];\n let current = value;\n while (true) {\n if (!isVariant(current)) {\n throw new Error(`${where}: expected list variant (nil or cons)`);\n }\n if (current.tag === \"nil\") {\n requireProduct3(current.value, `${where}.nil`);\n return result;\n }\n if (current.tag !== \"cons\") {\n throw new Error(\n `${where}: expected nil or cons, got ${current.tag}`\n );\n }\n const pair = requireProduct3(current.value, `${where}.cons`);\n result.push(pair.car);\n current = pair.cdr;\n }\n}\nfunction fromJsonValue(value) {\n if (value === null) {\n return Value.variant(\"null\", UNIT3);\n }\n if (typeof value === \"boolean\") {\n return Value.variant(\n \"bool\",\n Value.variant(value ? \"true\" : \"false\", UNIT3)\n );\n }\n if (typeof value === \"number\") {\n if (!Number.isFinite(value)) {\n throw new Error(\"JSON numbers must be finite\");\n }\n return Value.variant(\"number\", encodeNumberToValue(value));\n }\n if (typeof value === \"string\") {\n return Value.variant(\"string\", textToStringValue(value));\n }\n if (Array.isArray(value)) {\n return Value.variant(\"list\", listToK(value.map(fromJsonValue)));\n }\n if (value && typeof value === \"object\") {\n const keys = Object.keys(value).sort();\n const entries = keys.map(\n (k) => Value.product({\n key: textToStringValue(k),\n val: fromJsonValue(value[k])\n })\n );\n return Value.variant(\"object\", listToK(entries));\n }\n throw new Error(`Unsupported JSON value: ${value}`);\n}\nfunction toJsonValue(value) {\n if (!isVariant(value)) {\n throw new Error(\"djson.value: expected top-level Variant\");\n }\n if (value.tag === \"null\") {\n requireProduct3(value.value, \"djson.null\");\n return null;\n }\n if (value.tag === \"bool\") {\n if (!isVariant(value.value)) {\n throw new Error(\"djson.bool: expected variant\");\n }\n if (value.value.tag === \"true\") {\n requireProduct3(value.value.value, \"djson.bool.true\");\n return true;\n }\n if (value.value.tag === \"false\") {\n requireProduct3(value.value.value, \"djson.bool.false\");\n return false;\n }\n throw new Error(`djson.bool: invalid tag ${value.value.tag}`);\n }\n if (value.tag === \"number\") {\n return decodeValueToNumber(value.value);\n }\n if (value.tag === \"string\") {\n return stringValueToText(value.value);\n }\n if (value.tag === \"list\") {\n const items = listFromK(value.value, \"djson.list\");\n return items.map(toJsonValue);\n }\n if (value.tag === \"object\") {\n const pairs = listFromK(value.value, \"djson.object\");\n const result = {};\n for (const pairValue of pairs) {\n const pair = requireProduct3(pairValue, \"djson.object.pair\");\n const key = stringValueToText(pair.key);\n result[key] = toJsonValue(pair.val);\n }\n return result;\n }\n throw new Error(`Unsupported djson tag: ${value.tag}`);\n}\nfunction parse2(text) {\n const json = JSON.parse(text);\n const value = fromJsonValue(json);\n return withPattern(value, DJSON_PATTERN);\n}\nfunction print2(value) {\n return JSON.stringify(toJsonValue(value));\n}\nrunCodecCLI(import.meta.url, { parse: parse2, print: print2, doc: doc2 });\nexport {\n DJSON_PATTERN,\n doc2 as doc,\n fromJsonValue,\n parse2 as parse,\n print2 as print,\n toJsonValue\n};\n",
|
|
15
|
+
"utf8.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/runtime/unicode-string.mjs\nvar UNIT = Value.product({});\nvar BIT0 = Value.variant(\"0\", UNIT);\nvar BIT1 = Value.variant(\"1\", UNIT);\nvar BIT_KEYS = [\n \"0\",\n \"1\",\n \"2\",\n \"3\",\n \"4\",\n \"5\",\n \"6\",\n \"7\",\n \"8\",\n \"9\",\n \"10\"\n];\nvar lazyStringDecoder = null;\nfunction bitValue(bit) {\n return bit === 0 ? BIT0 : BIT1;\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction bitsProduct(width, n) {\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n product[key] = bitValue(n >> i & 1);\n }\n return Value.product(product);\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0;\n for (let i = maxBit; i >= 0; i--) {\n const key = i < BIT_KEYS.length ? BIT_KEYS[i] : String(i);\n const entry = requireVariant(product[key], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1 | (entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction byteProduct(n) {\n return bitsProduct(8, n);\n}\nfunction parseByteProduct(value, where) {\n return parseBitsProduct(value, 7, where);\n}\nfunction encodeBmpCommonHi(hi) {\n if (hi >= 8 && hi <= 15) return Value.variant(\"h08_0F\", bitsProduct(3, hi - 8));\n if (hi >= 16 && hi <= 127) return Value.variant(\"h10_7F\", bitsProduct(7, hi - 16));\n if (hi >= 128 && hi <= 207) return Value.variant(\"h80_CF\", bitsProduct(7, hi - 128));\n if (hi >= 208 && hi <= 215) return Value.variant(\"hD0_D7\", bitsProduct(3, hi - 208));\n if (hi === 249) return Value.variant(\"hF9\", UNIT);\n if (hi >= 250 && hi <= 251) return Value.variant(\"hFA_FB\", bitsProduct(1, hi - 250));\n if (hi >= 252 && hi <= 253) return Value.variant(\"hFC_FD\", bitsProduct(1, hi - 252));\n if (hi >= 254 && hi <= 255) return Value.variant(\"hFE_FF\", bitsProduct(1, hi - 254));\n throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpCommonHi(value) {\n const v = requireVariant(value, \"bmp_common.hi\");\n if (v.tag === \"h08_0F\") return 8 + parseBitsProduct(v.value, 2, \"h08_0F\");\n if (v.tag === \"h10_7F\") return 16 + parseBitsProduct(v.value, 6, \"h10_7F\");\n if (v.tag === \"h80_CF\") return 128 + parseBitsProduct(v.value, 6, \"h80_CF\");\n if (v.tag === \"hD0_D7\") return 208 + parseBitsProduct(v.value, 2, \"hD0_D7\");\n if (v.tag === \"hF9\") return 249;\n if (v.tag === \"hFA_FB\") return 250 + parseBitsProduct(v.value, 0, \"hFA_FB\");\n if (v.tag === \"hFC_FD\") return 252 + parseBitsProduct(v.value, 0, \"hFC_FD\");\n if (v.tag === \"hFE_FF\") return 254 + parseBitsProduct(v.value, 0, \"hFE_FF\");\n throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);\n}\nfunction encodeBmpPrivateHi(hi) {\n if (hi >= 224 && hi <= 239) return Value.variant(\"hE0_EF\", bitsProduct(4, hi - 224));\n if (hi >= 240 && hi <= 247) return Value.variant(\"hF0_F7\", bitsProduct(3, hi - 240));\n if (hi === 248) return Value.variant(\"hF8\", UNIT);\n throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);\n}\nfunction decodeBmpPrivateHi(value) {\n const v = requireVariant(value, \"bmp_private_use.hi\");\n if (v.tag === \"hE0_EF\") return 224 + parseBitsProduct(v.value, 3, \"hE0_EF\");\n if (v.tag === \"hF0_F7\") return 240 + parseBitsProduct(v.value, 2, \"hF0_F7\");\n if (v.tag === \"hF8\") return 248;\n throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);\n}\nfunction encodePlane2To16(plane) {\n if (plane >= 2 && plane <= 3) return Value.variant(\"p02_03\", bitsProduct(1, plane - 2));\n if (plane >= 4 && plane <= 7) return Value.variant(\"p04_07\", bitsProduct(2, plane - 4));\n if (plane >= 8 && plane <= 15) return Value.variant(\"p08_0F\", bitsProduct(3, plane - 8));\n if (plane === 16) return Value.variant(\"p10\", UNIT);\n throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);\n}\nfunction decodePlane2To16(value) {\n const v = requireVariant(value, \"supplementary_planes2_16.plane\");\n if (v.tag === \"p02_03\") return 2 + parseBitsProduct(v.value, 0, \"p02_03\");\n if (v.tag === \"p04_07\") return 4 + parseBitsProduct(v.value, 1, \"p04_07\");\n if (v.tag === \"p08_0F\") return 8 + parseBitsProduct(v.value, 2, \"p08_0F\");\n if (v.tag === \"p10\") return 16;\n throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);\n}\nfunction codePointToUnicodeValue(cp) {\n if (!Number.isInteger(cp) || cp < 0 || cp > 1114111) {\n throw new Error(`Code point out of Unicode range: ${cp}`);\n }\n if (cp >= 55296 && cp <= 57343) {\n throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);\n }\n if (cp <= 127) {\n return Value.variant(\"ascii\", bitsProduct(7, cp));\n }\n if (cp <= 2047) {\n return Value.variant(\"plane0\", bitsProduct(11, cp));\n }\n if (cp <= 65535) {\n const hi = cp >> 8 & 255;\n const lo2 = cp & 255;\n if (hi >= 224 && hi <= 248) {\n return Value.variant(\"bmp_private_use\", Value.product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo2) }));\n }\n return Value.variant(\"bmp_common\", Value.product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo2) }));\n }\n const mid = cp >> 8 & 255;\n const lo = cp & 255;\n if (cp <= 131071) {\n return Value.variant(\"supplementary_plane1\", Value.product({\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n }\n const plane = cp >> 16;\n return Value.variant(\"supplementary_planes2_16\", Value.product({\n plane: encodePlane2To16(plane),\n mid: byteProduct(mid),\n lo: byteProduct(lo)\n }));\n}\nfunction unicodeValueToCodePoint(value) {\n const v = requireVariant(value, \"unicode\");\n if (v.tag === \"ascii\") return parseBitsProduct(v.value, 6, \"ascii\");\n if (v.tag === \"plane0\") return parseBitsProduct(v.value, 10, \"plane0\");\n if (v.tag === \"bmp_common\") {\n const p = requireProduct(v.value, \"bmp_common\");\n return decodeBmpCommonHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_common.lo\");\n }\n if (v.tag === \"bmp_private_use\") {\n const p = requireProduct(v.value, \"bmp_private_use\");\n return decodeBmpPrivateHi(p.hi) << 8 | parseByteProduct(p.lo, \"bmp_private_use.lo\");\n }\n if (v.tag === \"supplementary_plane1\") {\n const p = requireProduct(v.value, \"supplementary_plane1\");\n return 1 << 16 | parseByteProduct(p.mid, \"supplementary_plane1.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_plane1.lo\");\n }\n if (v.tag === \"supplementary_planes2_16\") {\n const p = requireProduct(v.value, \"supplementary_planes2_16\");\n return decodePlane2To16(p.plane) << 16 | parseByteProduct(p.mid, \"supplementary_planes2_16.mid\") << 8 | parseByteProduct(p.lo, \"supplementary_planes2_16.lo\");\n }\n throw new Error(`Unsupported unicode tag: ${v.tag}`);\n}\nfunction textToStringValue(text) {\n let out = Value.variant(\"nil\", UNIT);\n const chars = Array.from(text);\n for (let i = chars.length - 1; i >= 0; i--) {\n const cp = chars[i].codePointAt(0);\n out = Value.variant(\"cons\", Value.product({\n car: codePointToUnicodeValue(cp),\n cdr: out\n }));\n }\n return out;\n}\nfunction stringValueToText(value) {\n if (lazyStringDecoder) {\n const fast = lazyStringDecoder(value);\n if (fast !== null) return fast;\n }\n const chunks = [];\n let chunk = \"\";\n let cursor = value;\n while (true) {\n const list = requireVariant(cursor, \"string\");\n if (list.tag === \"nil\") {\n requireProduct(list.value, \"string.nil\");\n if (chunk.length > 0) chunks.push(chunk);\n return chunks.join(\"\");\n }\n if (list.tag !== \"cons\") {\n throw new Error(`Unsupported string list tag: ${list.tag}`);\n }\n const pair = requireProduct(list.value, \"string.cons\");\n chunk += String.fromCodePoint(unicodeValueToCodePoint(pair.car));\n if (chunk.length >= 8192) {\n chunks.push(chunk);\n chunk = \"\";\n }\n cursor = pair.cdr;\n }\n}\n\n// codecs/string-codec.mjs\nvar STRING_PATTERN_PROPERTY_LIST = patternFromFilter(`\n? <\n {} nil,\n {\n <\n {\n < {} 0, {} 1 >=Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6\n }=Bits7 ascii,\n {\n <\n { Bit 0, Bit 1, Bit 2 }=Bits3 h08_0F,\n Bits7 h10_7F,\n Bits7 h80_CF,\n Bits3 hD0_D7,\n {} hF9,\n { Bit 0 }=Bits1 hFA_FB,\n Bits1 hFC_FD,\n Bits1 hFE_FF\n > hi,\n { Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7 }=Bits8 lo\n } bmp_common,\n {\n <\n { Bit 0, Bit 1, Bit 2, Bit 3 } hE0_EF,\n Bits3 hF0_F7,\n {} hF8\n > hi,\n Bits8 lo\n } bmp_private_use,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } plane0,\n { Bits8 mid, Bits8 lo } supplementary_plane1,\n {\n <\n Bits1 p02_03,\n { Bit 0, Bit 1 } p04_07,\n Bits3 p08_0F,\n {} p10\n > plane,\n Bits8 mid,\n Bits8 lo\n } supplementary_planes2_16\n > car,\n Str cdr\n } cons\n> = Str\n`);\n\n// codecs/utf8.mjs\nvar doc = `\nUTF-8 text codec.\n\nEncodes/decodes text strings to/from the canonical k string pattern.\n`;\nfunction parse(text) {\n const value = textToStringValue(text);\n return withPattern(value, STRING_PATTERN_PROPERTY_LIST);\n}\nfunction print(value) {\n return stringValueToText(value);\n}\nrunCodecCLI(import.meta.url, { parse, print, doc, addNewline: false });\nexport {\n doc,\n parse,\n print\n};\n",
|
|
16
|
+
"int.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/int.mjs\nvar doc = `\nDecimal integers and integer lists.\n\nSyntax:\n Decimal integer (e.g. 42, -15) or bracketed integer list\n (e.g. [0, 1, 2], []).\n`;\nvar INT_PATTERN = patternFromFilter('?< <bits 0, bits 1, {} _>=bits \"+\", bits \"-\">');\nvar INT_LIST_PATTERN = patternFromFilter(`\n ?<\n {} nil,\n {\n < <bits 0, bits 1, {} _>=bits \"+\", bits \"-\" > car,\n list cdr\n } cons\n > = list\n`);\nfunction buildBits(n) {\n const bitChars = n === 0n ? [\"0\"] : n.toString(2).split(\"\");\n let v = Value.variant(\"_\", Value.product({}));\n for (let i = bitChars.length - 1; i >= 0; i--) {\n v = Value.variant(bitChars[i], v);\n }\n return v;\n}\nfunction parseIntStr(str) {\n str = str.trim();\n let sign = \"+\";\n if (str[0] === \"-\") {\n sign = \"-\";\n str = str.slice(1).replace(/\\s+/g, \"\");\n } else if (str[0] === \"+\") {\n str = str.slice(1).replace(/\\s+/g, \"\");\n }\n if (!/^(0|[1-9][0-9]*)$/.test(str)) {\n throw new Error(`Invalid integer syntax: ${JSON.stringify(str)}`);\n }\n const n = BigInt(str);\n if (n === 0n) sign = \"+\";\n return Value.variant(sign, buildBits(n));\n}\nfunction parseIntListStr(str) {\n str = str.trim();\n if (!str.startsWith(\"[\") || !str.endsWith(\"]\")) {\n throw new Error(`Invalid integer list syntax: ${JSON.stringify(str)}`);\n }\n const inner = str.slice(1, -1).trim();\n if (inner === \"\") {\n return Value.variant(\"nil\", Value.product({}));\n }\n const parts = inner.split(\",\");\n const items = parts.map((part) => parseIntStr(part));\n let result = Value.variant(\"nil\", Value.product({}));\n for (let i = items.length - 1; i >= 0; i--) {\n result = Value.variant(\"cons\", Value.product({\n car: items[i],\n cdr: result\n }));\n }\n return result;\n}\nfunction parse(str) {\n str = str.trim();\n const raw = str.startsWith(\"[\") ? parseIntListStr(str) : parseIntStr(str);\n const pattern = isVariant(raw) && (raw.tag === \"nil\" || raw.tag === \"cons\") ? INT_LIST_PATTERN : INT_PATTERN;\n return withPattern(raw, pattern);\n}\nfunction printSingleInt(value) {\n if (!isVariant(value) || value.tag !== \"+\" && value.tag !== \"-\") {\n throw new Error(\"Not a valid k int value\");\n }\n const sign = value.tag;\n let bits = \"\";\n let node = value.value;\n while (isVariant(node) && node.tag !== \"_\") {\n bits += node.tag;\n node = node.value;\n }\n const n = bits === \"\" ? 0n : BigInt(\"0b\" + bits);\n const digits = n.toString(10);\n if (n === 0n) return \"0\";\n return (sign === \"-\" ? \"-\" : \"\") + digits;\n}\nfunction printIntList(value) {\n const items = [];\n let node = value;\n while (isVariant(node) && node.tag === \"cons\") {\n const product = isProduct(node.value) ? node.value.product : null;\n if (!product || !product.car || !product.cdr) {\n throw new Error(\"Invalid list cons node\");\n }\n items.push(printSingleInt(product.car));\n node = product.cdr;\n }\n if (!isVariant(node) || node.tag !== \"nil\") {\n throw new Error(\"Not a valid k int list: does not terminate in nil\");\n }\n return `[${items.join(\",\")}]`;\n}\nfunction print(value) {\n if (isVariant(value)) {\n if (value.tag === \"nil\" || value.tag === \"cons\") {\n return printIntList(value);\n }\n if (value.tag === \"+\" || value.tag === \"-\") {\n return printSingleInt(value);\n }\n }\n throw new Error(\"Not a valid k int or int list value\");\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\nexport {\n INT_LIST_PATTERN,\n INT_PATTERN,\n doc,\n parse,\n parseIntListStr,\n parseIntStr,\n print\n};\n",
|
|
17
|
+
"unit.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/unit.mjs\nvar doc = `\nUnit codec for {}.\n\nTranslates the empty product {} to/from binary pattern+value stream.\n`;\nvar UNIT_PATTERN = patternFromFilter(\"?{}\");\nfunction parse() {\n return withPattern(Value.product({}), UNIT_PATTERN);\n}\nfunction print(value) {\n if (!isProduct(value) || Object.keys(value.product).length !== 0) {\n throw new Error(\"Input is not a unit value\");\n }\n return \"{}\";\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\nexport {\n UNIT_PATTERN,\n doc,\n parse,\n print\n};\n",
|
|
18
|
+
"ieee.mjs": "#!/usr/bin/env node\n\n// Value.mjs\nvar nameRE = /^[a-zA-Z0-9_+-][a-zA-Z0-9_?!+-]*$/;\nfunction pLabel(label) {\n return nameRE.test(label) ? `${label}` : `${JSON.stringify(label)}`;\n}\nfunction isObject(value) {\n return value !== null && typeof value === \"object\";\n}\nfunction isProduct(value) {\n return isObject(value) && value.type === \"{}\" && isObject(value.product) && !Array.isArray(value.product);\n}\nfunction isVariant(value) {\n return isObject(value) && value.type === \"<>\" && typeof value.tag === \"string\" && Object.hasOwn(value, \"value\");\n}\nfunction toVector(m) {\n let vector = [];\n for (let i = 0; i < Object.keys(m).length; i++) {\n if (m[i] === void 0) {\n vector = [];\n break;\n }\n vector.push(m[i]);\n }\n return vector;\n}\nfunction withPattern(value, pattern) {\n if (!pattern) return value;\n if (value.pattern === pattern) return value;\n if (isProduct(value)) return Value.product(value.product, pattern);\n if (isVariant(value)) return Value.variant(value.tag, value.value, pattern);\n return value;\n}\nvar Value = class _Value {\n constructor(fields) {\n Object.assign(this, fields);\n }\n static product(product, pattern = null) {\n return new _Value({ type: \"{}\", pattern, product });\n }\n static variant(tag, value, pattern = null) {\n return new _Value({ type: \"<>\", pattern, tag, value });\n }\n toString() {\n if (isVariant(this)) {\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n tags.push(pLabel(curr.tag));\n curr = curr.value;\n }\n const base = curr ? curr.toString() : \"\";\n return tags.length === 0 ? base : `${base}|${tags.reverse().join(\"|\")}`;\n }\n if (isProduct(this)) {\n return `{${Object.entries(this.product).map(([k, v]) => `${JSON.stringify(k)}:${v.toString()}`).join(\",\")}}`;\n }\n return String(this.type);\n }\n toJSON() {\n if (isVariant(this)) {\n if (isProduct(this.value) && Object.keys(this.value.product).length === 0) {\n return this.tag;\n }\n const tags = [];\n let curr = this;\n while (isVariant(curr)) {\n if (isProduct(curr.value) && Object.keys(curr.value.product).length === 0) {\n let res2 = curr.tag;\n for (let i = tags.length - 1; i >= 0; i--) {\n res2 = { [tags[i]]: res2 };\n }\n return res2;\n }\n tags.push(curr.tag);\n curr = curr.value;\n }\n let res = curr ? typeof curr.toJSON === \"function\" ? curr.toJSON() : curr : null;\n for (let i = tags.length - 1; i >= 0; i--) {\n res = { [tags[i]]: res };\n }\n return res;\n }\n if (!isProduct(this)) return { type: this.type };\n let vector = toVector(this.product);\n if (vector.length > 0) return vector;\n return this.product;\n }\n};\n\n// codec-sdk-shim:./runtime/codec-sdk.mjs\nfunction runCodecCLI() {\n}\nfunction patternFromFilter(script, options) {\n if (typeof globalThis !== \"undefined\" && typeof globalThis.patternFromFilter === \"function\") {\n return globalThis.patternFromFilter(script, options);\n }\n throw new Error(\"patternFromFilter is not available in browser environment\");\n}\n\n// codecs/ieee.mjs\nvar doc = `\nIEEE 754 64-bit float codec.\n\nParses decimal floats, Infinity, -Infinity, NaN to 64-bit IEEE products.\n`;\nvar FLOAT64_PATTERN = patternFromFilter(`\n? {\n < {} \"+\", {} \"-\" > sign,\n {\n < {} 0, {} 1 >=Bit 0,\n Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9, Bit 10\n } exponent,\n {\n Bit 0, Bit 1, Bit 2, Bit 3, Bit 4, Bit 5, Bit 6, Bit 7, Bit 8, Bit 9,\n Bit 10, Bit 11, Bit 12, Bit 13, Bit 14, Bit 15, Bit 16, Bit 17, Bit 18, Bit 19,\n Bit 20, Bit 21, Bit 22, Bit 23, Bit 24, Bit 25, Bit 26, Bit 27, Bit 28, Bit 29,\n Bit 30, Bit 31, Bit 32, Bit 33, Bit 34, Bit 35, Bit 36, Bit 37, Bit 38, Bit 39,\n Bit 40, Bit 41, Bit 42, Bit 43, Bit 44, Bit 45, Bit 46, Bit 47, Bit 48, Bit 49,\n Bit 50, Bit 51\n } fraction\n}\n`);\nvar UNIT = Value.product({});\nfunction bitValue(bit) {\n return Value.variant(bit === 0 ? \"0\" : \"1\", UNIT);\n}\nfunction bitsProduct(width, value) {\n const big = BigInt(value);\n const product = {};\n for (let i = width - 1; i >= 0; i--) {\n product[String(i)] = bitValue(Number(big >> BigInt(i) & 1n));\n }\n return Value.product(product);\n}\nvar floatView = new DataView(new ArrayBuffer(8));\nfunction encodeNumberToValue(number) {\n floatView.setFloat64(0, number, false);\n const bits = floatView.getBigUint64(0, false);\n const sign = Number(bits >> 63n & 1n);\n const exponent = Number(bits >> 52n & 0x7ffn);\n const fraction = bits & (1n << 52n) - 1n;\n return Value.product({\n sign: Value.variant(sign === 0 ? \"+\" : \"-\", UNIT),\n exponent: bitsProduct(11, exponent),\n fraction: bitsProduct(52, fraction)\n });\n}\nfunction requireProduct(value, where) {\n if (!isProduct(value)) {\n throw new Error(`${where}: expected Product`);\n }\n return value.product;\n}\nfunction requireVariant(value, where) {\n if (!isVariant(value)) {\n throw new Error(`${where}: expected Variant`);\n }\n return value;\n}\nfunction parseBitsProduct(value, maxBit, where) {\n const product = requireProduct(value, where);\n let n = 0n;\n for (let i = maxBit; i >= 0; i--) {\n const entry = requireVariant(product[String(i)], `${where}.bit${i}`);\n if (entry.tag !== \"0\" && entry.tag !== \"1\") {\n throw new Error(`${where}.bit${i}: expected tag 0 or 1`);\n }\n n = n << 1n | BigInt(entry.tag === \"1\" ? 1 : 0);\n }\n return n;\n}\nfunction decodeValueToNumber(value) {\n const product = requireProduct(value, \"float64\");\n const sign = requireVariant(product.sign, \"float64.sign\");\n if (sign.tag !== \"+\" && sign.tag !== \"-\") {\n throw new Error(`float64.sign: expected + or -, got ${sign.tag}`);\n }\n const exponent = parseBitsProduct(product.exponent, 10, \"float64.exponent\");\n const fraction = parseBitsProduct(product.fraction, 51, \"float64.fraction\");\n const bits = BigInt(sign.tag === \"-\" ? 1 : 0) << 63n | exponent << 52n | fraction;\n floatView.setBigUint64(0, bits, false);\n return floatView.getFloat64(0, false);\n}\nfunction parseFloatText(text) {\n const trimmed = text.trim();\n if (trimmed === \"\") {\n throw new Error(\"Expected a floating-point literal\");\n }\n if (/^[+-]?nan$/i.test(trimmed)) return NaN;\n if (/^[+]?inf(inity)?$/i.test(trimmed)) return Infinity;\n if (/^[-](inf|infinity)$/i.test(trimmed)) return -Infinity;\n const n = Number(trimmed);\n if (Number.isNaN(n)) {\n throw new Error(`Invalid float literal ${JSON.stringify(trimmed)}`);\n }\n return n;\n}\nfunction printFloatText(n) {\n if (Number.isNaN(n)) return \"NaN\";\n if (n === Infinity) return \"Infinity\";\n if (n === -Infinity) return \"-Infinity\";\n if (Object.is(n, -0)) return \"-0\";\n return String(n);\n}\nfunction parse(text) {\n const n = parseFloatText(text);\n const value = encodeNumberToValue(n);\n return withPattern(value, FLOAT64_PATTERN);\n}\nfunction print(value) {\n return printFloatText(decodeValueToNumber(value));\n}\nrunCodecCLI(import.meta.url, { parse, print, doc });\nexport {\n FLOAT64_PATTERN,\n decodeValueToNumber,\n doc,\n encodeNumberToValue,\n parse,\n print\n};\n"
|
|
18
19
|
};
|
package/codecs/README.md
CHANGED
|
@@ -105,6 +105,7 @@ as `k-parse.mjs`, keep that name. Every installed codec binary supports
|
|
|
105
105
|
| `k-print` | `k-print.mjs` | Read a binary pattern+value stream and write the decoded JSON-like value. Use `--debug` to include the decoded pattern. |
|
|
106
106
|
| `k-show` | `show.mjs` | Pass the wire stream through unchanged on stdout while printing the decoded value and filter to stderr. |
|
|
107
107
|
| `k-json` | `json.mjs` | Convert JSON to/from the binary stream with `--parse` or `--print`. |
|
|
108
|
+
| `k-djson` | `djson.mjs` | Convert JSON to/from a universal dynamic JSON singleton pattern with `--parse` or `--print`. |
|
|
108
109
|
| `k-int` | `int.mjs` | Convert decimal integers to/from the binary stream with `--parse` or `--print`. |
|
|
109
110
|
| `k-ieee` | `ieee.mjs` | Convert float literals to/from the binary stream with `--parse` or `--print`. |
|
|
110
111
|
| `k-unit` | `unit.mjs` | Write or validate the unit value `{}` with `--parse` or `--print`. |
|