@fraczak/k 1.1.4 → 1.2.1
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/Benchmark/range1000 +1000 -0
- package/Benchmark/range200 +200 -0
- package/README.md +222 -194
- package/package.json +6 -7
- package/parser.jison +12 -13
- package/parser.js +50 -52
- package/repl.coffee +23 -0
- package/repl.js +43 -0
- package/repl.sh +44 -0
- package/run.coffee +50 -44
- package/run.js +86 -74
- package/test.coffee +2 -0
- package/bnat.k +0 -121
- package/code.k +0 -19
- package/error.k +0 -8
- package/factorial.k +0 -11
- package/fib.k +0 -15
- package/pow.k +0 -7
- package/test.k +0 -13
- package/typed-fib.k +0 -19
package/run.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// Generated by CoffeeScript 2.7.0
|
|
2
2
|
(function() {
|
|
3
|
-
var builtin, codes, run, valid, verify,
|
|
3
|
+
var builtin, codes, fs, run, valid, verify,
|
|
4
4
|
modulo = function(a, b) { return (+a % (b = +b) + b) % b; };
|
|
5
5
|
|
|
6
|
+
fs = require("fs");
|
|
7
|
+
|
|
6
8
|
valid = function(x) {
|
|
7
9
|
if (isNaN(x)) {
|
|
8
10
|
return void 0;
|
|
@@ -17,6 +19,13 @@
|
|
|
17
19
|
console.log(`_log!: ${JSON.stringify(arg)}`);
|
|
18
20
|
return arg;
|
|
19
21
|
},
|
|
22
|
+
"fromFILE": function(fileName) {
|
|
23
|
+
return fs.readFileSync(fileName).toString();
|
|
24
|
+
},
|
|
25
|
+
"toFILE": function([x, fileName]) {
|
|
26
|
+
fs.writeFileSync(fileName, x);
|
|
27
|
+
return x;
|
|
28
|
+
},
|
|
20
29
|
"GT": function(args) {
|
|
21
30
|
var _, ok;
|
|
22
31
|
[ok, _] = (function([last, ...args]) {
|
|
@@ -111,15 +120,24 @@
|
|
|
111
120
|
};
|
|
112
121
|
|
|
113
122
|
verify = function(code, value) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
123
|
+
if (code == null) {
|
|
124
|
+
// representatives = run.defs.representatives
|
|
125
|
+
// defCodes = JSON.stringify run.defs.codes
|
|
126
|
+
// console.log {code,value, representatives, defCodes}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
117
129
|
switch (code.code) {
|
|
118
130
|
case "vector":
|
|
131
|
+
if (!Array.isArray(value)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
119
134
|
return value.every(function(x) {
|
|
120
135
|
return verify(code.vector, x);
|
|
121
136
|
});
|
|
122
137
|
case "product":
|
|
138
|
+
if ("object" !== typeof value) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
123
141
|
return (function(fields) {
|
|
124
142
|
if (fields.length !== Object.keys(code.product).length) {
|
|
125
143
|
return false;
|
|
@@ -129,6 +147,9 @@
|
|
|
129
147
|
});
|
|
130
148
|
})(Object.keys(value));
|
|
131
149
|
case "union":
|
|
150
|
+
if ("object" !== typeof value) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
132
153
|
return (function(fields) {
|
|
133
154
|
if (fields.length !== 1) {
|
|
134
155
|
return false;
|
|
@@ -146,83 +167,74 @@
|
|
|
146
167
|
};
|
|
147
168
|
|
|
148
169
|
run = function(exp, value) {
|
|
149
|
-
|
|
170
|
+
"use strict";
|
|
171
|
+
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2, ref3, result;
|
|
150
172
|
if (value === void 0) {
|
|
151
173
|
// console.log {exp,value}
|
|
152
174
|
return void 0;
|
|
153
175
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (verify(exp.code, value)) {
|
|
158
|
-
return value;
|
|
159
|
-
}
|
|
160
|
-
break;
|
|
161
|
-
case "identity":
|
|
176
|
+
switch (exp.op) {
|
|
177
|
+
case "code":
|
|
178
|
+
if (verify(exp.code, value)) {
|
|
162
179
|
return value;
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
return exp.vector.map(function(exp) {
|
|
191
|
-
var result;
|
|
192
|
-
result = run(exp, value);
|
|
193
|
-
if (result === void 0) {
|
|
194
|
-
throw new Error("Undefined");
|
|
195
|
-
}
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
case "identity":
|
|
183
|
+
return value;
|
|
184
|
+
case "str":
|
|
185
|
+
case "int":
|
|
186
|
+
return exp[exp.op];
|
|
187
|
+
case "ref":
|
|
188
|
+
defn = (ref = run.defs.rels[exp.ref]) != null ? ref[0] : void 0;
|
|
189
|
+
if (defn != null) {
|
|
190
|
+
return run(defn, value);
|
|
191
|
+
}
|
|
192
|
+
return builtin[exp.ref](value);
|
|
193
|
+
case "dot":
|
|
194
|
+
return value[exp.dot];
|
|
195
|
+
case "comp":
|
|
196
|
+
return exp.comp.reduce(function(value, exp) {
|
|
197
|
+
if (value !== void 0) {
|
|
198
|
+
return run(exp, value);
|
|
199
|
+
}
|
|
200
|
+
}, value);
|
|
201
|
+
case "union":
|
|
202
|
+
ref1 = exp.union;
|
|
203
|
+
for (i = 0, len = ref1.length; i < len; i++) {
|
|
204
|
+
e = ref1[i];
|
|
205
|
+
result = run(e, value);
|
|
206
|
+
if (result !== void 0) {
|
|
196
207
|
return result;
|
|
197
|
-
});
|
|
198
|
-
case "union":
|
|
199
|
-
ref1 = exp.union;
|
|
200
|
-
for (i = 0, len = ref1.length; i < len; i++) {
|
|
201
|
-
e = ref1[i];
|
|
202
|
-
result = (function() {
|
|
203
|
-
try {
|
|
204
|
-
return run(e, value);
|
|
205
|
-
} catch (error) {}
|
|
206
|
-
})();
|
|
207
|
-
if (result !== void 0) {
|
|
208
|
-
return result;
|
|
209
|
-
}
|
|
210
208
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
209
|
+
}
|
|
210
|
+
return void 0;
|
|
211
|
+
case "vector":
|
|
212
|
+
result = [];
|
|
213
|
+
ref2 = exp.vector;
|
|
214
|
+
for (j = 0, len1 = ref2.length; j < len1; j++) {
|
|
215
|
+
e = ref2[j];
|
|
216
|
+
r = run(e, value);
|
|
217
|
+
if (r === void 0) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
result.push(r);
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
case "product":
|
|
224
|
+
result = {};
|
|
225
|
+
ref3 = exp.product;
|
|
226
|
+
for (k = 0, len2 = ref3.length; k < len2; k++) {
|
|
227
|
+
({label, exp} = ref3[k]);
|
|
228
|
+
r = run(exp, value);
|
|
229
|
+
if (r === void 0) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
result[label] = r;
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
default:
|
|
236
|
+
return console.error(exp.op);
|
|
237
|
+
}
|
|
226
238
|
};
|
|
227
239
|
|
|
228
240
|
module.exports = run;
|
package/test.coffee
CHANGED
package/bnat.k
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
-- Standard binary encoding of natural
|
|
2
|
-
-- numbers with addition (+).
|
|
3
|
-
|
|
4
|
-
_ = {{} _};
|
|
5
|
-
_1 = {() "1"};
|
|
6
|
-
_0 = {() "0"};
|
|
7
|
-
|
|
8
|
-
n0 = _ ;
|
|
9
|
-
n1 = _ _1;
|
|
10
|
-
|
|
11
|
-
from_digit = <
|
|
12
|
-
[0, ()] EQ _, -- {_: {}}
|
|
13
|
-
[1, ()] EQ n1, -- {"1": {_: {}}}
|
|
14
|
-
[2, ()] EQ _ _0 _1, -- {"1": {"0": {_: {}}}}
|
|
15
|
-
[3, ()] EQ _ _1 _1, -- {"1": {"1": {_: {}}}}
|
|
16
|
-
[4, ()] EQ _ _0 _0 _1, -- {"1": {"0": {"0": {_: {}}}}}
|
|
17
|
-
[5, ()] EQ _ _1 _0 _1,
|
|
18
|
-
[6, ()] EQ _ _0 _1 _1,
|
|
19
|
-
[7, ()] EQ _ _1 _1 _1,
|
|
20
|
-
[8, ()] EQ _ _0 _0 _0 _1,
|
|
21
|
-
[9, ()] EQ _ _1 _0 _0 _1,
|
|
22
|
-
[10, ()] EQ _ _0 _1 _0 _1
|
|
23
|
-
>;
|
|
24
|
-
|
|
25
|
-
to_digit = <
|
|
26
|
-
._ 0,
|
|
27
|
-
."1"._ 1,
|
|
28
|
-
."1"."0"._ 2,
|
|
29
|
-
."1"."1"._ 3,
|
|
30
|
-
."1"."0"."0"._ 4,
|
|
31
|
-
."1"."0"."1"._ 5,
|
|
32
|
-
."1"."1"."0"._ 6,
|
|
33
|
-
."1"."1"."1"._ 7,
|
|
34
|
-
."1"."0"."0"."0"._ 8,
|
|
35
|
-
."1"."0"."0"."1"._ 9,
|
|
36
|
-
."1"."0"."1"."0"._ 10,
|
|
37
|
-
."1"."0"."1"."1"._ 11,
|
|
38
|
-
."1"."1"."0"."0"._ 12,
|
|
39
|
-
."1"."1"."0"."1"._ 13,
|
|
40
|
-
."1"."1"."1"."0"._ 14,
|
|
41
|
-
."1"."1"."1"."1"._ 15
|
|
42
|
-
> ;
|
|
43
|
-
|
|
44
|
-
remove_leading_zeros =
|
|
45
|
-
< ."0" remove_leading_zeros, () >;
|
|
46
|
-
|
|
47
|
-
_append_zero =
|
|
48
|
-
< ._ _ _0, -- ( _ -> 0 _)
|
|
49
|
-
."1" _append_zero _1, -- ('1 ... -> '1 [$.1 | _append_zero]),
|
|
50
|
-
."0" _append_zero _0 -- ('0 ... -> '0 [$.0 | _append_zero]),
|
|
51
|
-
>
|
|
52
|
-
;
|
|
53
|
-
|
|
54
|
-
_append_one =
|
|
55
|
-
<
|
|
56
|
-
._ _ _1,
|
|
57
|
-
."1" _append_one _1,
|
|
58
|
-
."0" _append_one _0
|
|
59
|
-
>
|
|
60
|
-
;
|
|
61
|
-
|
|
62
|
-
reverse_bnat = {() x, _ y} _reverse_bnat;
|
|
63
|
-
|
|
64
|
-
_reverse_bnat =
|
|
65
|
-
<
|
|
66
|
-
{.x._ x, .y y} .y,
|
|
67
|
-
{.x."0" x, .y _0 y} _reverse_bnat,
|
|
68
|
-
{.x."1" x, .y _1 y} _reverse_bnat
|
|
69
|
-
>;
|
|
70
|
-
|
|
71
|
-
_add2wc =
|
|
72
|
-
-- least significant bit first addition with carry
|
|
73
|
-
--- :{ :bnat x, :bnat y, :[0,1] c}
|
|
74
|
-
-- _log!
|
|
75
|
-
<
|
|
76
|
-
{.x."0" x, .y."0" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _0,
|
|
77
|
-
{.x."0" x, .y."0" y, .c."1" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
78
|
-
|
|
79
|
-
{.x."0" x, .y."1" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
80
|
-
{.x."0" x, .y."1" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
81
|
-
|
|
82
|
-
{.x."1" x, .y."0" y, .c."0" c} {.x x, .y y, _ _0 c} _add2wc _1,
|
|
83
|
-
{.x."1" x, .y."0" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
84
|
-
|
|
85
|
-
{.x."1" x, .y."1" y, .c."0" c} {.x x, .y y, _ _1 c} _add2wc _0,
|
|
86
|
-
{.x."1" x, .y."1" y, .c."1" c} {.x x, .y y, _ _1 c} _add2wc _1,
|
|
87
|
-
{.x._ x, .c."0" c, .y y} .y,
|
|
88
|
-
{.y._ y, .c."0" c, .x x} .x,
|
|
89
|
-
{.x._ x, .c."1" c, .y._ y} n1,
|
|
90
|
-
{.x._ x, .y."0" y, .c."1" c} .y _1,
|
|
91
|
-
{.x."0" x, .y._ y, .c."1" c} .x _1,
|
|
92
|
-
{.x._ x, .y."1" y, .c."1" c} {_ x, .y y, {} _1 c} _add2wc _0,
|
|
93
|
-
{.x."1" x, .y._ y, .c."1" c} {.x x, _ y, {} _1 c} _add2wc _0
|
|
94
|
-
>;
|
|
95
|
-
|
|
96
|
-
plus = <
|
|
97
|
-
SNOC {
|
|
98
|
-
x: .0 from_digit remove_leading_zeros reverse_bnat,
|
|
99
|
-
y: .1 plus remove_leading_zeros reverse_bnat,
|
|
100
|
-
c: {} _0 } _add2wc reverse_bnat,
|
|
101
|
-
.0 from_digit
|
|
102
|
-
>;
|
|
103
|
-
|
|
104
|
-
to_string = <
|
|
105
|
-
._ "0",
|
|
106
|
-
toString
|
|
107
|
-
>;
|
|
108
|
-
|
|
109
|
-
toString = <
|
|
110
|
-
._ "",
|
|
111
|
-
["0", ."0" toString] CONCAT,
|
|
112
|
-
["1", ."1" toString] CONCAT
|
|
113
|
-
>;
|
|
114
|
-
|
|
115
|
-
[(), plus]
|
|
116
|
-
{
|
|
117
|
-
input: .0,
|
|
118
|
-
sum: {raw: .1, digits: .1 <to_digit,"...too big...">, binary: .1 to_string}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
-- example: > echo '[9,9,9,8]' | ./k.sh -k ./bnat.k
|
package/code.k
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
inc = [(),1] PLUS;
|
|
2
|
-
max = <GT .0, .1>; -- max = < SNOC [.0, .1 max] <GT .0, .1>, .0 >;
|
|
3
|
-
$tree = < string leaf, {tree left, tree right} tree>;
|
|
4
|
-
height = $tree <
|
|
5
|
-
.leaf 0,
|
|
6
|
-
.tree [.left height, .right height] max inc
|
|
7
|
-
> $int;
|
|
8
|
-
|
|
9
|
-
<$tree,
|
|
10
|
-
{tree:
|
|
11
|
-
{ left: { leaf: "a"},
|
|
12
|
-
right: { tree:
|
|
13
|
-
{ left: {leaf: "b"},
|
|
14
|
-
right: {leaf: "c"}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
> _log! height
|
package/error.k
DELETED
package/factorial.k
DELETED
package/fib.k
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
fib_aux = _log!
|
|
2
|
-
<
|
|
3
|
-
{ [.n, 1] GT [.0,-1] PLUS n,
|
|
4
|
-
."[fib(n),fib(n-1)" [[.0,.1] PLUS, .0] "[fib(n),fib(n-1)"
|
|
5
|
-
} fib_aux,
|
|
6
|
-
."[fib(n),fib(n-1)"
|
|
7
|
-
>
|
|
8
|
-
;
|
|
9
|
-
|
|
10
|
-
fib = {() n, [1,1] "[fib(n),fib(n-1)"}
|
|
11
|
-
fib_aux .0;
|
|
12
|
-
|
|
13
|
-
----------------------
|
|
14
|
-
|
|
15
|
-
fib
|
package/pow.k
DELETED
package/test.k
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
--------- comments start by #, --, or // ----------------------------------
|
|
2
|
-
< -- merge of 4 partial functions...
|
|
3
|
-
["x=", .x, " & y=", .y], -- produces a vector of 4 values, if fields 'x' and 'y' are present
|
|
4
|
-
["only x=", .x], -- produces a pair '["only x=", "value-of-x"]', for input like {"x":"value-of-x"}
|
|
5
|
-
-- it is defined only if field 'x' is present
|
|
6
|
-
["only y=", .y],
|
|
7
|
-
["no x nor y"] -- defined for all input, returns always the same one element vector
|
|
8
|
-
>
|
|
9
|
-
-- one of the string vectors is passed to the following partial function,
|
|
10
|
-
-- which produces a record (map) with one field "x&y", whose value is the
|
|
11
|
-
-- result of concatenating elements of the passed in vector
|
|
12
|
-
{ CONCAT "x&y" }
|
|
13
|
-
------------------------------------------------------------------------------
|
package/typed-fib.k
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
inc = [(),1] PLUS;
|
|
2
|
-
fib2 =
|
|
3
|
-
$ { int target, int x, int "fib(x)", int "fib(x-1)" }
|
|
4
|
-
_log!
|
|
5
|
-
<
|
|
6
|
-
{[.target,.x]GT.0 target ,
|
|
7
|
-
.x inc x ,
|
|
8
|
-
[."fib(x)",."fib(x-1)"]PLUS "fib(x)" ,
|
|
9
|
-
."fib(x)" "fib(x-1)" }
|
|
10
|
-
fib2,
|
|
11
|
-
|
|
12
|
-
{[.target,.x] EQ, ."fib(x)" result}
|
|
13
|
-
.result
|
|
14
|
-
>
|
|
15
|
-
$ int;
|
|
16
|
-
|
|
17
|
-
{() target, 0 x, 1 "fib(x)", 0 "fib(x-1)"} fib2
|
|
18
|
-
|
|
19
|
-
|