@fraczak/k 1.2.2 → 1.3.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/INTRO.md +131 -0
- package/README.md +17 -16
- package/codes.coffee +1 -32
- package/codes.js +2 -49
- package/package.json +1 -1
- package/parser.jison +4 -0
- package/parser.js +25 -25
- package/pretty.coffee +74 -0
- package/pretty.js +106 -0
- package/repl.coffee +14 -1
- package/repl.js +32 -6
- package/repl.sh +32 -6
- package/run.coffee +6 -8
- package/run.js +21 -19
- package/test.coffee +2 -0
- package/test.js +12 -0
- package/Benchmark/range1000 +0 -1000
- package/Benchmark/range200 +0 -200
package/repl.coffee
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env coffee
|
|
2
2
|
k = require "./"
|
|
3
3
|
run = require "./run"
|
|
4
|
+
{prettyCode, prettyRel} = require "./pretty"
|
|
4
5
|
|
|
5
6
|
console.log "Very! experimental repl shell for 'k-language'..."
|
|
6
7
|
|
|
@@ -8,6 +9,7 @@ help = ->
|
|
|
8
9
|
console.log " --h print help"
|
|
9
10
|
console.log " --a print codes and relations"
|
|
10
11
|
console.log " --c print codes"
|
|
12
|
+
console.log " --r print rels"
|
|
11
13
|
console.log " --l file.k load 'file.k'"
|
|
12
14
|
|
|
13
15
|
help()
|
|
@@ -35,8 +37,19 @@ do (val = {}, buffer = []) ->
|
|
|
35
37
|
console.log "=> #{JSON.stringify val}"
|
|
36
38
|
else if line.match /^[ \n\t]*(?:--a)?$/
|
|
37
39
|
console.log JSON.stringify run.defs, " ", 2
|
|
40
|
+
else if line.match /^[ \n\t]*(?:--r)?$/
|
|
41
|
+
console.log do (defs = run.defs, result = {}) ->
|
|
42
|
+
return result unless defs?
|
|
43
|
+
do (prettyRel = prettyRel.bind null, prettyCode.bind null, defs.codes, defs.representatives) ->
|
|
44
|
+
for relName, relExps of defs.rels
|
|
45
|
+
result[relName] = relExps.map prettyRel
|
|
46
|
+
result
|
|
38
47
|
else if line.match /^[ \n\t]*(?:--c)?$/
|
|
39
|
-
console.log
|
|
48
|
+
console.log do (defs = run.defs, result = {}) ->
|
|
49
|
+
return result unless defs?
|
|
50
|
+
for codeName, codeExp of defs.codes
|
|
51
|
+
result[codeName] = prettyCode defs.codes, defs.representatives, codeExp
|
|
52
|
+
result
|
|
40
53
|
else if not line.match /^[ \n\t]*(?:#.*)?$/
|
|
41
54
|
try
|
|
42
55
|
val = k.run "#{line} ()", val
|
package/repl.js
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
// Generated by CoffeeScript 2.7.0
|
|
2
2
|
(function() {
|
|
3
3
|
//!/usr/bin/env coffee
|
|
4
|
-
var help, k, re, run,
|
|
4
|
+
var help, k, prettyCode, prettyRel, re, run,
|
|
5
5
|
splice = [].splice;
|
|
6
6
|
|
|
7
7
|
k = require("./");
|
|
8
8
|
|
|
9
9
|
run = require("./run");
|
|
10
10
|
|
|
11
|
+
({prettyCode, prettyRel} = require("./pretty"));
|
|
12
|
+
|
|
11
13
|
console.log("Very! experimental repl shell for 'k-language'...");
|
|
12
14
|
|
|
13
15
|
help = function() {
|
|
14
16
|
console.log(" --h print help");
|
|
15
17
|
console.log(" --a print codes and relations");
|
|
16
18
|
console.log(" --c print codes");
|
|
19
|
+
console.log(" --r print rels");
|
|
17
20
|
return console.log(" --l file.k load 'file.k'");
|
|
18
21
|
};
|
|
19
22
|
|
|
@@ -23,7 +26,7 @@
|
|
|
23
26
|
|
|
24
27
|
(function(val, buffer) {
|
|
25
28
|
return process.stdin.on('data', function(data) {
|
|
26
|
-
var e, file, first, i, last, len, line, ref,
|
|
29
|
+
var e, file, first, i, last, len, line, ref, rest, results, todo;
|
|
27
30
|
[first, ...rest] = data.toString("utf8").split("\n");
|
|
28
31
|
buffer.push(first);
|
|
29
32
|
if (rest.length > 0) {
|
|
@@ -46,11 +49,34 @@
|
|
|
46
49
|
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
47
50
|
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
48
51
|
results.push(console.log(JSON.stringify(run.defs, " ", 2)));
|
|
52
|
+
} else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
|
|
53
|
+
results.push(console.log((function(defs, result) {
|
|
54
|
+
if (defs == null) {
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
return (function(prettyRel) {
|
|
58
|
+
var ref1, relExps, relName;
|
|
59
|
+
ref1 = defs.rels;
|
|
60
|
+
for (relName in ref1) {
|
|
61
|
+
relExps = ref1[relName];
|
|
62
|
+
result[relName] = relExps.map(prettyRel);
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
})(prettyRel.bind(null, prettyCode.bind(null, defs.codes, defs.representatives)));
|
|
66
|
+
})(run.defs, {})));
|
|
49
67
|
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
50
|
-
results.push(console.log(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
68
|
+
results.push(console.log((function(defs, result) {
|
|
69
|
+
var codeExp, codeName, ref1;
|
|
70
|
+
if (defs == null) {
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
ref1 = defs.codes;
|
|
74
|
+
for (codeName in ref1) {
|
|
75
|
+
codeExp = ref1[codeName];
|
|
76
|
+
result[codeName] = prettyCode(defs.codes, defs.representatives, codeExp);
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
})(run.defs, {})));
|
|
54
80
|
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
55
81
|
try {
|
|
56
82
|
val = k.run(`${line} ()`, val);
|
package/repl.sh
CHANGED
|
@@ -2,19 +2,22 @@
|
|
|
2
2
|
// Generated by CoffeeScript 2.7.0
|
|
3
3
|
(function() {
|
|
4
4
|
//!/usr/bin/env coffee
|
|
5
|
-
var help, k, re, run,
|
|
5
|
+
var help, k, prettyCode, prettyRel, re, run,
|
|
6
6
|
splice = [].splice;
|
|
7
7
|
|
|
8
8
|
k = require("./");
|
|
9
9
|
|
|
10
10
|
run = require("./run");
|
|
11
11
|
|
|
12
|
+
({prettyCode, prettyRel} = require("./pretty"));
|
|
13
|
+
|
|
12
14
|
console.log("Very! experimental repl shell for 'k-language'...");
|
|
13
15
|
|
|
14
16
|
help = function() {
|
|
15
17
|
console.log(" --h print help");
|
|
16
18
|
console.log(" --a print codes and relations");
|
|
17
19
|
console.log(" --c print codes");
|
|
20
|
+
console.log(" --r print rels");
|
|
18
21
|
return console.log(" --l file.k load 'file.k'");
|
|
19
22
|
};
|
|
20
23
|
|
|
@@ -24,7 +27,7 @@
|
|
|
24
27
|
|
|
25
28
|
(function(val, buffer) {
|
|
26
29
|
return process.stdin.on('data', function(data) {
|
|
27
|
-
var e, file, first, i, last, len, line, ref,
|
|
30
|
+
var e, file, first, i, last, len, line, ref, rest, results, todo;
|
|
28
31
|
[first, ...rest] = data.toString("utf8").split("\n");
|
|
29
32
|
buffer.push(first);
|
|
30
33
|
if (rest.length > 0) {
|
|
@@ -47,11 +50,34 @@
|
|
|
47
50
|
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
48
51
|
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
49
52
|
results.push(console.log(JSON.stringify(run.defs, " ", 2)));
|
|
53
|
+
} else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
|
|
54
|
+
results.push(console.log((function(defs, result) {
|
|
55
|
+
if (defs == null) {
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
return (function(prettyRel) {
|
|
59
|
+
var ref1, relExps, relName;
|
|
60
|
+
ref1 = defs.rels;
|
|
61
|
+
for (relName in ref1) {
|
|
62
|
+
relExps = ref1[relName];
|
|
63
|
+
result[relName] = relExps.map(prettyRel);
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
})(prettyRel.bind(null, prettyCode.bind(null, defs.codes, defs.representatives)));
|
|
67
|
+
})(run.defs, {})));
|
|
50
68
|
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
51
|
-
results.push(console.log(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
results.push(console.log((function(defs, result) {
|
|
70
|
+
var codeExp, codeName, ref1;
|
|
71
|
+
if (defs == null) {
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
ref1 = defs.codes;
|
|
75
|
+
for (codeName in ref1) {
|
|
76
|
+
codeExp = ref1[codeName];
|
|
77
|
+
result[codeName] = prettyCode(defs.codes, defs.representatives, codeExp);
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
})(run.defs, {})));
|
|
55
81
|
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
56
82
|
try {
|
|
57
83
|
val = k.run(`${line} ()`, val);
|
package/run.coffee
CHANGED
|
@@ -8,11 +8,6 @@ builtin =
|
|
|
8
8
|
"_log!": (arg) ->
|
|
9
9
|
console.error "_log!: #{JSON.stringify arg}"
|
|
10
10
|
arg
|
|
11
|
-
"fromFILE": (fileName) ->
|
|
12
|
-
fs.readFileSync(fileName).toString()
|
|
13
|
-
"toFILE": ([x,fileName]) ->
|
|
14
|
-
fs.writeFileSync fileName, x
|
|
15
|
-
return x
|
|
16
11
|
"GT": (args) ->
|
|
17
12
|
[ok,_] = do ([last,args...] = args) ->
|
|
18
13
|
args.reduce ([ok,last], x) ->
|
|
@@ -36,6 +31,7 @@ builtin =
|
|
|
36
31
|
div = x // y
|
|
37
32
|
rem = x %% y
|
|
38
33
|
return {div,rem} if x is div * y + rem
|
|
34
|
+
"FDIV": ([x,y]) -> x / y
|
|
39
35
|
"CONCAT": (strs) -> strs.join('')
|
|
40
36
|
"true": -> true
|
|
41
37
|
"false": -> false
|
|
@@ -44,7 +40,9 @@ builtin =
|
|
|
44
40
|
"fromJSON": (x) -> JSON.parse x
|
|
45
41
|
"CONS": ([x,y]) -> [x,y...]
|
|
46
42
|
"SNOC": (x) ->
|
|
47
|
-
return [x[0],x.slice 1] if x.length > 1
|
|
43
|
+
return [x[0],x.slice 1] if x.length > 1
|
|
44
|
+
"toDateMsec": (x) -> new Date(x).getTime()
|
|
45
|
+
"toDateStr": (x) -> new Date(x).toISOString()
|
|
48
46
|
|
|
49
47
|
codes =
|
|
50
48
|
"int": (x) ->
|
|
@@ -92,8 +90,8 @@ run = (exp, value) ->
|
|
|
92
90
|
when "str", "int"
|
|
93
91
|
return exp[exp.op]
|
|
94
92
|
when "ref"
|
|
95
|
-
defn = run.defs.rels[exp.ref]
|
|
96
|
-
return run defn, value if defn?
|
|
93
|
+
defn = run.defs.rels[exp.ref]
|
|
94
|
+
return run defn[defn.length - 1], value if defn?
|
|
97
95
|
return builtin[exp.ref] value
|
|
98
96
|
when "dot"
|
|
99
97
|
return value[exp.dot]
|
package/run.js
CHANGED
|
@@ -19,13 +19,6 @@
|
|
|
19
19
|
console.error(`_log!: ${JSON.stringify(arg)}`);
|
|
20
20
|
return arg;
|
|
21
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
|
-
},
|
|
29
22
|
"GT": function(args) {
|
|
30
23
|
var _, ok;
|
|
31
24
|
[ok, _] = (function([last, ...args]) {
|
|
@@ -79,6 +72,9 @@
|
|
|
79
72
|
return {div, rem};
|
|
80
73
|
}
|
|
81
74
|
},
|
|
75
|
+
"FDIV": function([x, y]) {
|
|
76
|
+
return x / y;
|
|
77
|
+
},
|
|
82
78
|
"CONCAT": function(strs) {
|
|
83
79
|
return strs.join('');
|
|
84
80
|
},
|
|
@@ -104,6 +100,12 @@
|
|
|
104
100
|
if (x.length > 1) {
|
|
105
101
|
return [x[0], x.slice(1)];
|
|
106
102
|
}
|
|
103
|
+
},
|
|
104
|
+
"toDateMsec": function(x) {
|
|
105
|
+
return new Date(x).getTime();
|
|
106
|
+
},
|
|
107
|
+
"toDateStr": function(x) {
|
|
108
|
+
return new Date(x).toISOString();
|
|
107
109
|
}
|
|
108
110
|
};
|
|
109
111
|
|
|
@@ -168,7 +170,7 @@
|
|
|
168
170
|
|
|
169
171
|
run = function(exp, value) {
|
|
170
172
|
"use strict";
|
|
171
|
-
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2,
|
|
173
|
+
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2, result;
|
|
172
174
|
if (value === void 0) {
|
|
173
175
|
// console.log {exp,value}
|
|
174
176
|
return void 0;
|
|
@@ -185,9 +187,9 @@
|
|
|
185
187
|
case "int":
|
|
186
188
|
return exp[exp.op];
|
|
187
189
|
case "ref":
|
|
188
|
-
defn =
|
|
190
|
+
defn = run.defs.rels[exp.ref];
|
|
189
191
|
if (defn != null) {
|
|
190
|
-
return run(defn, value);
|
|
192
|
+
return run(defn[defn.length - 1], value);
|
|
191
193
|
}
|
|
192
194
|
return builtin[exp.ref](value);
|
|
193
195
|
case "dot":
|
|
@@ -199,9 +201,9 @@
|
|
|
199
201
|
}
|
|
200
202
|
}, value);
|
|
201
203
|
case "union":
|
|
202
|
-
|
|
203
|
-
for (i = 0, len =
|
|
204
|
-
e =
|
|
204
|
+
ref = exp.union;
|
|
205
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
|
206
|
+
e = ref[i];
|
|
205
207
|
result = run(e, value);
|
|
206
208
|
if (result !== void 0) {
|
|
207
209
|
return result;
|
|
@@ -210,9 +212,9 @@
|
|
|
210
212
|
return void 0;
|
|
211
213
|
case "vector":
|
|
212
214
|
result = [];
|
|
213
|
-
|
|
214
|
-
for (j = 0, len1 =
|
|
215
|
-
e =
|
|
215
|
+
ref1 = exp.vector;
|
|
216
|
+
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
|
217
|
+
e = ref1[j];
|
|
216
218
|
r = run(e, value);
|
|
217
219
|
if (r === void 0) {
|
|
218
220
|
return;
|
|
@@ -222,9 +224,9 @@
|
|
|
222
224
|
return result;
|
|
223
225
|
case "product":
|
|
224
226
|
result = {};
|
|
225
|
-
|
|
226
|
-
for (k = 0, len2 =
|
|
227
|
-
({label, exp} =
|
|
227
|
+
ref2 = exp.product;
|
|
228
|
+
for (k = 0, len2 = ref2.length; k < len2; k++) {
|
|
229
|
+
({label, exp} = ref2[k]);
|
|
228
230
|
r = run(exp, value);
|
|
229
231
|
if (r === void 0) {
|
|
230
232
|
return;
|
package/test.coffee
CHANGED
|
@@ -58,6 +58,8 @@ console.log ""
|
|
|
58
58
|
{None: "None"}
|
|
59
59
|
{list: {ints: []}}
|
|
60
60
|
{list: {ints: [1,2,3]}}
|
|
61
|
+
{list: {bools: [true, false]}}
|
|
62
|
+
{list: {bools: [true, 0]}}
|
|
61
63
|
].map (data) ->
|
|
62
64
|
console.log "k_fn(#{JSON.stringify(data)});"
|
|
63
65
|
console.log "// RETURNS: #{JSON.stringify k_fn data}"
|
package/test.js
CHANGED