@fraczak/k 1.2.1 → 1.2.3
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/README.md +10 -10
- package/codes.coffee +1 -32
- package/codes.js +2 -49
- package/package.json +5 -1
- package/pretty.coffee +74 -0
- package/pretty.js +106 -0
- package/repl.coffee +38 -1
- package/repl.js +58 -3
- package/repl.sh +58 -3
- package/run.coffee +3 -3
- package/run.js +13 -13
- package/test.coffee +2 -0
- package/test.js +12 -0
- package/Benchmark/range1000 +0 -1000
- package/Benchmark/range200 +0 -200
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# k-language
|
|
2
2
|
|
|
3
|
-
npm install @fraczak/k
|
|
3
|
+
npm install '@fraczak/k'
|
|
4
4
|
|
|
5
5
|
From javascript:
|
|
6
6
|
|
|
@@ -84,8 +84,8 @@ Another example could be finding the biggest (max) value in a vector:
|
|
|
84
84
|
max = <
|
|
85
85
|
SNOC -- [x0, x1, x2, ...] -> [x0, [x1, x2, ...]]
|
|
86
86
|
[.0, .1 max] -- [x0, [x1, x2, ...]] -> [x0, max(x1,x2,...)], i.e., recursive call
|
|
87
|
-
<GT.0
|
|
88
|
-
|
|
87
|
+
<GT .0, .1>, -- if x0 > max(x1,x2,...) then x0 else max(x1,x2,...)
|
|
88
|
+
-- when SNOC is not defined, e.g. the argument is a singleton vector [x0]
|
|
89
89
|
.0 -- [x0] -> x0,
|
|
90
90
|
>;
|
|
91
91
|
max
|
|
@@ -102,7 +102,7 @@ There are three predefined value encodings: `int`, `string`, and
|
|
|
102
102
|
|
|
103
103
|
One can define recursive codes. E.g.:
|
|
104
104
|
|
|
105
|
-
tree = <string leaf, {tree left, tree right} tree>;
|
|
105
|
+
$tree = <string leaf, {tree left, tree right} tree>;
|
|
106
106
|
|
|
107
107
|
Each _code_ definition starts with a `$`.
|
|
108
108
|
The above example defines new code called _tree_.
|
|
@@ -143,8 +143,8 @@ run the language from command line.
|
|
|
143
143
|
> ./node_modules/.bin/k '<["x=",.x," & y=",.y],["only x=",.x],["only y=",.y],["no x nor y"]>{CONCAT "x&y"}'
|
|
144
144
|
|
|
145
145
|
{"y": 123, "x": 432,"others": "..."} --> {"x&y":"x=432 & y=123"}
|
|
146
|
-
{"x":987}
|
|
147
|
-
{"z":123}
|
|
146
|
+
{"x": 987} --> {"x&y":"only x=987"}
|
|
147
|
+
{"z": 123} --> {"x&y":"no x nor y"}
|
|
148
148
|
^D - to interrupt
|
|
149
149
|
|
|
150
150
|
3. If the `k`-expression is long, it can be put in a file, e.g.:
|
|
@@ -172,7 +172,7 @@ run the language from command line.
|
|
|
172
172
|
|
|
173
173
|
> ./node_modules/.bin/k -k test.k my-objects.json
|
|
174
174
|
{"x&y":"x=432 & y=123"}
|
|
175
|
-
{"x&y":"
|
|
175
|
+
{"x&y":"only x=987"}
|
|
176
176
|
{"x&y":"no x nor y"}
|
|
177
177
|
|
|
178
178
|
where:
|
|
@@ -181,8 +181,8 @@ run the language from command line.
|
|
|
181
181
|
####################################################
|
|
182
182
|
# empty lines and lines starting with # are ignored
|
|
183
183
|
{"y": 123, "x": 432,"others": "..."}
|
|
184
|
-
{"x":987}
|
|
185
|
-
{"z":123}
|
|
184
|
+
{"x": 987}
|
|
185
|
+
{"z": 123}
|
|
186
186
|
####################################################
|
|
187
187
|
|
|
188
188
|
### k-REPL (Read-Evaluate-Print Loop)
|
|
@@ -194,7 +194,7 @@ shell for the language. E.g.:
|
|
|
194
194
|
{'a' a, 'b' b} toJSON
|
|
195
195
|
=> "{\"a\":\"a\",\"b\":\"b\"}"
|
|
196
196
|
{"a" a} toJSON fromJSON
|
|
197
|
-
=> {"a":"a"}
|
|
197
|
+
=> {"a": "a"}
|
|
198
198
|
inc = [(),1] PLUS; 1 inc inc
|
|
199
199
|
=> 3
|
|
200
200
|
inc inc inc inc
|
package/codes.coffee
CHANGED
|
@@ -53,37 +53,6 @@ minimize = (codes) ->
|
|
|
53
53
|
representatives[x] = new_rep
|
|
54
54
|
{classes,representatives}
|
|
55
55
|
|
|
56
|
-
compareAs = (fn) ->
|
|
57
|
-
(a,b) ->
|
|
58
|
-
[a,b] = [a,b].map fn
|
|
59
|
-
return -1 if a < b
|
|
60
|
-
return 1 if a > b
|
|
61
|
-
0
|
|
62
|
-
|
|
63
|
-
pretty_labels = (label_ref_map, codes, representatives) ->
|
|
64
|
-
labels = Object.keys(label_ref_map)
|
|
65
|
-
.sort compareAs (x) -> x
|
|
66
|
-
.map (label) ->
|
|
67
|
-
"#{pretty {code:"ref",ref:label_ref_map[label]}, codes, representatives} #{label}"
|
|
68
|
-
.join ", "
|
|
69
|
-
|
|
70
|
-
pretty = (codeExp, codes, representatives) ->
|
|
71
|
-
switch codeExp.code
|
|
72
|
-
when "ref"
|
|
73
|
-
name = representatives[codeExp.ref] ? codeExp.ref
|
|
74
|
-
if name.startsWith ":"
|
|
75
|
-
pretty codes[name], codes, representatives
|
|
76
|
-
else
|
|
77
|
-
name
|
|
78
|
-
when "product"
|
|
79
|
-
"{#{pretty_labels codeExp.product, codes, representatives}}"
|
|
80
|
-
when "union"
|
|
81
|
-
"<#{pretty_labels codeExp.union, codes, representatives}>"
|
|
82
|
-
when "vector"
|
|
83
|
-
"[#{representatives[codeExp.ref] ? codeExp.ref}]"
|
|
84
|
-
else
|
|
85
|
-
":error"
|
|
86
|
-
|
|
87
56
|
normalize = (label_ref_map, representatives) ->
|
|
88
57
|
# console.log {label_ref_map, representatives}
|
|
89
58
|
Object.keys(label_ref_map).reduce (result, label) ->
|
|
@@ -107,6 +76,6 @@ normalizeAll = (codes,representatives) ->
|
|
|
107
76
|
normalized
|
|
108
77
|
, {}
|
|
109
78
|
|
|
110
|
-
module.exports = {minimize,
|
|
79
|
+
module.exports = {minimize,normalize,normalizeAll}
|
|
111
80
|
|
|
112
81
|
|
package/codes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Generated by CoffeeScript 2.7.0
|
|
2
2
|
(function() {
|
|
3
|
-
var are_different,
|
|
3
|
+
var are_different, minimize, normalize, normalizeAll;
|
|
4
4
|
|
|
5
5
|
are_different = function(classes, representatives, name1, name2, codes) {
|
|
6
6
|
var arg1, arg2, code1, code2, field, fields1, fields2, rep;
|
|
@@ -92,53 +92,6 @@
|
|
|
92
92
|
return {classes, representatives};
|
|
93
93
|
};
|
|
94
94
|
|
|
95
|
-
compareAs = function(fn) {
|
|
96
|
-
return function(a, b) {
|
|
97
|
-
[a, b] = [a, b].map(fn);
|
|
98
|
-
if (a < b) {
|
|
99
|
-
return -1;
|
|
100
|
-
}
|
|
101
|
-
if (a > b) {
|
|
102
|
-
return 1;
|
|
103
|
-
}
|
|
104
|
-
return 0;
|
|
105
|
-
};
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
pretty_labels = function(label_ref_map, codes, representatives) {
|
|
109
|
-
var labels;
|
|
110
|
-
return labels = Object.keys(label_ref_map).sort(compareAs(function(x) {
|
|
111
|
-
return x;
|
|
112
|
-
})).map(function(label) {
|
|
113
|
-
return `${pretty({
|
|
114
|
-
code: "ref",
|
|
115
|
-
ref: label_ref_map[label]
|
|
116
|
-
}, codes, representatives)} ${label}`;
|
|
117
|
-
}).join(", ");
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
pretty = function(codeExp, codes, representatives) {
|
|
121
|
-
var name, ref, ref1;
|
|
122
|
-
switch (codeExp.code) {
|
|
123
|
-
case "ref":
|
|
124
|
-
name = (ref = representatives[codeExp.ref]) != null ? ref : codeExp.ref;
|
|
125
|
-
if (name.startsWith(":")) {
|
|
126
|
-
return pretty(codes[name], codes, representatives);
|
|
127
|
-
} else {
|
|
128
|
-
return name;
|
|
129
|
-
}
|
|
130
|
-
break;
|
|
131
|
-
case "product":
|
|
132
|
-
return `{${pretty_labels(codeExp.product, codes, representatives)}}`;
|
|
133
|
-
case "union":
|
|
134
|
-
return `<${pretty_labels(codeExp.union, codes, representatives)}>`;
|
|
135
|
-
case "vector":
|
|
136
|
-
return `[${(ref1 = representatives[codeExp.ref]) != null ? ref1 : codeExp.ref}]`;
|
|
137
|
-
default:
|
|
138
|
-
return ":error";
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
95
|
normalize = function(label_ref_map, representatives) {
|
|
143
96
|
// console.log {label_ref_map, representatives}
|
|
144
97
|
return Object.keys(label_ref_map).reduce(function(result, label) {
|
|
@@ -176,6 +129,6 @@
|
|
|
176
129
|
}, {});
|
|
177
130
|
};
|
|
178
131
|
|
|
179
|
-
module.exports = {minimize,
|
|
132
|
+
module.exports = {minimize, normalize, normalizeAll};
|
|
180
133
|
|
|
181
134
|
}).call(this);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fraczak/k",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "k-language for JSON-like data transformation",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
"all": "coffee -c *.coffee; npm run prepare; npm run prepublishOnly",
|
|
10
10
|
"test": "coffee -c symbol-table.coffee; jison parser.jison; coffee test.coffee"
|
|
11
11
|
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/fraczak/k.git"
|
|
15
|
+
},
|
|
12
16
|
"author": "Wojciech Fraczak",
|
|
13
17
|
"license": "ISC",
|
|
14
18
|
"bin": {
|
package/pretty.coffee
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
|
|
2
|
+
compareAs = (fn) ->
|
|
3
|
+
(a,b) ->
|
|
4
|
+
[a,b] = [a,b].map fn
|
|
5
|
+
return -1 if a < b
|
|
6
|
+
return 1 if a > b
|
|
7
|
+
0
|
|
8
|
+
|
|
9
|
+
nameRE = /^[a-zA-Z_][a-zA-Z0-9_?!]*$/
|
|
10
|
+
prettyCode_labels = ( codes, representatives, label_ref_map) ->
|
|
11
|
+
labels = Object.keys(label_ref_map)
|
|
12
|
+
.sort compareAs (x) -> x
|
|
13
|
+
.map (label) ->
|
|
14
|
+
plabel = if /^[a-zA-Z_][a-zA-Z0-9_?!]*$/.test label
|
|
15
|
+
label
|
|
16
|
+
else
|
|
17
|
+
"'#{label}'"
|
|
18
|
+
"#{prettyCode codes, representatives, {code:"ref",ref:label_ref_map[label]}} #{plabel}"
|
|
19
|
+
.join ", "
|
|
20
|
+
|
|
21
|
+
prettyCode = (codes, representatives, codeExp) ->
|
|
22
|
+
switch codeExp.code
|
|
23
|
+
when "ref"
|
|
24
|
+
name = representatives[codeExp.ref] ? codeExp.ref
|
|
25
|
+
if name.startsWith ":"
|
|
26
|
+
prettyCode codes, representatives, codes[name]
|
|
27
|
+
else
|
|
28
|
+
name
|
|
29
|
+
when "product"
|
|
30
|
+
"{#{prettyCode_labels codes, representatives, codeExp.product}}"
|
|
31
|
+
when "union"
|
|
32
|
+
"<#{prettyCode_labels codes, representatives, codeExp.union}>"
|
|
33
|
+
when "vector"
|
|
34
|
+
"[#{representatives[codeExp.vector] ? codeExp.vector}]"
|
|
35
|
+
else
|
|
36
|
+
":error"
|
|
37
|
+
|
|
38
|
+
prettyRel = (prettyCode, exp) ->
|
|
39
|
+
"use strict"
|
|
40
|
+
pretty = (exp) ->
|
|
41
|
+
switch exp.op
|
|
42
|
+
when 'vector'
|
|
43
|
+
"[#{exp.vector.map(pretty).join(", ")}]"
|
|
44
|
+
when 'union'
|
|
45
|
+
"<#{exp.union.map(pretty).join(", ")}>"
|
|
46
|
+
when 'ref'
|
|
47
|
+
exp.ref
|
|
48
|
+
when 'identity'
|
|
49
|
+
"()"
|
|
50
|
+
when 'comp'
|
|
51
|
+
exp.comp.map(pretty).join(" ")
|
|
52
|
+
when 'str'
|
|
53
|
+
"'#{exp.str}'"
|
|
54
|
+
when 'int'
|
|
55
|
+
exp.int
|
|
56
|
+
when 'dot'
|
|
57
|
+
if 'number' is typeof exp.dot
|
|
58
|
+
".#{exp.dot}"
|
|
59
|
+
else if nameRE.test exp.dot
|
|
60
|
+
".#{exp.dot}"
|
|
61
|
+
else
|
|
62
|
+
"'#{exp.dot}'"
|
|
63
|
+
when 'code'
|
|
64
|
+
"$#{prettyCode {code:"ref",ref:exp.code}}"
|
|
65
|
+
when 'product'
|
|
66
|
+
do (labelled = exp.product.map ({label,exp}) ->
|
|
67
|
+
if /^[a-zA-Z_][a-zA-Z0-9_?!]*$/.test label
|
|
68
|
+
"#{pretty exp} #{label}"
|
|
69
|
+
else
|
|
70
|
+
"#{pretty exp} '#{label}'") ->
|
|
71
|
+
"{#{labelled.join(", ")}}"
|
|
72
|
+
pretty exp
|
|
73
|
+
|
|
74
|
+
module.exports = { prettyCode, prettyRel }
|
package/pretty.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Generated by CoffeeScript 2.7.0
|
|
2
|
+
(function() {
|
|
3
|
+
var compareAs, nameRE, prettyCode, prettyCode_labels, prettyRel;
|
|
4
|
+
|
|
5
|
+
compareAs = function(fn) {
|
|
6
|
+
return function(a, b) {
|
|
7
|
+
[a, b] = [a, b].map(fn);
|
|
8
|
+
if (a < b) {
|
|
9
|
+
return -1;
|
|
10
|
+
}
|
|
11
|
+
if (a > b) {
|
|
12
|
+
return 1;
|
|
13
|
+
}
|
|
14
|
+
return 0;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
nameRE = /^[a-zA-Z_][a-zA-Z0-9_?!]*$/;
|
|
19
|
+
|
|
20
|
+
prettyCode_labels = function(codes, representatives, label_ref_map) {
|
|
21
|
+
var labels;
|
|
22
|
+
return labels = Object.keys(label_ref_map).sort(compareAs(function(x) {
|
|
23
|
+
return x;
|
|
24
|
+
})).map(function(label) {
|
|
25
|
+
var plabel;
|
|
26
|
+
plabel = /^[a-zA-Z_][a-zA-Z0-9_?!]*$/.test(label) ? label : `'${label}'`;
|
|
27
|
+
return `${prettyCode(codes, representatives, {
|
|
28
|
+
code: "ref",
|
|
29
|
+
ref: label_ref_map[label]
|
|
30
|
+
})} ${plabel}`;
|
|
31
|
+
}).join(", ");
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
prettyCode = function(codes, representatives, codeExp) {
|
|
35
|
+
var name, ref, ref1;
|
|
36
|
+
switch (codeExp.code) {
|
|
37
|
+
case "ref":
|
|
38
|
+
name = (ref = representatives[codeExp.ref]) != null ? ref : codeExp.ref;
|
|
39
|
+
if (name.startsWith(":")) {
|
|
40
|
+
return prettyCode(codes, representatives, codes[name]);
|
|
41
|
+
} else {
|
|
42
|
+
return name;
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
case "product":
|
|
46
|
+
return `{${prettyCode_labels(codes, representatives, codeExp.product)}}`;
|
|
47
|
+
case "union":
|
|
48
|
+
return `<${prettyCode_labels(codes, representatives, codeExp.union)}>`;
|
|
49
|
+
case "vector":
|
|
50
|
+
return `[${(ref1 = representatives[codeExp.vector]) != null ? ref1 : codeExp.vector}]`;
|
|
51
|
+
default:
|
|
52
|
+
return ":error";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
prettyRel = function(prettyCode, exp) {
|
|
57
|
+
"use strict";
|
|
58
|
+
var pretty;
|
|
59
|
+
pretty = function(exp) {
|
|
60
|
+
switch (exp.op) {
|
|
61
|
+
case 'vector':
|
|
62
|
+
return `[${exp.vector.map(pretty).join(", ")}]`;
|
|
63
|
+
case 'union':
|
|
64
|
+
return `<${exp.union.map(pretty).join(", ")}>`;
|
|
65
|
+
case 'ref':
|
|
66
|
+
return exp.ref;
|
|
67
|
+
case 'identity':
|
|
68
|
+
return "()";
|
|
69
|
+
case 'comp':
|
|
70
|
+
return exp.comp.map(pretty).join(" ");
|
|
71
|
+
case 'str':
|
|
72
|
+
return `'${exp.str}'`;
|
|
73
|
+
case 'int':
|
|
74
|
+
return exp.int;
|
|
75
|
+
case 'dot':
|
|
76
|
+
if ('number' === typeof exp.dot) {
|
|
77
|
+
return `.${exp.dot}`;
|
|
78
|
+
} else if (nameRE.test(exp.dot)) {
|
|
79
|
+
return `.${exp.dot}`;
|
|
80
|
+
} else {
|
|
81
|
+
return `'${exp.dot}'`;
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
case 'code':
|
|
85
|
+
return `$${prettyCode({
|
|
86
|
+
code: "ref",
|
|
87
|
+
ref: exp.code
|
|
88
|
+
})}`;
|
|
89
|
+
case 'product':
|
|
90
|
+
return (function(labelled) {
|
|
91
|
+
return `{${labelled.join(", ")}}`;
|
|
92
|
+
})(exp.product.map(function({label, exp}) {
|
|
93
|
+
if (/^[a-zA-Z_][a-zA-Z0-9_?!]*$/.test(label)) {
|
|
94
|
+
return `${pretty(exp)} ${label}`;
|
|
95
|
+
} else {
|
|
96
|
+
return `${pretty(exp)} '${label}'`;
|
|
97
|
+
}
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
return pretty(exp);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
module.exports = {prettyCode, prettyRel};
|
|
105
|
+
|
|
106
|
+
}).call(this);
|
package/repl.coffee
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env coffee
|
|
2
2
|
k = require "./"
|
|
3
|
+
run = require "./run"
|
|
4
|
+
{prettyCode, prettyRel} = require "./pretty"
|
|
5
|
+
|
|
6
|
+
console.log "Very! experimental repl shell for 'k-language'..."
|
|
7
|
+
|
|
8
|
+
help = ->
|
|
9
|
+
console.log " --h print help"
|
|
10
|
+
console.log " --a print codes and relations"
|
|
11
|
+
console.log " --c print codes"
|
|
12
|
+
console.log " --r print rels"
|
|
13
|
+
console.log " --l file.k load 'file.k'"
|
|
14
|
+
|
|
15
|
+
help()
|
|
16
|
+
|
|
17
|
+
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/
|
|
3
18
|
|
|
4
19
|
do (val = {}, buffer = []) ->
|
|
5
20
|
process.stdin.on 'data', (data) ->
|
|
@@ -13,7 +28,29 @@ do (val = {}, buffer = []) ->
|
|
|
13
28
|
for line in todo
|
|
14
29
|
if val is undefined
|
|
15
30
|
val = {}
|
|
16
|
-
if
|
|
31
|
+
if line.match /^[ \n\t]*(?:--h)?$/
|
|
32
|
+
help()
|
|
33
|
+
else if line.match re
|
|
34
|
+
file = line.match(re)[1]
|
|
35
|
+
console.log "-- loading file: #{file} ..."
|
|
36
|
+
val = k.compile_file(file) val
|
|
37
|
+
console.log "=> #{JSON.stringify val}"
|
|
38
|
+
else if line.match /^[ \n\t]*(?:--a)?$/
|
|
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
|
|
47
|
+
else if line.match /^[ \n\t]*(?:--c)?$/
|
|
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
|
|
53
|
+
else if not line.match /^[ \n\t]*(?:#.*)?$/
|
|
17
54
|
try
|
|
18
55
|
val = k.run "#{line} ()", val
|
|
19
56
|
console.log "=> #{JSON.stringify val}"
|
package/repl.js
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
// Generated by CoffeeScript 2.7.0
|
|
2
2
|
(function() {
|
|
3
3
|
//!/usr/bin/env coffee
|
|
4
|
-
var k,
|
|
4
|
+
var help, k, prettyCode, prettyRel, re, run,
|
|
5
5
|
splice = [].splice;
|
|
6
6
|
|
|
7
7
|
k = require("./");
|
|
8
8
|
|
|
9
|
+
run = require("./run");
|
|
10
|
+
|
|
11
|
+
({prettyCode, prettyRel} = require("./pretty"));
|
|
12
|
+
|
|
13
|
+
console.log("Very! experimental repl shell for 'k-language'...");
|
|
14
|
+
|
|
15
|
+
help = function() {
|
|
16
|
+
console.log(" --h print help");
|
|
17
|
+
console.log(" --a print codes and relations");
|
|
18
|
+
console.log(" --c print codes");
|
|
19
|
+
console.log(" --r print rels");
|
|
20
|
+
return console.log(" --l file.k load 'file.k'");
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
help();
|
|
24
|
+
|
|
25
|
+
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
|
|
26
|
+
|
|
9
27
|
(function(val, buffer) {
|
|
10
28
|
return process.stdin.on('data', function(data) {
|
|
11
|
-
var e, first, i, last, len, line, ref, rest, results, todo;
|
|
29
|
+
var e, file, first, i, last, len, line, ref, rest, results, todo;
|
|
12
30
|
[first, ...rest] = data.toString("utf8").split("\n");
|
|
13
31
|
buffer.push(first);
|
|
14
32
|
if (rest.length > 0) {
|
|
@@ -22,7 +40,44 @@
|
|
|
22
40
|
if (val === void 0) {
|
|
23
41
|
val = {};
|
|
24
42
|
}
|
|
25
|
-
if (
|
|
43
|
+
if (line.match(/^[ \n\t]*(?:--h)?$/)) {
|
|
44
|
+
results.push(help());
|
|
45
|
+
} else if (line.match(re)) {
|
|
46
|
+
file = line.match(re)[1];
|
|
47
|
+
console.log(`-- loading file: ${file} ...`);
|
|
48
|
+
val = k.compile_file(file)(val);
|
|
49
|
+
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
50
|
+
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
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, {})));
|
|
67
|
+
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
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, {})));
|
|
80
|
+
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
26
81
|
try {
|
|
27
82
|
val = k.run(`${line} ()`, val);
|
|
28
83
|
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
package/repl.sh
CHANGED
|
@@ -2,14 +2,32 @@
|
|
|
2
2
|
// Generated by CoffeeScript 2.7.0
|
|
3
3
|
(function() {
|
|
4
4
|
//!/usr/bin/env coffee
|
|
5
|
-
var k,
|
|
5
|
+
var help, k, prettyCode, prettyRel, re, run,
|
|
6
6
|
splice = [].splice;
|
|
7
7
|
|
|
8
8
|
k = require("./");
|
|
9
9
|
|
|
10
|
+
run = require("./run");
|
|
11
|
+
|
|
12
|
+
({prettyCode, prettyRel} = require("./pretty"));
|
|
13
|
+
|
|
14
|
+
console.log("Very! experimental repl shell for 'k-language'...");
|
|
15
|
+
|
|
16
|
+
help = function() {
|
|
17
|
+
console.log(" --h print help");
|
|
18
|
+
console.log(" --a print codes and relations");
|
|
19
|
+
console.log(" --c print codes");
|
|
20
|
+
console.log(" --r print rels");
|
|
21
|
+
return console.log(" --l file.k load 'file.k'");
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
help();
|
|
25
|
+
|
|
26
|
+
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
|
|
27
|
+
|
|
10
28
|
(function(val, buffer) {
|
|
11
29
|
return process.stdin.on('data', function(data) {
|
|
12
|
-
var e, first, i, last, len, line, ref, rest, results, todo;
|
|
30
|
+
var e, file, first, i, last, len, line, ref, rest, results, todo;
|
|
13
31
|
[first, ...rest] = data.toString("utf8").split("\n");
|
|
14
32
|
buffer.push(first);
|
|
15
33
|
if (rest.length > 0) {
|
|
@@ -23,7 +41,44 @@
|
|
|
23
41
|
if (val === void 0) {
|
|
24
42
|
val = {};
|
|
25
43
|
}
|
|
26
|
-
if (
|
|
44
|
+
if (line.match(/^[ \n\t]*(?:--h)?$/)) {
|
|
45
|
+
results.push(help());
|
|
46
|
+
} else if (line.match(re)) {
|
|
47
|
+
file = line.match(re)[1];
|
|
48
|
+
console.log(`-- loading file: ${file} ...`);
|
|
49
|
+
val = k.compile_file(file)(val);
|
|
50
|
+
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
51
|
+
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
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, {})));
|
|
68
|
+
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
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, {})));
|
|
81
|
+
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
27
82
|
try {
|
|
28
83
|
val = k.run(`${line} ()`, val);
|
|
29
84
|
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
package/run.coffee
CHANGED
|
@@ -6,7 +6,7 @@ valid = (x) ->
|
|
|
6
6
|
|
|
7
7
|
builtin =
|
|
8
8
|
"_log!": (arg) ->
|
|
9
|
-
console.
|
|
9
|
+
console.error "_log!: #{JSON.stringify arg}"
|
|
10
10
|
arg
|
|
11
11
|
"fromFILE": (fileName) ->
|
|
12
12
|
fs.readFileSync(fileName).toString()
|
|
@@ -92,8 +92,8 @@ run = (exp, value) ->
|
|
|
92
92
|
when "str", "int"
|
|
93
93
|
return exp[exp.op]
|
|
94
94
|
when "ref"
|
|
95
|
-
defn = run.defs.rels[exp.ref]
|
|
96
|
-
return run defn, value if defn?
|
|
95
|
+
defn = run.defs.rels[exp.ref]
|
|
96
|
+
return run defn[defn.length - 1], value if defn?
|
|
97
97
|
return builtin[exp.ref] value
|
|
98
98
|
when "dot"
|
|
99
99
|
return value[exp.dot]
|
package/run.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
builtin = {
|
|
18
18
|
"_log!": function(arg) {
|
|
19
|
-
console.
|
|
19
|
+
console.error(`_log!: ${JSON.stringify(arg)}`);
|
|
20
20
|
return arg;
|
|
21
21
|
},
|
|
22
22
|
"fromFILE": function(fileName) {
|
|
@@ -168,7 +168,7 @@
|
|
|
168
168
|
|
|
169
169
|
run = function(exp, value) {
|
|
170
170
|
"use strict";
|
|
171
|
-
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2,
|
|
171
|
+
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2, result;
|
|
172
172
|
if (value === void 0) {
|
|
173
173
|
// console.log {exp,value}
|
|
174
174
|
return void 0;
|
|
@@ -185,9 +185,9 @@
|
|
|
185
185
|
case "int":
|
|
186
186
|
return exp[exp.op];
|
|
187
187
|
case "ref":
|
|
188
|
-
defn =
|
|
188
|
+
defn = run.defs.rels[exp.ref];
|
|
189
189
|
if (defn != null) {
|
|
190
|
-
return run(defn, value);
|
|
190
|
+
return run(defn[defn.length - 1], value);
|
|
191
191
|
}
|
|
192
192
|
return builtin[exp.ref](value);
|
|
193
193
|
case "dot":
|
|
@@ -199,9 +199,9 @@
|
|
|
199
199
|
}
|
|
200
200
|
}, value);
|
|
201
201
|
case "union":
|
|
202
|
-
|
|
203
|
-
for (i = 0, len =
|
|
204
|
-
e =
|
|
202
|
+
ref = exp.union;
|
|
203
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
|
204
|
+
e = ref[i];
|
|
205
205
|
result = run(e, value);
|
|
206
206
|
if (result !== void 0) {
|
|
207
207
|
return result;
|
|
@@ -210,9 +210,9 @@
|
|
|
210
210
|
return void 0;
|
|
211
211
|
case "vector":
|
|
212
212
|
result = [];
|
|
213
|
-
|
|
214
|
-
for (j = 0, len1 =
|
|
215
|
-
e =
|
|
213
|
+
ref1 = exp.vector;
|
|
214
|
+
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
|
215
|
+
e = ref1[j];
|
|
216
216
|
r = run(e, value);
|
|
217
217
|
if (r === void 0) {
|
|
218
218
|
return;
|
|
@@ -222,9 +222,9 @@
|
|
|
222
222
|
return result;
|
|
223
223
|
case "product":
|
|
224
224
|
result = {};
|
|
225
|
-
|
|
226
|
-
for (k = 0, len2 =
|
|
227
|
-
({label, exp} =
|
|
225
|
+
ref2 = exp.product;
|
|
226
|
+
for (k = 0, len2 = ref2.length; k < len2; k++) {
|
|
227
|
+
({label, exp} = ref2[k]);
|
|
228
228
|
r = run(exp, value);
|
|
229
229
|
if (r === void 0) {
|
|
230
230
|
return;
|