@fraczak/k 8.6.1 → 8.7.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 +22 -0
- package/DOCS/CODECS.md +20 -14
- package/DOCS/FILE_FORMATS.md +79 -12
- package/DOCS/GETTING_STARTED.md +17 -8
- package/DOCS/PATTERNS.md +17 -0
- package/DOCS/REPL.md +13 -7
- package/Examples/arithmetics.k +81 -468
- package/Examples/ieee-arithmetics.k +37 -33
- package/Examples/poly.k +107 -104
- package/README.md +19 -11
- package/backend-api.mjs +3 -1
- package/backends/llvm/README.md +1 -1
- package/backends/llvm/bin/k-llvm-build.mjs +5 -5
- package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
- package/backends/llvm/bin/k-llvm.mjs +6 -6
- package/backends/llvm/src/cli.mjs +17 -12
- package/backends/llvm/tests/integration.sh +4 -1
- package/backends/wasm/README.md +3 -3
- package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
- package/backends/wasm/bin/k-wasm.mjs +4 -4
- package/backends/wasm/src/cli.mjs +12 -10
- package/backends/wasm/src/wasm.mjs +6 -2
- package/backends/wasm/tests/integration.sh +8 -3
- package/browser/shims/fs.mjs +19 -3
- package/canonical-pattern.mjs +217 -57
- package/core.k +2 -1
- package/harness/suites/poly.mjs +14 -30
- package/k.mjs +17 -13
- package/kvm.mjs +13 -9
- package/lib/arithmetics.k +287 -0
- package/lib/bit.k +68 -0
- package/lib/bits.k +221 -0
- package/lib/btrie.k +187 -0
- package/lib/djson.k +326 -0
- package/lib/ieee.k +709 -0
- package/lib/list.k +49 -0
- package/lib/string.k +588 -0
- package/named-patterns.mjs +2 -2
- package/object.mjs +51 -9
- package/objects/README.md +10 -3
- package/objects/compile.mjs +10 -8
- package/package.json +1 -1
- package/repl.mjs +28 -6
- package/scripts/benchmark-idempotency.mjs +22 -15
- package/scripts/benchmark.mjs +29 -10
- package/scripts/build-repl-html.mjs +41 -13
- package/scripts/perf-ieee.mjs +9 -1
- package/scripts/perf-int.mjs +10 -17
- package/scripts/perf-multiplication.mjs +9 -15
- package/scripts/perf-poly.mjs +16 -32
- package/scripts/run-tests.mjs +2 -2
- package/scripts/test-package.mjs +8 -2
- package/tests/helpers/cli.mjs +11 -3
- package/tests/integration.sh +9 -4
- package/tests/test-arithmetics.mjs +4 -2
- package/tests/test-djson-methods.mjs +16 -12
- package/tests/test-ieee-arithmetic.mjs +11 -3
- package/tests/test-ieee-text.mjs +15 -4
- package/tests/test-k-object.mjs +11 -3
- package/tests/test-karatsuba.mjs +1 -1
- package/tests/test-kvm-polymorphic.mjs +54 -34
- package/tests/test-lib.mjs +115 -0
- package/tests/test-named-patterns.mjs +12 -3
- package/tests/test-parse-json.mjs +33 -13
- package/tests/test-poly.mjs +10 -4
- package/tests/test-repl-html.mjs +18 -7
- package/tests/test-repl.mjs +50 -35
- package/Examples/djson.k +0 -894
- package/Examples/ieee.k +0 -969
- package/Examples/karatsuba-mult.k +0 -227
- package/Examples/list.k +0 -10
package/Examples/poly.k
CHANGED
|
@@ -1,144 +1,147 @@
|
|
|
1
|
-
-- Examples/poly.k
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
0 =
|
|
8
|
-
int
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
-- Examples/poly.k — Polymorphic list operations indexed by int[]
|
|
2
|
+
|
|
3
|
+
---------------------------------------------------------------
|
|
4
|
+
-- §1. Base Patterns, Primitives & Imported Relations
|
|
5
|
+
---------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
bits[] = < {} _, B 0, B 1 > = B;
|
|
8
|
+
int[] = < bits[] -, bits[] + >;
|
|
9
|
+
list[X] = < {} nil, { X car, L cdr } cons > = L;
|
|
10
|
+
|
|
11
|
+
_ = {} |_ ?bits[];
|
|
12
|
+
0b = |0 ?bits[];
|
|
13
|
+
0 = _ 0b;
|
|
14
|
+
pos_int = |+ ?int[];
|
|
15
|
+
neg_int = |- ?int[];
|
|
16
|
+
un_pos = ?int[] /+;
|
|
17
|
+
un_neg = ?int[] /-;
|
|
18
|
+
|
|
19
|
+
nil = {} |nil ?list[X];
|
|
20
|
+
cons = |cons ?list[X];
|
|
21
|
+
un_nil = ?list[X] /nil;
|
|
22
|
+
car = ?list[X] /cons .car;
|
|
23
|
+
cdr = ?list[X] /cons .cdr;
|
|
24
|
+
nil? = un_nil nil;
|
|
25
|
+
singleton = { () car, nil cdr } cons;
|
|
26
|
+
|
|
27
|
+
# --lib lib/arithmetics.k --use nat int neg? zero_int? GT inc dec
|
|
28
|
+
nat = @ktrZRefK3zvnUqYYisWBH3MgzcQJD9hs2P5LeSJ7tFrA;
|
|
29
|
+
int = @BgUayEar88hcmcAYM5giDsucyGbtumbaXpyussK8k7vm;
|
|
30
|
+
neg? = @Hwv3CjPRgtJDb7ZGn7cReF9iZ25LFpjv2MXRBJyUGBij;
|
|
31
|
+
zero_int? = @eYsSuRAeFTejC7W9mLLtxuSeebLuszMGxadnZsg4iuUf;
|
|
32
|
+
GT = @qWipdJHEEG3KccU4RVFLrWciLCvP2Cbz7myNxjNtvUCP;
|
|
33
|
+
inc = @FpWRmLNa6guwACtpwYUAuX3usE27WqAKPzQxrVRtyKND;
|
|
34
|
+
dec = @3YRXdfNimGSWfSpCcTzBauTU7HRrjLUFMp4dbRZGhJXv;
|
|
35
|
+
|
|
36
|
+
# --lib lib/list.k --use rev_append reverse
|
|
37
|
+
rev_append = @7xb2uLU5cS7XgmFbKMrvg4UwpcTQ3whSMzBMeaSnGwkm;
|
|
38
|
+
reverse = @peiZq2evd5u4Qk42AT3W4YmWZZ3JAeXNKTnmDtpb9EZh;
|
|
39
|
+
|
|
40
|
+
---------------------------------------------------------------
|
|
41
|
+
-- §2. Polymorphic List Operations
|
|
42
|
+
---------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
-- Decrement a positive integer and normalize
|
|
45
|
+
pred_int = dec un_pos nat pos_int;
|
|
26
46
|
|
|
27
47
|
-- Concatenate two lists: {xs, ys} -> list
|
|
28
|
-
concat =
|
|
29
|
-
|
|
30
|
-
-- Reverse a list: xs -> list
|
|
31
|
-
reverse = ? list[X] {() xs, nil acc} _reverse;
|
|
32
|
-
_reverse = ? { list[X] xs, list[X] acc } <
|
|
33
|
-
{.xs cdr xs, {.xs car car, .acc cdr} cons acc} _reverse,
|
|
34
|
-
.acc
|
|
35
|
-
>;
|
|
48
|
+
concat = { .xs reverse x, .ys y } rev_append;
|
|
36
49
|
|
|
37
50
|
-- Length of a list: xs -> int
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
{.xs
|
|
41
|
-
{.xs cdr xs, .acc inc acc} _length
|
|
51
|
+
length_loop = <
|
|
52
|
+
{ .xs un_nil if, .acc then } .then,
|
|
53
|
+
{ .xs cdr xs, .acc inc acc } length_loop
|
|
42
54
|
>;
|
|
55
|
+
length = { () xs, 0 int acc } length_loop;
|
|
43
56
|
|
|
44
57
|
-- Get N-th element of a list (0-indexed): {n, xs} -> element
|
|
45
|
-
get_nth =
|
|
46
|
-
{.n zero_int? if, .xs car then} .then,
|
|
47
|
-
{.n pred_int n, .xs cdr xs} get_nth
|
|
58
|
+
get_nth = <
|
|
59
|
+
{ .n zero_int? if, .xs car then } .then,
|
|
60
|
+
{ .n pred_int n, .xs cdr xs } get_nth
|
|
48
61
|
>;
|
|
49
62
|
|
|
50
63
|
-- Split list at index N: {n, xs} -> {first, second}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
{.
|
|
54
|
-
{.xs
|
|
55
|
-
{.n pred_int n, .xs cdr xs, {.xs car car, .acc cdr} cons acc} _split_by
|
|
64
|
+
split_by_loop = <
|
|
65
|
+
{ .n zero_int? if, { .acc reverse first, .xs second } then } .then,
|
|
66
|
+
{ .xs un_nil if, { .acc reverse first, nil second } then } .then,
|
|
67
|
+
{ .n pred_int n, .xs cdr xs, { .xs car car, .acc cdr } cons acc } split_by_loop
|
|
56
68
|
>;
|
|
69
|
+
split_by = { .n n, .xs xs, nil acc } split_by_loop;
|
|
57
70
|
|
|
58
71
|
-- Take the first N elements of a list: {n, xs} -> list
|
|
59
|
-
take =
|
|
72
|
+
take = split_by .first;
|
|
60
73
|
|
|
61
74
|
-- Drop the first N elements of a list: {n, xs} -> list
|
|
62
|
-
drop =
|
|
63
|
-
{.n zero_int? if, .xs then} .then,
|
|
64
|
-
{.xs
|
|
65
|
-
{.n pred_int n, .xs cdr xs} drop
|
|
75
|
+
drop = <
|
|
76
|
+
{ .n zero_int? if, .xs then } .then,
|
|
77
|
+
{ .xs un_nil if, nil then } .then,
|
|
78
|
+
{ .n pred_int n, .xs cdr xs } drop
|
|
66
79
|
>;
|
|
67
80
|
|
|
68
81
|
-- Repeat element X, N times: {n, x} -> list
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
{.n
|
|
72
|
-
{.n pred_int n, .x x, {.x car, .acc cdr} cons acc} _repeat
|
|
82
|
+
repeat_loop = <
|
|
83
|
+
{ .n zero_int? if, .acc then } .then,
|
|
84
|
+
{ .n pred_int n, .x x, { .x car, .acc cdr } cons acc } repeat_loop
|
|
73
85
|
>;
|
|
86
|
+
repeat = { .n n, .x x, nil acc } repeat_loop;
|
|
74
87
|
|
|
75
88
|
-- Append an element to the end of a list: {x, xs} -> list
|
|
76
|
-
append =
|
|
77
|
-
{.xs xs, {.x car, nil cdr} cons ys} concat;
|
|
89
|
+
append = { .xs xs, .x singleton ys } concat;
|
|
78
90
|
|
|
79
91
|
-- Zip two lists into a list of {x, y} pairs: {xs, ys} -> list
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
{
|
|
83
|
-
|
|
92
|
+
zip_loop = <
|
|
93
|
+
{ < .xs un_nil, .ys un_nil > if, .acc reverse then } .then,
|
|
94
|
+
{
|
|
95
|
+
.xs cdr xs,
|
|
84
96
|
.ys cdr ys,
|
|
85
|
-
{
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
97
|
+
{ { .xs car x, .ys car y } car, .acc cdr } cons acc
|
|
98
|
+
} zip_loop
|
|
99
|
+
>;
|
|
100
|
+
zip = { .xs xs, .ys ys, nil acc } zip_loop;
|
|
101
|
+
|
|
102
|
+
-- Generate list of integers: n -> [0, 1, ..., n]
|
|
103
|
+
gen_list_loop = <
|
|
104
|
+
{ .n neg? if, .list then } .then,
|
|
105
|
+
{ .n dec n, { .n car, .list cdr } cons list } gen_list_loop
|
|
90
106
|
>;
|
|
107
|
+
gen_list = { () n, nil list } gen_list_loop;
|
|
91
108
|
|
|
92
|
-
--
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
{.
|
|
96
|
-
{.
|
|
109
|
+
-- Identity filter on {x, y} when x > y
|
|
110
|
+
gt? = <
|
|
111
|
+
{ .x un_neg if1, .y un_pos if2, () then } .then,
|
|
112
|
+
{ { .x un_neg y, .y un_neg x } GT if, () then } .then,
|
|
113
|
+
{ { .x un_pos x, .y un_pos y } GT if, () then } .then
|
|
97
114
|
>;
|
|
98
115
|
|
|
99
|
-
--
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
{.x/- if1, .y/+ if2, () then} .then,
|
|
103
|
-
{{.x/- y, .y/- x} GT if, () then} .then,
|
|
104
|
-
{{.x/+ x, .y/+ y} GT if, () then} .then
|
|
105
|
-
>
|
|
106
|
-
;
|
|
107
|
-
|
|
108
|
-
-- Split list into two halves by alternating elements:
|
|
109
|
-
-- xs -> {first, second}
|
|
110
|
-
split = ? list[X] {() xs, nil first, nil second} _split;
|
|
111
|
-
_split = ? { list[X] xs, list[X] first, list[X] second } <
|
|
112
|
-
{.xs /nil if, {.first first, .second second} then} .then,
|
|
116
|
+
-- Split list into two halves by alternating elements: xs -> {first, second}
|
|
117
|
+
split_loop = <
|
|
118
|
+
{ .xs un_nil if, { .first first, .second second } then } .then,
|
|
113
119
|
{
|
|
114
|
-
.xs cdr
|
|
115
|
-
{{.xs car car, .first cdr} cons first, .second second} then
|
|
120
|
+
.xs cdr un_nil if,
|
|
121
|
+
{ { .xs car car, .first cdr } cons first, .second second } then
|
|
116
122
|
} .then,
|
|
117
123
|
{
|
|
118
124
|
.xs cdr cdr xs,
|
|
119
|
-
{.xs car car, .first cdr} cons first,
|
|
120
|
-
{.xs cdr car car, .second cdr} cons second
|
|
121
|
-
}
|
|
125
|
+
{ .xs car car, .first cdr } cons first,
|
|
126
|
+
{ .xs cdr car car, .second cdr } cons second
|
|
127
|
+
} split_loop
|
|
122
128
|
>;
|
|
129
|
+
split = { () xs, nil first, nil second } split_loop;
|
|
123
130
|
|
|
124
131
|
-- Merge two sorted lists: {xs, ys} -> list
|
|
125
|
-
merge =
|
|
126
|
-
{.xs
|
|
127
|
-
{.ys
|
|
132
|
+
merge = <
|
|
133
|
+
{ .xs un_nil if, .ys then } .then,
|
|
134
|
+
{ .ys un_nil if, .xs then } .then,
|
|
128
135
|
{
|
|
129
|
-
{.xs car x, .ys car y} gt? if,
|
|
130
|
-
{.ys car car, {.xs xs, .ys cdr ys} merge cdr} cons then
|
|
136
|
+
{ .xs car x, .ys car y } gt? if,
|
|
137
|
+
{ .ys car car, { .xs xs, .ys cdr ys } merge cdr } cons then
|
|
131
138
|
} .then,
|
|
132
|
-
{.xs car car, {.xs cdr xs, .ys ys} merge cdr} cons
|
|
139
|
+
{ .xs car car, { .xs cdr xs, .ys ys } merge cdr } cons
|
|
133
140
|
>;
|
|
134
141
|
|
|
135
142
|
-- Sort a list in ascending order (merge sort): xs -> list
|
|
136
|
-
sort =
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
>
|
|
142
|
-
> ? list[X];
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
sort = <
|
|
144
|
+
un_nil nil,
|
|
145
|
+
{ cdr un_nil if, car singleton then } .then,
|
|
146
|
+
split { .first sort xs, .second sort ys } merge
|
|
147
|
+
>;
|
package/README.md
CHANGED
|
@@ -63,18 +63,26 @@ relations, libraries, and codecs.
|
|
|
63
63
|
|
|
64
64
|
| File | Purpose |
|
|
65
65
|
| --- | --- |
|
|
66
|
-
| [
|
|
66
|
+
| [lib/bit.k](lib/bit.k) | Bit, boolean, and ternary ordering (`bit[]`, `bool[]`, `order[]`) |
|
|
67
|
+
| [lib/bits.k](lib/bits.k) | Arbitrary-width bit strings and 8-bit bytes (`bits[]`, `byte[]`) |
|
|
68
|
+
| [lib/arithmetics.k](lib/arithmetics.k) | Signed arbitrary-precision integer arithmetic (`int[]`, `plus`, `minus`, `times`, `karatsuba`, `div`, `mod`, `gcd`, `lcm`) |
|
|
69
|
+
| [lib/list.k](lib/list.k) | Parametric linked lists (`list[X]`) |
|
|
70
|
+
| [lib/btrie.k](lib/btrie.k) | Parametric binary tries and optional values (`opt[V]`, `btrie[V]`) |
|
|
71
|
+
| [lib/string.k](lib/string.k) | ASCII bytes, Unicode codepoints, and strings (`ascii[]`, `unicode[]`, `string[]`) |
|
|
72
|
+
| [lib/ieee.k](lib/ieee.k) | Binary64 types and `parseFloat64` / `printFloat64` (`float64[]`) |
|
|
73
|
+
| [lib/djson.k](lib/djson.k) | JSON text parsing (`parseJson`) and dynamic JSON operations (`djson[]`) |
|
|
74
|
+
| [core.k](core.k) | Standalone bit, Unicode string, and self-hosted pattern schemas |
|
|
67
75
|
| [Examples/nat.k](Examples/nat.k) | Small Peano arithmetic example |
|
|
68
|
-
| [Examples/
|
|
69
|
-
| [Examples/
|
|
70
|
-
| [Examples/
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
| [Examples/arithmetics.k](Examples/arithmetics.k) | Factorial, Fibonacci, and exact rational arithmetic (`rat[]`); load `lib/bit.k`, `lib/bits.k`, and `lib/arithmetics.k` first |
|
|
77
|
+
| [Examples/poly.k](Examples/poly.k) | Polymorphic list operations indexed by `int[]`; load `lib/bit.k`, `lib/bits.k`, `lib/arithmetics.k`, and `lib/list.k` first |
|
|
78
|
+
| [Examples/ieee-arithmetics.k](Examples/ieee-arithmetics.k) | Binary64 arithmetic returning `{result, flags}`; load `lib/bit.k` through `lib/ieee.k` first |
|
|
79
|
+
|
|
80
|
+
Load libraries explicitly with `:load` in the REPL. Source files declare
|
|
81
|
+
dependencies and imported symbols via `# --lib <dependency> --use <specs>`
|
|
82
|
+
header directives at the top of the file, allowing compilation with
|
|
83
|
+
`--lib <dependency.klib>` without repeating `--use` flags on the CLI.
|
|
84
|
+
Command-line `--use` flags remain available for ad-hoc expressions. See
|
|
85
|
+
[file formats and tools](DOCS/FILE_FORMATS.md).
|
|
78
86
|
|
|
79
87
|
## JavaScript API
|
|
80
88
|
|
package/backend-api.mjs
CHANGED
|
@@ -21,7 +21,9 @@ export {
|
|
|
21
21
|
compileLibrary,
|
|
22
22
|
decodeObject,
|
|
23
23
|
loadLibrary,
|
|
24
|
-
|
|
24
|
+
buildUsePreamble,
|
|
25
|
+
parseHeaderDirectives,
|
|
26
|
+
parseHeaderUses
|
|
25
27
|
} from "./object.mjs";
|
|
26
28
|
export { createState, evaluateInput } from "./repl.mjs";
|
|
27
29
|
export { parse as parseFloat64, print as printFloat64 } from "./codecs/ieee.mjs";
|
package/backends/llvm/README.md
CHANGED
|
@@ -56,7 +56,7 @@ Common compile options:
|
|
|
56
56
|
|
|
57
57
|
- `-o` / `--output path`: output path instead of a positional output.
|
|
58
58
|
- `--lib file`: one `.k` or `.klib` dependency.
|
|
59
|
-
- `--
|
|
59
|
+
- `--use name` or `--use libraryName:localName`: repeatable alias imports.
|
|
60
60
|
- `--pkir`: compile source through PKIR.
|
|
61
61
|
- `--retype relation`: select the relation for envelope specialization.
|
|
62
62
|
- `--opt flag`: Clang optimization flag.
|
|
@@ -39,8 +39,8 @@ function helpText() {
|
|
|
39
39
|
" --input-pattern pattern Input pattern JSON or file for spec.",
|
|
40
40
|
" --retype rel Relation to specialize (default: main).",
|
|
41
41
|
" --lib file Load .klib or .k library before build.",
|
|
42
|
-
" --
|
|
43
|
-
"
|
|
42
|
+
" --use spec Use library alias in scope (repeatable).",
|
|
43
|
+
" spec is 'name' or 'libname:localname'.",
|
|
44
44
|
" --opt flag Clang optimization flag (default: -O3).",
|
|
45
45
|
" -h, --help Show this help.",
|
|
46
46
|
"",
|
|
@@ -52,7 +52,7 @@ function helpText() {
|
|
|
52
52
|
"Examples:",
|
|
53
53
|
` ${prog} 'x = |x; x x' -o program.exe`,
|
|
54
54
|
` ${prog} program.k program.exe`,
|
|
55
|
-
` ${prog} --lib Examples/poly.k --
|
|
55
|
+
` ${prog} --lib Examples/poly.k --use concat 's' -o s.exe`
|
|
56
56
|
].join("\n");
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -119,7 +119,7 @@ try {
|
|
|
119
119
|
|
|
120
120
|
const inputPattern = readPattern(rawInputPattern);
|
|
121
121
|
|
|
122
|
-
const { libraries,
|
|
122
|
+
const { libraries, useSpecs, usePKIR } =
|
|
123
123
|
parseCompileOptions(remainingArgs);
|
|
124
124
|
const input = resolveProgramInput(remainingArgs, {
|
|
125
125
|
allowStdinSource: true
|
|
@@ -154,7 +154,7 @@ try {
|
|
|
154
154
|
} else {
|
|
155
155
|
const object = await compileProgramInputToObject(input, {
|
|
156
156
|
libraries,
|
|
157
|
-
|
|
157
|
+
useSpecs,
|
|
158
158
|
stdin
|
|
159
159
|
});
|
|
160
160
|
|
|
@@ -33,7 +33,7 @@ function helpText() {
|
|
|
33
33
|
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
34
34
|
" --retype rel Relation to compile (default: object main).",
|
|
35
35
|
" --lib file Load one .klib or .k library before compiling.",
|
|
36
|
-
" --
|
|
36
|
+
" --use spec Use library alias in scope (repeatable).",
|
|
37
37
|
" spec is 'name' or 'libname:localname'.",
|
|
38
38
|
" -h, --help Show this help.",
|
|
39
39
|
"",
|
|
@@ -41,7 +41,7 @@ function helpText() {
|
|
|
41
41
|
` ${prog} 'x = |x; x x' program.ll`,
|
|
42
42
|
` ${prog} program.k -o program.ll`,
|
|
43
43
|
` ${prog} --pkir program.k -o program.ll`,
|
|
44
|
-
` ${prog} --lib core.klib --
|
|
44
|
+
` ${prog} --lib core.klib --use add program.k program.ll`
|
|
45
45
|
].join("\n");
|
|
46
46
|
}
|
|
47
47
|
|
|
@@ -78,7 +78,7 @@ try {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
const { libraries,
|
|
81
|
+
const { libraries, useSpecs, usePKIR } =
|
|
82
82
|
parseCompileOptions(remainingArgs);
|
|
83
83
|
const input = resolveProgramInput(remainingArgs, {
|
|
84
84
|
allowStdinSource: true
|
|
@@ -110,7 +110,7 @@ try {
|
|
|
110
110
|
} else {
|
|
111
111
|
const object = await compileProgramInputToObject(input, {
|
|
112
112
|
libraries,
|
|
113
|
-
|
|
113
|
+
useSpecs,
|
|
114
114
|
stdin
|
|
115
115
|
});
|
|
116
116
|
|
|
@@ -40,7 +40,7 @@ function helpText() {
|
|
|
40
40
|
"",
|
|
41
41
|
"Options:",
|
|
42
42
|
" --lib file Load one .klib or .k library before compiling.",
|
|
43
|
-
" --
|
|
43
|
+
" --use spec Use library alias in scope (repeatable).",
|
|
44
44
|
" spec is 'name' or 'libname:localname'.",
|
|
45
45
|
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
46
46
|
" --retype rel Relation to specialize (default: object main).",
|
|
@@ -58,7 +58,7 @@ function helpText() {
|
|
|
58
58
|
"Examples:",
|
|
59
59
|
` echo '"abc"' | k-parse | ${prog} '{.1 0}' | k-print`,
|
|
60
60
|
` echo '{"x":1,"y":2}' | k-json --parse | \\`,
|
|
61
|
-
` ${prog} --lib Examples/poly.k --
|
|
61
|
+
` ${prog} --lib Examples/poly.k --use concat \\`,
|
|
62
62
|
` 's={()xs,()ys}concat; s' | k-json --print`
|
|
63
63
|
].join("\n");
|
|
64
64
|
}
|
|
@@ -174,7 +174,7 @@ async function main() {
|
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
const { libraries,
|
|
177
|
+
const { libraries, useSpecs, usePKIR } =
|
|
178
178
|
parseCompileOptions(remainingArgs);
|
|
179
179
|
const programInput = resolveProgramInput(remainingArgs);
|
|
180
180
|
const inputWirePath = remainingArgs.shift();
|
|
@@ -200,7 +200,7 @@ async function main() {
|
|
|
200
200
|
const inputKey = JSON.stringify({
|
|
201
201
|
programInput,
|
|
202
202
|
libraries: libraries.map((lib) => Object.keys(lib.rels || {})),
|
|
203
|
-
|
|
203
|
+
useSpecs
|
|
204
204
|
});
|
|
205
205
|
const key = computeCacheKey({
|
|
206
206
|
fingerprint,
|
|
@@ -233,7 +233,7 @@ async function main() {
|
|
|
233
233
|
} else {
|
|
234
234
|
const object = await compileProgramInputToObject(programInput, {
|
|
235
235
|
libraries,
|
|
236
|
-
|
|
236
|
+
useSpecs,
|
|
237
237
|
stdin
|
|
238
238
|
});
|
|
239
239
|
compileObjectToExecutable(object, tmpPath, {
|
|
@@ -264,7 +264,7 @@ async function main() {
|
|
|
264
264
|
} else {
|
|
265
265
|
const object = await compileProgramInputToObject(programInput, {
|
|
266
266
|
libraries,
|
|
267
|
-
|
|
267
|
+
useSpecs,
|
|
268
268
|
stdin
|
|
269
269
|
});
|
|
270
270
|
compileObjectToExecutable(object, exePath, {
|
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
compileObjectBuffer,
|
|
6
6
|
decodeObject,
|
|
7
7
|
loadLibrary,
|
|
8
|
-
|
|
8
|
+
buildUsePreamble,
|
|
9
|
+
parseHeaderUses
|
|
9
10
|
} from "@fraczak/k/object.mjs";
|
|
10
11
|
|
|
11
12
|
export function readAll(stream) {
|
|
@@ -35,7 +36,7 @@ export function loadLibraryFromPath(libPath) {
|
|
|
35
36
|
|
|
36
37
|
export function parseCompileOptions(args) {
|
|
37
38
|
const libraries = [];
|
|
38
|
-
const
|
|
39
|
+
const useSpecs = [];
|
|
39
40
|
let loadedLibrary = false;
|
|
40
41
|
let usePKIR = false;
|
|
41
42
|
|
|
@@ -53,15 +54,15 @@ export function parseCompileOptions(args) {
|
|
|
53
54
|
if (!libPath) throw new Error("--lib requires a file argument");
|
|
54
55
|
libraries.push(loadLibraryFromPath(libPath));
|
|
55
56
|
loadedLibrary = true;
|
|
56
|
-
} else if (args[0] === "--
|
|
57
|
+
} else if (args[0] === "--use") {
|
|
57
58
|
args.shift();
|
|
58
59
|
const spec = args.shift();
|
|
59
60
|
if (!spec) {
|
|
60
61
|
throw new Error(
|
|
61
|
-
"--
|
|
62
|
+
"--use requires a spec argument (name or libname:localname)"
|
|
62
63
|
);
|
|
63
64
|
}
|
|
64
|
-
|
|
65
|
+
useSpecs.push(spec);
|
|
65
66
|
} else if (args[0] === "--pkir") {
|
|
66
67
|
args.shift();
|
|
67
68
|
usePKIR = true;
|
|
@@ -72,7 +73,7 @@ export function parseCompileOptions(args) {
|
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
return { libraries,
|
|
76
|
+
return { libraries, useSpecs, usePKIR };
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
export function fileInputType(filePath) {
|
|
@@ -108,15 +109,17 @@ export function resolveProgramInput(args, { allowStdinSource = false } = {}) {
|
|
|
108
109
|
};
|
|
109
110
|
}
|
|
110
111
|
|
|
111
|
-
export {
|
|
112
|
+
export { buildUsePreamble };
|
|
112
113
|
|
|
113
114
|
export async function compileProgramInputToObject(
|
|
114
115
|
input,
|
|
115
|
-
{ libraries = [], exportSpecs = [], stdin = null } = {}
|
|
116
|
+
{ libraries = [], useSpecs = [], exportSpecs = [], stdin = null } = {}
|
|
116
117
|
) {
|
|
118
|
+
const specs = useSpecs.length > 0 ? useSpecs : exportSpecs;
|
|
117
119
|
let object;
|
|
118
120
|
if (input.kind === "inline-source") {
|
|
119
|
-
const
|
|
121
|
+
const allSpecs = [...parseHeaderUses(input.source), ...specs];
|
|
122
|
+
const preamble = buildUsePreamble(allSpecs, libraries);
|
|
120
123
|
const objBuffer = compileObjectBuffer(preamble + input.source, {
|
|
121
124
|
libraries,
|
|
122
125
|
source: input.sourceName
|
|
@@ -127,7 +130,8 @@ export async function compileProgramInputToObject(
|
|
|
127
130
|
throw new Error("stdin source requested without a stdin stream");
|
|
128
131
|
}
|
|
129
132
|
const source = (await readAll(stdin)).toString("utf8");
|
|
130
|
-
const
|
|
133
|
+
const allSpecs = [...parseHeaderUses(source), ...specs];
|
|
134
|
+
const preamble = buildUsePreamble(allSpecs, libraries);
|
|
131
135
|
const objBuffer = compileObjectBuffer(preamble + source, {
|
|
132
136
|
libraries,
|
|
133
137
|
source: input.sourceName
|
|
@@ -135,7 +139,8 @@ export async function compileProgramInputToObject(
|
|
|
135
139
|
object = decodeObject(objBuffer);
|
|
136
140
|
} else if (input.kind === "source-file") {
|
|
137
141
|
const source = fs.readFileSync(input.path, "utf8");
|
|
138
|
-
const
|
|
142
|
+
const allSpecs = [...parseHeaderUses(source), ...specs];
|
|
143
|
+
const preamble = buildUsePreamble(allSpecs, libraries);
|
|
139
144
|
const objBuffer = compileObjectBuffer(preamble + source, {
|
|
140
145
|
libraries,
|
|
141
146
|
source: input.sourceName
|
|
@@ -161,7 +166,7 @@ export async function compileProgramInputToObject(
|
|
|
161
166
|
}
|
|
162
167
|
|
|
163
168
|
export default {
|
|
164
|
-
|
|
169
|
+
buildUsePreamble,
|
|
165
170
|
compileProgramInputToObject,
|
|
166
171
|
fileInputType,
|
|
167
172
|
parseCompileOptions,
|
|
@@ -121,7 +121,10 @@ printf '2' \
|
|
|
121
121
|
|
|
122
122
|
node --input-type=module -e "import assert from 'node:assert/strict'; import { spawn } from 'node:child_process'; import { encodeToWire, decodeWire } from '../../codecs/runtime/prefix-codec.mjs'; import { parse, INT_PATTERN } from '../../codecs/int.mjs'; const input = encodeToWire(parse('2'), INT_PATTERN); const header = Buffer.alloc(4); header.writeUInt32BE(input.length); const child = spawn(process.argv[1], ['--server']); const chunks = []; const stderr = []; child.stdout.on('data', (chunk) => chunks.push(chunk)); child.stderr.on('data', (chunk) => stderr.push(chunk)); child.stdin.end(Buffer.concat([header, input])); const status = await new Promise((resolve, reject) => { child.on('error', reject); child.on('close', resolve); }); assert.equal(status, 0, Buffer.concat(stderr).toString('utf8')); const output = Buffer.concat(chunks); const length = output.readUInt32BE(0); assert.equal(output.length, 4 + length); assert.deepEqual(decodeWire(output.subarray(4)).value.toJSON(), parse('2').toJSON());" "$TMP_DIR/id-int-exe"
|
|
123
123
|
|
|
124
|
-
node
|
|
124
|
+
node ../../objects/compile.mjs ../../lib/bit.k "$TMP_DIR/bit.klib"
|
|
125
|
+
node ../../objects/compile.mjs --lib "$TMP_DIR/bit.klib" ../../lib/bits.k "$TMP_DIR/bits.klib"
|
|
126
|
+
node ../../objects/compile.mjs --lib "$TMP_DIR/bits.klib" ../../lib/arithmetics.k "$TMP_DIR/arithmetics.klib"
|
|
127
|
+
node ./bin/k-llvm-build.mjs --lib "$TMP_DIR/arithmetics.klib" --use times 's = ?< <bits 0, bits 1, {} _>=bits "+", bits "-" > {()x, ()y} times; s' -o "$TMP_DIR/square-exe"
|
|
125
128
|
printf '987654321' \
|
|
126
129
|
| node ../../codecs/int.mjs --parse \
|
|
127
130
|
| "$TMP_DIR/square-exe" \
|
package/backends/wasm/README.md
CHANGED
|
@@ -43,8 +43,8 @@ filename. Compile tools read source from stdin when the program is omitted.
|
|
|
43
43
|
Source files need a terminal expression; a `.klib` alone is not an executable
|
|
44
44
|
input.
|
|
45
45
|
|
|
46
|
-
Compile commands accept one `--lib file` and repeated `--
|
|
47
|
-
`--
|
|
46
|
+
Compile commands accept one `--lib file` and repeated `--use alias` or
|
|
47
|
+
`--use libraryAlias:localAlias` options before the program. Libraries can
|
|
48
48
|
be `.k` source or `.klib` objects. `--pkir` chooses the experimental PKIR
|
|
49
49
|
lowering path; `-o` / `--output` selects an output path.
|
|
50
50
|
|
|
@@ -55,7 +55,7 @@ For a library example using only a small definition:
|
|
|
55
55
|
|
|
56
56
|
```sh
|
|
57
57
|
node objects/compile.mjs 'greet = {} |hello;' /tmp/k-demo.klib
|
|
58
|
-
node backends/wasm/bin/k-wasm-compile.mjs --lib /tmp/k-demo.klib --
|
|
58
|
+
node backends/wasm/bin/k-wasm-compile.mjs --lib /tmp/k-demo.klib --use greet '?{} greet' /tmp/k-greet.wasm
|
|
59
59
|
node codecs/unit.mjs --parse |
|
|
60
60
|
node backends/wasm/bin/k-wasm-run.mjs /tmp/k-greet.wasm |
|
|
61
61
|
node codecs/k-print.mjs
|
|
@@ -28,7 +28,7 @@ function helpText() {
|
|
|
28
28
|
" -o, --output path Specify output file path explicitly.",
|
|
29
29
|
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
30
30
|
" --lib file Load one .klib or .k library before compiling.",
|
|
31
|
-
" --
|
|
31
|
+
" --use spec Use library alias in scope (repeatable).",
|
|
32
32
|
" spec is 'name' or 'libname:localname'.",
|
|
33
33
|
" -h, --help Show this help.",
|
|
34
34
|
"",
|
|
@@ -36,7 +36,7 @@ function helpText() {
|
|
|
36
36
|
` ${prog} 'x = |x; x x' program.wasm`,
|
|
37
37
|
` ${prog} program.k -o program.wasm`,
|
|
38
38
|
` ${prog} --pkir program.k -o program.wasm`,
|
|
39
|
-
` ${prog} --lib core.klib --
|
|
39
|
+
` ${prog} --lib core.klib --use add program.k program.wasm`
|
|
40
40
|
].join("\n");
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -66,7 +66,7 @@ try {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
const { libraries,
|
|
69
|
+
const { libraries, useSpecs, usePKIR } =
|
|
70
70
|
parseCompileOptions(remainingArgs);
|
|
71
71
|
const input = resolveProgramInput(remainingArgs, {
|
|
72
72
|
allowStdinSource: true
|
|
@@ -76,7 +76,7 @@ try {
|
|
|
76
76
|
const outputPath = explicitOutput || positionalOutput || null;
|
|
77
77
|
const artifact = await compileProgramInput(input, {
|
|
78
78
|
libraries,
|
|
79
|
-
|
|
79
|
+
useSpecs,
|
|
80
80
|
stdin,
|
|
81
81
|
usePKIR
|
|
82
82
|
});
|
|
@@ -28,7 +28,7 @@ function helpText() {
|
|
|
28
28
|
"",
|
|
29
29
|
"Options:",
|
|
30
30
|
" --lib file Load one .klib or .k library before compiling.",
|
|
31
|
-
" --
|
|
31
|
+
" --use spec Use library alias in scope (repeatable).",
|
|
32
32
|
" spec is 'name' or 'libname:localname'.",
|
|
33
33
|
" --pkir Lower via Polymorphic K IR (PKIR).",
|
|
34
34
|
" -h, --help Show this help.",
|
|
@@ -39,7 +39,7 @@ function helpText() {
|
|
|
39
39
|
"Examples:",
|
|
40
40
|
` echo '"abc"' | k-parse | ${prog} '{.1 0}' | k-print`,
|
|
41
41
|
` echo '{"x":1,"y":2}' | k-json --parse | \\`,
|
|
42
|
-
` ${prog} --lib Examples/poly.k --
|
|
42
|
+
` ${prog} --lib Examples/poly.k --use concat \\`,
|
|
43
43
|
` 's={()xs,()ys}concat; s' | k-json --print`
|
|
44
44
|
].join("\n");
|
|
45
45
|
}
|
|
@@ -55,7 +55,7 @@ async function main() {
|
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
const { libraries,
|
|
58
|
+
const { libraries, useSpecs, usePKIR } = parseCompileOptions(args);
|
|
59
59
|
const programInput = resolveProgramInput(args);
|
|
60
60
|
const inputPath = args.shift();
|
|
61
61
|
if (args.length > 0) throw new Error("Too many arguments");
|
|
@@ -70,7 +70,7 @@ async function main() {
|
|
|
70
70
|
const { pattern: inputEnvelopePattern } = decodeWire(inputBuffer);
|
|
71
71
|
const artifact = await compileProgramInput(programInput, {
|
|
72
72
|
libraries,
|
|
73
|
-
|
|
73
|
+
useSpecs,
|
|
74
74
|
inputEnvelopePattern,
|
|
75
75
|
usePKIR
|
|
76
76
|
});
|