@fraczak/k 1.3.4 → 2.0.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/README.md +3 -2
- package/codes.mjs +104 -0
- package/index.mjs +35 -0
- package/k.mjs +92 -0
- package/package.json +6 -9
- package/parser.jison +1 -1
- package/{parser.js → parser.mjs} +9 -17
- package/pretty.mjs +95 -0
- package/repl.mjs +112 -0
- package/run.mjs +142 -0
- package/symbol-table.mjs +133 -0
- package/test.mjs +143 -0
- package/codes.coffee +0 -81
- package/codes.js +0 -134
- package/index.coffee +0 -28
- package/index.js +0 -49
- package/k.coffee +0 -59
- package/k.js +0 -93
- package/k.sh +0 -94
- package/pretty.coffee +0 -74
- package/pretty.js +0 -106
- package/repl.coffee +0 -62
- package/repl.js +0 -101
- package/repl.sh +0 -102
- package/run.coffee +0 -117
- package/run.js +0 -229
- package/symbol-table.coffee +0 -90
- package/symbol-table.js +0 -165
- package/test.coffee +0 -68
- package/test.js +0 -161
package/run.coffee
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
valid = (x) ->
|
|
2
|
-
return undefined if isNaN x
|
|
3
|
-
return x if x?
|
|
4
|
-
|
|
5
|
-
builtin =
|
|
6
|
-
"_log!": (arg) ->
|
|
7
|
-
console.error "_log!: #{JSON.stringify arg}"
|
|
8
|
-
arg
|
|
9
|
-
"GT": (args) ->
|
|
10
|
-
[ok,_] = do ([last,args...] = args) ->
|
|
11
|
-
args.reduce ([ok,last], x) ->
|
|
12
|
-
[ok and (last > x), x ]
|
|
13
|
-
, [true,last]
|
|
14
|
-
return args if ok
|
|
15
|
-
"EQ": (args) ->
|
|
16
|
-
[ok,_] = do ([last,args...] = args) ->
|
|
17
|
-
args.reduce ([ok,last], x) ->
|
|
18
|
-
[ok and (last is x), x ]
|
|
19
|
-
, [true,last]
|
|
20
|
-
return args if ok
|
|
21
|
-
"PLUS": (args) -> valid args.reduce ((res, x) -> res + x), 0
|
|
22
|
-
"TIMES": (args) -> valid args.reduce ((res, x) -> res * x), 1
|
|
23
|
-
"DIV": ([x,y]) ->
|
|
24
|
-
div = x // y
|
|
25
|
-
rem = x %% y
|
|
26
|
-
return {div,rem} if x is div * y + rem
|
|
27
|
-
"FDIV": ([x,y]) -> x / y
|
|
28
|
-
"CONCAT": (strs) -> strs.join('')
|
|
29
|
-
"true": -> true
|
|
30
|
-
"false": -> false
|
|
31
|
-
"null": -> null
|
|
32
|
-
"toJSON": (x) -> JSON.stringify x
|
|
33
|
-
"fromJSON": (x) -> JSON.parse x
|
|
34
|
-
"CONS": ([x,y]) -> [x,y...]
|
|
35
|
-
"SNOC": (x) ->
|
|
36
|
-
return [x[0],x.slice 1] if x.length > 1
|
|
37
|
-
"toDateMsec": (x) -> new Date(x).getTime()
|
|
38
|
-
"toDateStr": (x) -> new Date(x).toISOString()
|
|
39
|
-
|
|
40
|
-
codes =
|
|
41
|
-
"int": (x) ->
|
|
42
|
-
Number.isInteger x
|
|
43
|
-
"string": (x) ->
|
|
44
|
-
(x instanceof String) or ('string' is typeof x)
|
|
45
|
-
"bool": (x) ->
|
|
46
|
-
x is true or x is false
|
|
47
|
-
|
|
48
|
-
verify = (code, value) ->
|
|
49
|
-
# representatives = run.defs.representatives
|
|
50
|
-
# defCodes = JSON.stringify run.defs.codes
|
|
51
|
-
# console.log {code,value, representatives, defCodes}
|
|
52
|
-
return false unless code?
|
|
53
|
-
switch code.code
|
|
54
|
-
when "vector"
|
|
55
|
-
return false unless Array.isArray value
|
|
56
|
-
value.every (x) ->
|
|
57
|
-
verify code.vector, x
|
|
58
|
-
when "product"
|
|
59
|
-
return false unless "object" is typeof value
|
|
60
|
-
do (fields = Object.keys(value)) ->
|
|
61
|
-
return false unless fields.length is Object.keys(code.product).length
|
|
62
|
-
fields.every (label) ->
|
|
63
|
-
verify code.product[label], value[label]
|
|
64
|
-
when "union"
|
|
65
|
-
return false unless "object" is typeof value
|
|
66
|
-
do (fields = Object.keys(value)) ->
|
|
67
|
-
return false unless fields.length is 1
|
|
68
|
-
verify code.union[fields[0]], value[fields[0]]
|
|
69
|
-
else
|
|
70
|
-
do (c = run.defs.codes[run.defs.representatives[code]]) ->
|
|
71
|
-
return verify c, value if c?
|
|
72
|
-
codes[code] value
|
|
73
|
-
|
|
74
|
-
run = (exp, value) ->
|
|
75
|
-
"use strict"
|
|
76
|
-
# console.log {exp,value}
|
|
77
|
-
return undefined if value is undefined
|
|
78
|
-
switch exp.op
|
|
79
|
-
when "code"
|
|
80
|
-
return value if verify exp.code, value
|
|
81
|
-
when "identity"
|
|
82
|
-
return value
|
|
83
|
-
when "str", "int"
|
|
84
|
-
return exp[exp.op]
|
|
85
|
-
when "ref"
|
|
86
|
-
defn = run.defs.rels[exp.ref]
|
|
87
|
-
return run defn[defn.length - 1], value if defn?
|
|
88
|
-
return builtin[exp.ref] value
|
|
89
|
-
when "dot"
|
|
90
|
-
return value[exp.dot]
|
|
91
|
-
when "comp"
|
|
92
|
-
return exp.comp.reduce (value, exp) ->
|
|
93
|
-
run exp, value unless value is undefined
|
|
94
|
-
, value
|
|
95
|
-
when "union"
|
|
96
|
-
for e in exp.union
|
|
97
|
-
result = run e, value
|
|
98
|
-
return result unless result is undefined
|
|
99
|
-
return undefined
|
|
100
|
-
when "vector"
|
|
101
|
-
result = []
|
|
102
|
-
for e in exp.vector
|
|
103
|
-
r = run e, value
|
|
104
|
-
return if r is undefined
|
|
105
|
-
result.push r
|
|
106
|
-
return result
|
|
107
|
-
when "product"
|
|
108
|
-
result = {}
|
|
109
|
-
for {label,exp} in exp.product
|
|
110
|
-
r = run exp, value
|
|
111
|
-
return if r is undefined
|
|
112
|
-
result[label] = r
|
|
113
|
-
return result
|
|
114
|
-
else
|
|
115
|
-
console.error exp.op
|
|
116
|
-
|
|
117
|
-
module.exports = run
|
package/run.js
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
// Generated by CoffeeScript 2.7.0
|
|
2
|
-
(function() {
|
|
3
|
-
var builtin, codes, run, valid, verify,
|
|
4
|
-
modulo = function(a, b) { return (+a % (b = +b) + b) % b; };
|
|
5
|
-
|
|
6
|
-
valid = function(x) {
|
|
7
|
-
if (isNaN(x)) {
|
|
8
|
-
return void 0;
|
|
9
|
-
}
|
|
10
|
-
if (x != null) {
|
|
11
|
-
return x;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
builtin = {
|
|
16
|
-
"_log!": function(arg) {
|
|
17
|
-
console.error(`_log!: ${JSON.stringify(arg)}`);
|
|
18
|
-
return arg;
|
|
19
|
-
},
|
|
20
|
-
"GT": function(args) {
|
|
21
|
-
var _, ok;
|
|
22
|
-
[ok, _] = (function([last, ...args]) {
|
|
23
|
-
return args.reduce(function([ok, last], x) {
|
|
24
|
-
return [ok && (last > x), x];
|
|
25
|
-
}, [true, last]);
|
|
26
|
-
})(args);
|
|
27
|
-
if (ok) {
|
|
28
|
-
return args;
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"EQ": function(args) {
|
|
32
|
-
var _, ok;
|
|
33
|
-
[ok, _] = (function([last, ...args]) {
|
|
34
|
-
return args.reduce(function([ok, last], x) {
|
|
35
|
-
return [ok && (last === x), x];
|
|
36
|
-
}, [true, last]);
|
|
37
|
-
})(args);
|
|
38
|
-
if (ok) {
|
|
39
|
-
return args;
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
"PLUS": function(args) {
|
|
43
|
-
return valid(args.reduce((function(res, x) {
|
|
44
|
-
return res + x;
|
|
45
|
-
}), 0));
|
|
46
|
-
},
|
|
47
|
-
"TIMES": function(args) {
|
|
48
|
-
return valid(args.reduce((function(res, x) {
|
|
49
|
-
return res * x;
|
|
50
|
-
}), 1));
|
|
51
|
-
},
|
|
52
|
-
"DIV": function([x, y]) {
|
|
53
|
-
var div, rem;
|
|
54
|
-
div = Math.floor(x / y);
|
|
55
|
-
rem = modulo(x, y);
|
|
56
|
-
if (x === div * y + rem) {
|
|
57
|
-
return {div, rem};
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
"FDIV": function([x, y]) {
|
|
61
|
-
return x / y;
|
|
62
|
-
},
|
|
63
|
-
"CONCAT": function(strs) {
|
|
64
|
-
return strs.join('');
|
|
65
|
-
},
|
|
66
|
-
"true": function() {
|
|
67
|
-
return true;
|
|
68
|
-
},
|
|
69
|
-
"false": function() {
|
|
70
|
-
return false;
|
|
71
|
-
},
|
|
72
|
-
"null": function() {
|
|
73
|
-
return null;
|
|
74
|
-
},
|
|
75
|
-
"toJSON": function(x) {
|
|
76
|
-
return JSON.stringify(x);
|
|
77
|
-
},
|
|
78
|
-
"fromJSON": function(x) {
|
|
79
|
-
return JSON.parse(x);
|
|
80
|
-
},
|
|
81
|
-
"CONS": function([x, y]) {
|
|
82
|
-
return [x, ...y];
|
|
83
|
-
},
|
|
84
|
-
"SNOC": function(x) {
|
|
85
|
-
if (x.length > 1) {
|
|
86
|
-
return [x[0], x.slice(1)];
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
"toDateMsec": function(x) {
|
|
90
|
-
return new Date(x).getTime();
|
|
91
|
-
},
|
|
92
|
-
"toDateStr": function(x) {
|
|
93
|
-
return new Date(x).toISOString();
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
codes = {
|
|
98
|
-
"int": function(x) {
|
|
99
|
-
return Number.isInteger(x);
|
|
100
|
-
},
|
|
101
|
-
"string": function(x) {
|
|
102
|
-
return (x instanceof String) || ('string' === typeof x);
|
|
103
|
-
},
|
|
104
|
-
"bool": function(x) {
|
|
105
|
-
return x === true || x === false;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
verify = function(code, value) {
|
|
110
|
-
if (code == null) {
|
|
111
|
-
// representatives = run.defs.representatives
|
|
112
|
-
// defCodes = JSON.stringify run.defs.codes
|
|
113
|
-
// console.log {code,value, representatives, defCodes}
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
switch (code.code) {
|
|
117
|
-
case "vector":
|
|
118
|
-
if (!Array.isArray(value)) {
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
return value.every(function(x) {
|
|
122
|
-
return verify(code.vector, x);
|
|
123
|
-
});
|
|
124
|
-
case "product":
|
|
125
|
-
if ("object" !== typeof value) {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
return (function(fields) {
|
|
129
|
-
if (fields.length !== Object.keys(code.product).length) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
return fields.every(function(label) {
|
|
133
|
-
return verify(code.product[label], value[label]);
|
|
134
|
-
});
|
|
135
|
-
})(Object.keys(value));
|
|
136
|
-
case "union":
|
|
137
|
-
if ("object" !== typeof value) {
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
return (function(fields) {
|
|
141
|
-
if (fields.length !== 1) {
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
return verify(code.union[fields[0]], value[fields[0]]);
|
|
145
|
-
})(Object.keys(value));
|
|
146
|
-
default:
|
|
147
|
-
return (function(c) {
|
|
148
|
-
if (c != null) {
|
|
149
|
-
return verify(c, value);
|
|
150
|
-
}
|
|
151
|
-
return codes[code](value);
|
|
152
|
-
})(run.defs.codes[run.defs.representatives[code]]);
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
run = function(exp, value) {
|
|
157
|
-
"use strict";
|
|
158
|
-
var defn, e, i, j, k, label, len, len1, len2, r, ref, ref1, ref2, result;
|
|
159
|
-
if (value === void 0) {
|
|
160
|
-
// console.log {exp,value}
|
|
161
|
-
return void 0;
|
|
162
|
-
}
|
|
163
|
-
switch (exp.op) {
|
|
164
|
-
case "code":
|
|
165
|
-
if (verify(exp.code, value)) {
|
|
166
|
-
return value;
|
|
167
|
-
}
|
|
168
|
-
break;
|
|
169
|
-
case "identity":
|
|
170
|
-
return value;
|
|
171
|
-
case "str":
|
|
172
|
-
case "int":
|
|
173
|
-
return exp[exp.op];
|
|
174
|
-
case "ref":
|
|
175
|
-
defn = run.defs.rels[exp.ref];
|
|
176
|
-
if (defn != null) {
|
|
177
|
-
return run(defn[defn.length - 1], value);
|
|
178
|
-
}
|
|
179
|
-
return builtin[exp.ref](value);
|
|
180
|
-
case "dot":
|
|
181
|
-
return value[exp.dot];
|
|
182
|
-
case "comp":
|
|
183
|
-
return exp.comp.reduce(function(value, exp) {
|
|
184
|
-
if (value !== void 0) {
|
|
185
|
-
return run(exp, value);
|
|
186
|
-
}
|
|
187
|
-
}, value);
|
|
188
|
-
case "union":
|
|
189
|
-
ref = exp.union;
|
|
190
|
-
for (i = 0, len = ref.length; i < len; i++) {
|
|
191
|
-
e = ref[i];
|
|
192
|
-
result = run(e, value);
|
|
193
|
-
if (result !== void 0) {
|
|
194
|
-
return result;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return void 0;
|
|
198
|
-
case "vector":
|
|
199
|
-
result = [];
|
|
200
|
-
ref1 = exp.vector;
|
|
201
|
-
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
|
202
|
-
e = ref1[j];
|
|
203
|
-
r = run(e, value);
|
|
204
|
-
if (r === void 0) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
result.push(r);
|
|
208
|
-
}
|
|
209
|
-
return result;
|
|
210
|
-
case "product":
|
|
211
|
-
result = {};
|
|
212
|
-
ref2 = exp.product;
|
|
213
|
-
for (k = 0, len2 = ref2.length; k < len2; k++) {
|
|
214
|
-
({label, exp} = ref2[k]);
|
|
215
|
-
r = run(exp, value);
|
|
216
|
-
if (r === void 0) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
result[label] = r;
|
|
220
|
-
}
|
|
221
|
-
return result;
|
|
222
|
-
default:
|
|
223
|
-
return console.error(exp.op);
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
module.exports = run;
|
|
228
|
-
|
|
229
|
-
}).call(this);
|
package/symbol-table.coffee
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
rels = Object.create null
|
|
2
|
-
codes = Object.create null
|
|
3
|
-
codes["{}"] = {code: "product", product: {}}
|
|
4
|
-
|
|
5
|
-
identity = op: "identity"
|
|
6
|
-
|
|
7
|
-
is_identity_rel = (rel) ->
|
|
8
|
-
rel.op is "identity"
|
|
9
|
-
|
|
10
|
-
is_constant_rel = (rel) ->
|
|
11
|
-
switch rel?.op
|
|
12
|
-
when "int", "str"
|
|
13
|
-
true
|
|
14
|
-
when "vector"
|
|
15
|
-
Object.values(rel[rel.op]).every is_constant_rel
|
|
16
|
-
when "product"
|
|
17
|
-
Object.values(rel[rel.op]).every ({exp}) ->
|
|
18
|
-
is_constant_rel exp
|
|
19
|
-
else
|
|
20
|
-
false
|
|
21
|
-
|
|
22
|
-
is_empty_rel = (rel) ->
|
|
23
|
-
rel.op is "union" and rel.union.length is 0
|
|
24
|
-
|
|
25
|
-
is_full_rel = (rel) ->
|
|
26
|
-
return true if is_constant_rel rel
|
|
27
|
-
switch rel.op
|
|
28
|
-
when "int", "str", "identity"
|
|
29
|
-
true
|
|
30
|
-
when "comp"
|
|
31
|
-
rel.comp.every is_full_rel
|
|
32
|
-
when "vector"
|
|
33
|
-
Object.values(rel[rel.op]).every is_full_rel
|
|
34
|
-
when "product"
|
|
35
|
-
Object.values(rel[rel.op]).every ({exp}) ->
|
|
36
|
-
is_full_rel exp
|
|
37
|
-
else
|
|
38
|
-
false
|
|
39
|
-
|
|
40
|
-
comp_first = (e1, e2) ->
|
|
41
|
-
return e2 if is_identity_rel e1
|
|
42
|
-
return e1 if is_identity_rel e2
|
|
43
|
-
return e1 if is_empty_rel e1
|
|
44
|
-
return e2 if is_empty_rel e2
|
|
45
|
-
if e1.op is "comp" and e2.op is "comp"
|
|
46
|
-
return {op: "comp", comp: [].concat(e1.comp,e2.comp)}
|
|
47
|
-
if e1.op is "comp"
|
|
48
|
-
return {op: "comp", comp: [].concat(e1.comp,[e2])}
|
|
49
|
-
if e2.op is "comp"
|
|
50
|
-
return {op: "comp", comp: [].concat([e1],e2.comp)}
|
|
51
|
-
{op: "comp", comp: [e1,e2]}
|
|
52
|
-
|
|
53
|
-
comp = (e1, e2) ->
|
|
54
|
-
result = comp_first e1, e2
|
|
55
|
-
return result unless result.op is "comp"
|
|
56
|
-
result.comp = result.comp.reduceRight (c, e) ->
|
|
57
|
-
if is_constant_rel(c[0]) and is_full_rel e
|
|
58
|
-
return c
|
|
59
|
-
return [e, c...]
|
|
60
|
-
, []
|
|
61
|
-
result
|
|
62
|
-
|
|
63
|
-
union = (rels) ->
|
|
64
|
-
list = []
|
|
65
|
-
do ->
|
|
66
|
-
for rel in rels
|
|
67
|
-
new_rels = if rel.op is "union"
|
|
68
|
-
rel.union
|
|
69
|
-
else
|
|
70
|
-
[rel]
|
|
71
|
-
for rel in new_rels
|
|
72
|
-
list.push rel
|
|
73
|
-
return if is_full_rel rel
|
|
74
|
-
return list[0] if list.length is 1
|
|
75
|
-
{op: "union", union: list}
|
|
76
|
-
|
|
77
|
-
as_ref = (codeExp) ->
|
|
78
|
-
return codeExp.ref if codeExp.code is "ref"
|
|
79
|
-
newName = ":#{Object.keys(codes).length}"
|
|
80
|
-
codes[newName] = codeExp
|
|
81
|
-
return newName
|
|
82
|
-
|
|
83
|
-
add_rel = (name, rel) ->
|
|
84
|
-
rels[name] ?= []
|
|
85
|
-
rels[name].push rel
|
|
86
|
-
|
|
87
|
-
add_code = (name, code) ->
|
|
88
|
-
codes[name] = code
|
|
89
|
-
|
|
90
|
-
module.exports = {identity, comp, union, rels, codes, add_rel, add_code, as_ref};
|
package/symbol-table.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
// Generated by CoffeeScript 2.7.0
|
|
2
|
-
(function() {
|
|
3
|
-
var add_code, add_rel, as_ref, codes, comp, comp_first, identity, is_constant_rel, is_empty_rel, is_full_rel, is_identity_rel, rels, union;
|
|
4
|
-
|
|
5
|
-
rels = Object.create(null);
|
|
6
|
-
|
|
7
|
-
codes = Object.create(null);
|
|
8
|
-
|
|
9
|
-
codes["{}"] = {
|
|
10
|
-
code: "product",
|
|
11
|
-
product: {}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
identity = {
|
|
15
|
-
op: "identity"
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
is_identity_rel = function(rel) {
|
|
19
|
-
return rel.op === "identity";
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
is_constant_rel = function(rel) {
|
|
23
|
-
switch (rel != null ? rel.op : void 0) {
|
|
24
|
-
case "int":
|
|
25
|
-
case "str":
|
|
26
|
-
return true;
|
|
27
|
-
case "vector":
|
|
28
|
-
return Object.values(rel[rel.op]).every(is_constant_rel);
|
|
29
|
-
case "product":
|
|
30
|
-
return Object.values(rel[rel.op]).every(function({exp}) {
|
|
31
|
-
return is_constant_rel(exp);
|
|
32
|
-
});
|
|
33
|
-
default:
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
is_empty_rel = function(rel) {
|
|
39
|
-
return rel.op === "union" && rel.union.length === 0;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
is_full_rel = function(rel) {
|
|
43
|
-
if (is_constant_rel(rel)) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
switch (rel.op) {
|
|
47
|
-
case "int":
|
|
48
|
-
case "str":
|
|
49
|
-
case "identity":
|
|
50
|
-
return true;
|
|
51
|
-
case "comp":
|
|
52
|
-
return rel.comp.every(is_full_rel);
|
|
53
|
-
case "vector":
|
|
54
|
-
return Object.values(rel[rel.op]).every(is_full_rel);
|
|
55
|
-
case "product":
|
|
56
|
-
return Object.values(rel[rel.op]).every(function({exp}) {
|
|
57
|
-
return is_full_rel(exp);
|
|
58
|
-
});
|
|
59
|
-
default:
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
comp_first = function(e1, e2) {
|
|
65
|
-
if (is_identity_rel(e1)) {
|
|
66
|
-
return e2;
|
|
67
|
-
}
|
|
68
|
-
if (is_identity_rel(e2)) {
|
|
69
|
-
return e1;
|
|
70
|
-
}
|
|
71
|
-
if (is_empty_rel(e1)) {
|
|
72
|
-
return e1;
|
|
73
|
-
}
|
|
74
|
-
if (is_empty_rel(e2)) {
|
|
75
|
-
return e2;
|
|
76
|
-
}
|
|
77
|
-
if (e1.op === "comp" && e2.op === "comp") {
|
|
78
|
-
return {
|
|
79
|
-
op: "comp",
|
|
80
|
-
comp: [].concat(e1.comp, e2.comp)
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
if (e1.op === "comp") {
|
|
84
|
-
return {
|
|
85
|
-
op: "comp",
|
|
86
|
-
comp: [].concat(e1.comp, [e2])
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
if (e2.op === "comp") {
|
|
90
|
-
return {
|
|
91
|
-
op: "comp",
|
|
92
|
-
comp: [].concat([e1], e2.comp)
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
return {
|
|
96
|
-
op: "comp",
|
|
97
|
-
comp: [e1, e2]
|
|
98
|
-
};
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
comp = function(e1, e2) {
|
|
102
|
-
var result;
|
|
103
|
-
result = comp_first(e1, e2);
|
|
104
|
-
if (result.op !== "comp") {
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
result.comp = result.comp.reduceRight(function(c, e) {
|
|
108
|
-
if (is_constant_rel(c[0]) && is_full_rel(e)) {
|
|
109
|
-
return c;
|
|
110
|
-
}
|
|
111
|
-
return [e, ...c];
|
|
112
|
-
}, []);
|
|
113
|
-
return result;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
union = function(rels) {
|
|
117
|
-
var list;
|
|
118
|
-
list = [];
|
|
119
|
-
(function() {
|
|
120
|
-
var i, j, len, len1, new_rels, rel;
|
|
121
|
-
for (i = 0, len = rels.length; i < len; i++) {
|
|
122
|
-
rel = rels[i];
|
|
123
|
-
new_rels = rel.op === "union" ? rel.union : [rel];
|
|
124
|
-
for (j = 0, len1 = new_rels.length; j < len1; j++) {
|
|
125
|
-
rel = new_rels[j];
|
|
126
|
-
list.push(rel);
|
|
127
|
-
if (is_full_rel(rel)) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
})();
|
|
133
|
-
if (list.length === 1) {
|
|
134
|
-
return list[0];
|
|
135
|
-
}
|
|
136
|
-
return {
|
|
137
|
-
op: "union",
|
|
138
|
-
union: list
|
|
139
|
-
};
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
as_ref = function(codeExp) {
|
|
143
|
-
var newName;
|
|
144
|
-
if (codeExp.code === "ref") {
|
|
145
|
-
return codeExp.ref;
|
|
146
|
-
}
|
|
147
|
-
newName = `:${Object.keys(codes).length}`;
|
|
148
|
-
codes[newName] = codeExp;
|
|
149
|
-
return newName;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
add_rel = function(name, rel) {
|
|
153
|
-
if (rels[name] == null) {
|
|
154
|
-
rels[name] = [];
|
|
155
|
-
}
|
|
156
|
-
return rels[name].push(rel);
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
add_code = function(name, code) {
|
|
160
|
-
return codes[name] = code;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
module.exports = {identity, comp, union, rels, codes, add_rel, add_code, as_ref};
|
|
164
|
-
|
|
165
|
-
}).call(this);
|
package/test.coffee
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
k = require "./index.coffee"
|
|
3
|
-
|
|
4
|
-
console.log 'var k = require("k");'
|
|
5
|
-
console.log ""
|
|
6
|
-
[
|
|
7
|
-
["ANYTHING...", '()']
|
|
8
|
-
["ANYTHING...", '{"ala" name, 23 age}']
|
|
9
|
-
[{year: 2002, age: 19}, '[.year, .age]' ]
|
|
10
|
-
["duplicate me", '[(), ()]']
|
|
11
|
-
["nesting", '[[[()]]]']
|
|
12
|
-
["nesting and accessing", '[[()]] {() nested, .0.0 val}']
|
|
13
|
-
[{test: "parse integer"}, '0000']
|
|
14
|
-
[{x: 3, y: 4}, '[.y,.x] PLUS']
|
|
15
|
-
].map ([data,script]) ->
|
|
16
|
-
console.log "k_expression = '#{script}';"
|
|
17
|
-
console.log "k.run(k_expression,#{JSON.stringify(data)});"
|
|
18
|
-
console.log "// RETURNS: #{JSON.stringify k.run script, data}"
|
|
19
|
-
console.log ""
|
|
20
|
-
|
|
21
|
-
k_expression = '{.name nom, <[.age, 18] GT .0, [.age, 12] GT "ado", "enfant"> age}'
|
|
22
|
-
k_fn = k.compile k_expression
|
|
23
|
-
|
|
24
|
-
console.log "var k_fn = k.compile('#{k_expression}');"
|
|
25
|
-
console.log ""
|
|
26
|
-
|
|
27
|
-
[
|
|
28
|
-
{age: 23, name: "Emily"}
|
|
29
|
-
{age: 16, name: "Katrina"}
|
|
30
|
-
{age: 2, name: "Mark"}
|
|
31
|
-
].map (data) ->
|
|
32
|
-
console.log "k_fn(#{JSON.stringify(data)});"
|
|
33
|
-
console.log "// RETURNS: #{JSON.stringify k_fn data}"
|
|
34
|
-
console.log ""
|
|
35
|
-
|
|
36
|
-
k_expression = "$t = < i: int, t: [ t ] > ; <$t, $int>"
|
|
37
|
-
k_fn = k.compile k_expression
|
|
38
|
-
|
|
39
|
-
console.log "var k_fn = k.compile('#{k_expression}');"
|
|
40
|
-
console.log ""
|
|
41
|
-
|
|
42
|
-
[
|
|
43
|
-
1
|
|
44
|
-
{i:1}
|
|
45
|
-
[{i:2},{i:3},{t:[]}]
|
|
46
|
-
{"t":[{"i":2},{"i":3},{"t":[]}]}
|
|
47
|
-
].map (data) ->
|
|
48
|
-
console.log "k_fn(#{JSON.stringify(data)});"
|
|
49
|
-
console.log "// RETURNS: #{JSON.stringify k_fn data}"
|
|
50
|
-
console.log ""
|
|
51
|
-
|
|
52
|
-
k_expression = "$ < < [ int ] ints, [ bool ] bools > list, string None>"
|
|
53
|
-
k_fn = k.compile k_expression
|
|
54
|
-
console.log "var k_fn = k.compile('#{k_expression}');"
|
|
55
|
-
console.log ""
|
|
56
|
-
|
|
57
|
-
[
|
|
58
|
-
{None: "None"}
|
|
59
|
-
{list: {ints: []}}
|
|
60
|
-
{list: {ints: [1,2,3]}}
|
|
61
|
-
{list: {bools: [true, false]}}
|
|
62
|
-
{list: {bools: [true, 0]}}
|
|
63
|
-
].map (data) ->
|
|
64
|
-
console.log "k_fn(#{JSON.stringify(data)});"
|
|
65
|
-
console.log "// RETURNS: #{JSON.stringify k_fn data}"
|
|
66
|
-
console.log ""
|
|
67
|
-
|
|
68
|
-
|