@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/k.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// Generated by CoffeeScript 2.7.0
|
|
2
|
-
(function() {
|
|
3
|
-
//!/usr/bin/env coffee
|
|
4
|
-
var fs, jsonStream, k, kScript, oneJson, prog,
|
|
5
|
-
splice = [].splice;
|
|
6
|
-
|
|
7
|
-
fs = require("fs");
|
|
8
|
-
|
|
9
|
-
k = require("./");
|
|
10
|
-
|
|
11
|
-
prog = process.argv[1];
|
|
12
|
-
|
|
13
|
-
({kScript, jsonStream, oneJson} = (function(oneJson, args) {
|
|
14
|
-
var e;
|
|
15
|
-
try {
|
|
16
|
-
kScript = (function(arg) {
|
|
17
|
-
if (arg == null) {
|
|
18
|
-
throw new Error();
|
|
19
|
-
}
|
|
20
|
-
if (arg === "-k") {
|
|
21
|
-
return fs.readFileSync(args.shift()).toString("utf8");
|
|
22
|
-
} else {
|
|
23
|
-
return arg;
|
|
24
|
-
}
|
|
25
|
-
})(args.shift());
|
|
26
|
-
kScript = k.compile(kScript);
|
|
27
|
-
jsonStream = (function(arg) {
|
|
28
|
-
if (arg === "-1") {
|
|
29
|
-
oneJson = true;
|
|
30
|
-
arg = args.shift();
|
|
31
|
-
}
|
|
32
|
-
if (arg == null) {
|
|
33
|
-
return process.stdin;
|
|
34
|
-
}
|
|
35
|
-
return fs.createReadStream(arg);
|
|
36
|
-
})(args.shift());
|
|
37
|
-
return {kScript, jsonStream, oneJson};
|
|
38
|
-
} catch (error) {
|
|
39
|
-
e = error;
|
|
40
|
-
console.error(e);
|
|
41
|
-
console.error(`Usage: ${prog} ( k-expr | -k k-file ) [ -1 ] [ json-file ]`);
|
|
42
|
-
console.error(` E.g., echo '{\"a\": 12}' | ${prog} '[(),()]'`);
|
|
43
|
-
return process.exit(-1);
|
|
44
|
-
}
|
|
45
|
-
})(false, process.argv.slice(2)));
|
|
46
|
-
|
|
47
|
-
if (oneJson) {
|
|
48
|
-
(function(buffer) {
|
|
49
|
-
jsonStream.on('data', function(data) {
|
|
50
|
-
return buffer.push(data);
|
|
51
|
-
});
|
|
52
|
-
return jsonStream.on('end', function() {
|
|
53
|
-
var e;
|
|
54
|
-
try {
|
|
55
|
-
return console.log(JSON.stringify(kScript(JSON.parse(buffer.join('')))));
|
|
56
|
-
} catch (error) {
|
|
57
|
-
e = error;
|
|
58
|
-
return console.error(e);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
})([]);
|
|
62
|
-
} else {
|
|
63
|
-
(function(buffer, line) {
|
|
64
|
-
return jsonStream.on('data', function(data) {
|
|
65
|
-
var e, first, i, json, last, len, ref, rest, results, todo;
|
|
66
|
-
[first, ...rest] = data.toString("utf8").split("\n");
|
|
67
|
-
buffer.push(first);
|
|
68
|
-
if (rest.length > 0) {
|
|
69
|
-
todo = buffer.join("");
|
|
70
|
-
ref = rest, [...rest] = ref, [last] = splice.call(rest, -1);
|
|
71
|
-
buffer = [last];
|
|
72
|
-
todo = [todo, ...rest];
|
|
73
|
-
results = [];
|
|
74
|
-
for (i = 0, len = todo.length; i < len; i++) {
|
|
75
|
-
json = todo[i];
|
|
76
|
-
if (!json.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
77
|
-
try {
|
|
78
|
-
console.log(JSON.stringify(kScript(JSON.parse(json))));
|
|
79
|
-
} catch (error) {
|
|
80
|
-
e = error;
|
|
81
|
-
console.error(`Problem [line ${line}]: '${json}'`);
|
|
82
|
-
console.error(e);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
results.push(line = line + 1);
|
|
86
|
-
}
|
|
87
|
-
return results;
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
})([], 0);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
}).call(this);
|
package/k.sh
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Generated by CoffeeScript 2.7.0
|
|
3
|
-
(function() {
|
|
4
|
-
//!/usr/bin/env coffee
|
|
5
|
-
var fs, jsonStream, k, kScript, oneJson, prog,
|
|
6
|
-
splice = [].splice;
|
|
7
|
-
|
|
8
|
-
fs = require("fs");
|
|
9
|
-
|
|
10
|
-
k = require("./");
|
|
11
|
-
|
|
12
|
-
prog = process.argv[1];
|
|
13
|
-
|
|
14
|
-
({kScript, jsonStream, oneJson} = (function(oneJson, args) {
|
|
15
|
-
var e;
|
|
16
|
-
try {
|
|
17
|
-
kScript = (function(arg) {
|
|
18
|
-
if (arg == null) {
|
|
19
|
-
throw new Error();
|
|
20
|
-
}
|
|
21
|
-
if (arg === "-k") {
|
|
22
|
-
return fs.readFileSync(args.shift()).toString("utf8");
|
|
23
|
-
} else {
|
|
24
|
-
return arg;
|
|
25
|
-
}
|
|
26
|
-
})(args.shift());
|
|
27
|
-
kScript = k.compile(kScript);
|
|
28
|
-
jsonStream = (function(arg) {
|
|
29
|
-
if (arg === "-1") {
|
|
30
|
-
oneJson = true;
|
|
31
|
-
arg = args.shift();
|
|
32
|
-
}
|
|
33
|
-
if (arg == null) {
|
|
34
|
-
return process.stdin;
|
|
35
|
-
}
|
|
36
|
-
return fs.createReadStream(arg);
|
|
37
|
-
})(args.shift());
|
|
38
|
-
return {kScript, jsonStream, oneJson};
|
|
39
|
-
} catch (error) {
|
|
40
|
-
e = error;
|
|
41
|
-
console.error(e);
|
|
42
|
-
console.error(`Usage: ${prog} ( k-expr | -k k-file ) [ -1 ] [ json-file ]`);
|
|
43
|
-
console.error(` E.g., echo '{\"a\": 12}' | ${prog} '[(),()]'`);
|
|
44
|
-
return process.exit(-1);
|
|
45
|
-
}
|
|
46
|
-
})(false, process.argv.slice(2)));
|
|
47
|
-
|
|
48
|
-
if (oneJson) {
|
|
49
|
-
(function(buffer) {
|
|
50
|
-
jsonStream.on('data', function(data) {
|
|
51
|
-
return buffer.push(data);
|
|
52
|
-
});
|
|
53
|
-
return jsonStream.on('end', function() {
|
|
54
|
-
var e;
|
|
55
|
-
try {
|
|
56
|
-
return console.log(JSON.stringify(kScript(JSON.parse(buffer.join('')))));
|
|
57
|
-
} catch (error) {
|
|
58
|
-
e = error;
|
|
59
|
-
return console.error(e);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
})([]);
|
|
63
|
-
} else {
|
|
64
|
-
(function(buffer, line) {
|
|
65
|
-
return jsonStream.on('data', function(data) {
|
|
66
|
-
var e, first, i, json, last, len, ref, rest, results, todo;
|
|
67
|
-
[first, ...rest] = data.toString("utf8").split("\n");
|
|
68
|
-
buffer.push(first);
|
|
69
|
-
if (rest.length > 0) {
|
|
70
|
-
todo = buffer.join("");
|
|
71
|
-
ref = rest, [...rest] = ref, [last] = splice.call(rest, -1);
|
|
72
|
-
buffer = [last];
|
|
73
|
-
todo = [todo, ...rest];
|
|
74
|
-
results = [];
|
|
75
|
-
for (i = 0, len = todo.length; i < len; i++) {
|
|
76
|
-
json = todo[i];
|
|
77
|
-
if (!json.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
78
|
-
try {
|
|
79
|
-
console.log(JSON.stringify(kScript(JSON.parse(json))));
|
|
80
|
-
} catch (error) {
|
|
81
|
-
e = error;
|
|
82
|
-
console.error(`Problem [line ${line}]: '${json}'`);
|
|
83
|
-
console.error(e);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
results.push(line = line + 1);
|
|
87
|
-
}
|
|
88
|
-
return results;
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
})([], 0);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
}).call(this);
|
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);
|