@fraczak/k 1.3.4 → 2.0.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/README.md +224 -44
- package/codes.mjs +103 -0
- package/index.mjs +32 -0
- package/k.mjs +79 -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 +158 -0
- package/symbol-table.mjs +133 -0
- package/test.mjs +143 -0
- package/INTRO.md +0 -160
- 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/pretty.coffee
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
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-Z0-9_][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 nameRE.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 nameRE.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
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
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-Z0-9_][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 = nameRE.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 (nameRE.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
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env coffee
|
|
2
|
-
k = require "./"
|
|
3
|
-
run = require "./run"
|
|
4
|
-
{prettyCode, prettyRel} = require "./pretty"
|
|
5
|
-
fs = require "fs"
|
|
6
|
-
|
|
7
|
-
console.log "Very! experimental repl shell for 'k-language'..."
|
|
8
|
-
|
|
9
|
-
help = ->
|
|
10
|
-
console.log " --h print help"
|
|
11
|
-
console.log " --a print codes and relations"
|
|
12
|
-
console.log " --c print codes"
|
|
13
|
-
console.log " --r print rels"
|
|
14
|
-
console.log " --l file.k load 'file.k'"
|
|
15
|
-
|
|
16
|
-
help()
|
|
17
|
-
|
|
18
|
-
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/
|
|
19
|
-
|
|
20
|
-
do (val = {}, buffer = []) ->
|
|
21
|
-
process.stdin.on 'data', (data) ->
|
|
22
|
-
[first, rest...] = data.toString("utf8").split "\n"
|
|
23
|
-
buffer.push first
|
|
24
|
-
if rest.length > 0
|
|
25
|
-
todo = buffer.join ""
|
|
26
|
-
[rest..., last] = rest
|
|
27
|
-
buffer = [last]
|
|
28
|
-
todo = [todo, rest...]
|
|
29
|
-
for line in todo
|
|
30
|
-
if val is undefined
|
|
31
|
-
val = {}
|
|
32
|
-
if line.match /^[ \n\t]*(?:--h)?$/
|
|
33
|
-
help()
|
|
34
|
-
else if line.match re
|
|
35
|
-
file = line.match(re)[1]
|
|
36
|
-
console.log "-- loading file: #{file} ..."
|
|
37
|
-
kScript = fs.readFileSync(file).toString();
|
|
38
|
-
val = k.compile(kScript) val
|
|
39
|
-
console.log "=> #{JSON.stringify val}"
|
|
40
|
-
else if line.match /^[ \n\t]*(?:--a)?$/
|
|
41
|
-
console.log JSON.stringify run.defs, " ", 2
|
|
42
|
-
else if line.match /^[ \n\t]*(?:--r)?$/
|
|
43
|
-
console.log do (defs = run.defs, result = {}) ->
|
|
44
|
-
return result unless defs?
|
|
45
|
-
do (prettyRel = prettyRel.bind null, prettyCode.bind null, defs.codes, defs.representatives) ->
|
|
46
|
-
for relName, relExps of defs.rels
|
|
47
|
-
result[relName] = relExps.map prettyRel
|
|
48
|
-
result
|
|
49
|
-
else if line.match /^[ \n\t]*(?:--c)?$/
|
|
50
|
-
console.log do (defs = run.defs, result = {}) ->
|
|
51
|
-
return result unless defs?
|
|
52
|
-
for codeName, codeExp of defs.codes
|
|
53
|
-
result[codeName] = prettyCode defs.codes, defs.representatives, codeExp
|
|
54
|
-
result
|
|
55
|
-
else if not line.match /^[ \n\t]*(?:#.*)?$/
|
|
56
|
-
try
|
|
57
|
-
val = k.run "#{line} ()", val
|
|
58
|
-
console.log "=> #{JSON.stringify val}"
|
|
59
|
-
catch e
|
|
60
|
-
console.error "ERROR:"
|
|
61
|
-
console.error e
|
|
62
|
-
|
package/repl.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
// Generated by CoffeeScript 2.7.0
|
|
2
|
-
(function() {
|
|
3
|
-
//!/usr/bin/env coffee
|
|
4
|
-
var fs, help, k, prettyCode, prettyRel, re, run,
|
|
5
|
-
splice = [].splice;
|
|
6
|
-
|
|
7
|
-
k = require("./");
|
|
8
|
-
|
|
9
|
-
run = require("./run");
|
|
10
|
-
|
|
11
|
-
({prettyCode, prettyRel} = require("./pretty"));
|
|
12
|
-
|
|
13
|
-
fs = require("fs");
|
|
14
|
-
|
|
15
|
-
console.log("Very! experimental repl shell for 'k-language'...");
|
|
16
|
-
|
|
17
|
-
help = function() {
|
|
18
|
-
console.log(" --h print help");
|
|
19
|
-
console.log(" --a print codes and relations");
|
|
20
|
-
console.log(" --c print codes");
|
|
21
|
-
console.log(" --r print rels");
|
|
22
|
-
return console.log(" --l file.k load 'file.k'");
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
help();
|
|
26
|
-
|
|
27
|
-
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
|
|
28
|
-
|
|
29
|
-
(function(val, buffer) {
|
|
30
|
-
return process.stdin.on('data', function(data) {
|
|
31
|
-
var e, file, first, i, kScript, last, len, line, ref, rest, results, todo;
|
|
32
|
-
[first, ...rest] = data.toString("utf8").split("\n");
|
|
33
|
-
buffer.push(first);
|
|
34
|
-
if (rest.length > 0) {
|
|
35
|
-
todo = buffer.join("");
|
|
36
|
-
ref = rest, [...rest] = ref, [last] = splice.call(rest, -1);
|
|
37
|
-
buffer = [last];
|
|
38
|
-
todo = [todo, ...rest];
|
|
39
|
-
results = [];
|
|
40
|
-
for (i = 0, len = todo.length; i < len; i++) {
|
|
41
|
-
line = todo[i];
|
|
42
|
-
if (val === void 0) {
|
|
43
|
-
val = {};
|
|
44
|
-
}
|
|
45
|
-
if (line.match(/^[ \n\t]*(?:--h)?$/)) {
|
|
46
|
-
results.push(help());
|
|
47
|
-
} else if (line.match(re)) {
|
|
48
|
-
file = line.match(re)[1];
|
|
49
|
-
console.log(`-- loading file: ${file} ...`);
|
|
50
|
-
kScript = fs.readFileSync(file).toString();
|
|
51
|
-
val = k.compile(kScript)(val);
|
|
52
|
-
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
53
|
-
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
54
|
-
results.push(console.log(JSON.stringify(run.defs, " ", 2)));
|
|
55
|
-
} else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
|
|
56
|
-
results.push(console.log((function(defs, result) {
|
|
57
|
-
if (defs == null) {
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
return (function(prettyRel) {
|
|
61
|
-
var ref1, relExps, relName;
|
|
62
|
-
ref1 = defs.rels;
|
|
63
|
-
for (relName in ref1) {
|
|
64
|
-
relExps = ref1[relName];
|
|
65
|
-
result[relName] = relExps.map(prettyRel);
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
})(prettyRel.bind(null, prettyCode.bind(null, defs.codes, defs.representatives)));
|
|
69
|
-
})(run.defs, {})));
|
|
70
|
-
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
71
|
-
results.push(console.log((function(defs, result) {
|
|
72
|
-
var codeExp, codeName, ref1;
|
|
73
|
-
if (defs == null) {
|
|
74
|
-
return result;
|
|
75
|
-
}
|
|
76
|
-
ref1 = defs.codes;
|
|
77
|
-
for (codeName in ref1) {
|
|
78
|
-
codeExp = ref1[codeName];
|
|
79
|
-
result[codeName] = prettyCode(defs.codes, defs.representatives, codeExp);
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
})(run.defs, {})));
|
|
83
|
-
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
84
|
-
try {
|
|
85
|
-
val = k.run(`${line} ()`, val);
|
|
86
|
-
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
87
|
-
} catch (error) {
|
|
88
|
-
e = error;
|
|
89
|
-
console.error("ERROR:");
|
|
90
|
-
results.push(console.error(e));
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
results.push(void 0);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return results;
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
})({}, []);
|
|
100
|
-
|
|
101
|
-
}).call(this);
|
package/repl.sh
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Generated by CoffeeScript 2.7.0
|
|
3
|
-
(function() {
|
|
4
|
-
//!/usr/bin/env coffee
|
|
5
|
-
var fs, help, k, prettyCode, prettyRel, re, run,
|
|
6
|
-
splice = [].splice;
|
|
7
|
-
|
|
8
|
-
k = require("./");
|
|
9
|
-
|
|
10
|
-
run = require("./run");
|
|
11
|
-
|
|
12
|
-
({prettyCode, prettyRel} = require("./pretty"));
|
|
13
|
-
|
|
14
|
-
fs = require("fs");
|
|
15
|
-
|
|
16
|
-
console.log("Very! experimental repl shell for 'k-language'...");
|
|
17
|
-
|
|
18
|
-
help = function() {
|
|
19
|
-
console.log(" --h print help");
|
|
20
|
-
console.log(" --a print codes and relations");
|
|
21
|
-
console.log(" --c print codes");
|
|
22
|
-
console.log(" --r print rels");
|
|
23
|
-
return console.log(" --l file.k load 'file.k'");
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
help();
|
|
27
|
-
|
|
28
|
-
re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
|
|
29
|
-
|
|
30
|
-
(function(val, buffer) {
|
|
31
|
-
return process.stdin.on('data', function(data) {
|
|
32
|
-
var e, file, first, i, kScript, last, len, line, ref, rest, results, todo;
|
|
33
|
-
[first, ...rest] = data.toString("utf8").split("\n");
|
|
34
|
-
buffer.push(first);
|
|
35
|
-
if (rest.length > 0) {
|
|
36
|
-
todo = buffer.join("");
|
|
37
|
-
ref = rest, [...rest] = ref, [last] = splice.call(rest, -1);
|
|
38
|
-
buffer = [last];
|
|
39
|
-
todo = [todo, ...rest];
|
|
40
|
-
results = [];
|
|
41
|
-
for (i = 0, len = todo.length; i < len; i++) {
|
|
42
|
-
line = todo[i];
|
|
43
|
-
if (val === void 0) {
|
|
44
|
-
val = {};
|
|
45
|
-
}
|
|
46
|
-
if (line.match(/^[ \n\t]*(?:--h)?$/)) {
|
|
47
|
-
results.push(help());
|
|
48
|
-
} else if (line.match(re)) {
|
|
49
|
-
file = line.match(re)[1];
|
|
50
|
-
console.log(`-- loading file: ${file} ...`);
|
|
51
|
-
kScript = fs.readFileSync(file).toString();
|
|
52
|
-
val = k.compile(kScript)(val);
|
|
53
|
-
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
54
|
-
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
55
|
-
results.push(console.log(JSON.stringify(run.defs, " ", 2)));
|
|
56
|
-
} else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
|
|
57
|
-
results.push(console.log((function(defs, result) {
|
|
58
|
-
if (defs == null) {
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
return (function(prettyRel) {
|
|
62
|
-
var ref1, relExps, relName;
|
|
63
|
-
ref1 = defs.rels;
|
|
64
|
-
for (relName in ref1) {
|
|
65
|
-
relExps = ref1[relName];
|
|
66
|
-
result[relName] = relExps.map(prettyRel);
|
|
67
|
-
}
|
|
68
|
-
return result;
|
|
69
|
-
})(prettyRel.bind(null, prettyCode.bind(null, defs.codes, defs.representatives)));
|
|
70
|
-
})(run.defs, {})));
|
|
71
|
-
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
72
|
-
results.push(console.log((function(defs, result) {
|
|
73
|
-
var codeExp, codeName, ref1;
|
|
74
|
-
if (defs == null) {
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
ref1 = defs.codes;
|
|
78
|
-
for (codeName in ref1) {
|
|
79
|
-
codeExp = ref1[codeName];
|
|
80
|
-
result[codeName] = prettyCode(defs.codes, defs.representatives, codeExp);
|
|
81
|
-
}
|
|
82
|
-
return result;
|
|
83
|
-
})(run.defs, {})));
|
|
84
|
-
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
85
|
-
try {
|
|
86
|
-
val = k.run(`${line} ()`, val);
|
|
87
|
-
results.push(console.log(`=> ${JSON.stringify(val)}`));
|
|
88
|
-
} catch (error) {
|
|
89
|
-
e = error;
|
|
90
|
-
console.error("ERROR:");
|
|
91
|
-
results.push(console.error(e));
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
results.push(void 0);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return results;
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
})({}, []);
|
|
101
|
-
|
|
102
|
-
}).call(this);
|
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
|