@fraczak/k 1.3.1 → 1.3.2

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/index.cjs ADDED
@@ -0,0 +1,49 @@
1
+ // Generated by CoffeeScript 2.7.0
2
+ (async function() {
3
+ var compile, finalize, parse, run, runScriptOnData, t;
4
+
5
+ ({parse} = await import("./parser.js"));
6
+
7
+ run = require("./run");
8
+
9
+ t = require("./codes");
10
+
11
+ finalize = function(codes) {
12
+ var representatives;
13
+ representatives = t.minimize(codes).representatives;
14
+ codes = t.normalizeAll(codes, representatives);
15
+ return {codes, representatives};
16
+ };
17
+
18
+ compile = function(script) {
19
+ return (function({defs, exp}) {
20
+ var codes, representatives;
21
+ // console.log defs
22
+ ({codes, representatives} = finalize(defs.codes));
23
+ // console.log {codes, representatives}
24
+ run.defs = {
25
+ codes,
26
+ representatives,
27
+ rels: defs.rels
28
+ };
29
+ return function(data) {
30
+ return run(exp, data);
31
+ };
32
+ })(parse(script));
33
+ };
34
+
35
+ compile.doc = "Transforms k-script (string) into a function";
36
+
37
+ runScriptOnData = function(script, data) {
38
+ return compile(script)(data);
39
+ };
40
+
41
+ runScriptOnData.doc = "Run 'script' (string) on 'data': (script,data) -> data";
42
+
43
+ module.exports = {
44
+ compile,
45
+ run: runScriptOnData,
46
+ parse
47
+ };
48
+
49
+ }).call(this);
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "k-language for JSON-like data transformation",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "prepare": "coffee -c symbol-table.coffee; jison parser.jison",
7
+ "prepare": "coffee -c symbol-table.coffee; jiwson parser.jison",
8
8
  "prepublishOnly": "coffee -c *.coffee; echo '#!/usr/bin/env node' | cat - k.js > k.sh; echo '#!/usr/bin/env node' | cat - repl.js > repl.sh;",
9
9
  "all": "coffee -c *.coffee; npm run prepare; npm run prepublishOnly",
10
- "test": "coffee -c symbol-table.coffee; jison parser.jison; coffee test.coffee"
10
+ "test": "coffee -c symbol-table.coffee; jiwson parser.jison; coffee test.coffee"
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
@@ -21,6 +21,6 @@
21
21
  },
22
22
  "devDependencies": {
23
23
  "coffeescript": "^2.7.0",
24
- "jison": "^0.4.18"
24
+ "jiwson": "^0.0.1"
25
25
  }
26
26
  }
package/parser.js CHANGED
@@ -1,4 +1,4 @@
1
- /* parser generated by jison 0.4.18 */
1
+ /* parser generated by jison 0.0.1 */
2
2
  /*
3
3
  Returns a Parser object of the following structure:
4
4
 
@@ -71,6 +71,13 @@
71
71
  recoverable: (boolean: TRUE when the parser has a error recovery rule available for this particular error)
72
72
  }
73
73
  */
74
+
75
+
76
+
77
+ var s = require("./symbol-table");
78
+
79
+
80
+
74
81
  var parser = (function(){
75
82
  var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[4,6,8,15,18,21,23,32,34],$V1=[1,7],$V2=[1,15],$V3=[1,16],$V4=[1,8],$V5=[1,9],$V6=[1,10],$V7=[1,11],$V8=[1,14],$V9=[1,20],$Va=[4,6,8,12,15,18,21,23,32,34],$Vb=[2,28],$Vc=[1,25],$Vd=[1,26],$Ve=[1,27],$Vf=[4,6,8,12,14,15,18,20,21,22,23,24,27,32,33,34],$Vg=[2,47],$Vh=[2,31],$Vi=[2,32],$Vj=[4,6,8,12,14,15,18,20,21,22,23,24,27,28,32,33,34],$Vk=[2,8],$Vl=[2,13],$Vm=[20,27],$Vn=[22,24,27],$Vo=[20,24,27],$Vp=[2,41],$Vq=[2,42],$Vr=[2,43];
76
83
  var parser = {trace: function trace () { },
@@ -344,13 +351,7 @@ parse: function parse(input) {
344
351
  }
345
352
  }
346
353
  return true;
347
- }};
348
-
349
-
350
- var s = require("./symbol-table");
351
-
352
-
353
- /* generated by jison-lex 0.3.4 */
354
+ }};/* generated by jison-lex */
354
355
  var lexer = (function(){
355
356
  var lexer = ({
356
357
 
package/repl.coffee CHANGED
@@ -2,6 +2,7 @@
2
2
  k = require "./"
3
3
  run = require "./run"
4
4
  {prettyCode, prettyRel} = require "./pretty"
5
+ fs = require "fs"
5
6
 
6
7
  console.log "Very! experimental repl shell for 'k-language'..."
7
8
 
@@ -33,7 +34,8 @@ do (val = {}, buffer = []) ->
33
34
  else if line.match re
34
35
  file = line.match(re)[1]
35
36
  console.log "-- loading file: #{file} ..."
36
- val = k.compile_file(file) val
37
+ kScript = fs.readFileSync(file).toString();
38
+ val = k.compile(kScript) val
37
39
  console.log "=> #{JSON.stringify val}"
38
40
  else if line.match /^[ \n\t]*(?:--a)?$/
39
41
  console.log JSON.stringify run.defs, " ", 2
package/repl.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  (function() {
3
3
  //!/usr/bin/env coffee
4
- var help, k, prettyCode, prettyRel, re, run,
4
+ var fs, help, k, prettyCode, prettyRel, re, run,
5
5
  splice = [].splice;
6
6
 
7
7
  k = require("./");
@@ -10,6 +10,8 @@
10
10
 
11
11
  ({prettyCode, prettyRel} = require("./pretty"));
12
12
 
13
+ fs = require("fs");
14
+
13
15
  console.log("Very! experimental repl shell for 'k-language'...");
14
16
 
15
17
  help = function() {
@@ -26,7 +28,7 @@
26
28
 
27
29
  (function(val, buffer) {
28
30
  return process.stdin.on('data', function(data) {
29
- var e, file, first, i, last, len, line, ref, rest, results, todo;
31
+ var e, file, first, i, kScript, last, len, line, ref, rest, results, todo;
30
32
  [first, ...rest] = data.toString("utf8").split("\n");
31
33
  buffer.push(first);
32
34
  if (rest.length > 0) {
@@ -45,7 +47,8 @@
45
47
  } else if (line.match(re)) {
46
48
  file = line.match(re)[1];
47
49
  console.log(`-- loading file: ${file} ...`);
48
- val = k.compile_file(file)(val);
50
+ kScript = fs.readFileSync(file).toString();
51
+ val = k.compile(kScript)(val);
49
52
  results.push(console.log(`=> ${JSON.stringify(val)}`));
50
53
  } else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
51
54
  results.push(console.log(JSON.stringify(run.defs, " ", 2)));
package/repl.sh CHANGED
@@ -2,7 +2,7 @@
2
2
  // Generated by CoffeeScript 2.7.0
3
3
  (function() {
4
4
  //!/usr/bin/env coffee
5
- var help, k, prettyCode, prettyRel, re, run,
5
+ var fs, help, k, prettyCode, prettyRel, re, run,
6
6
  splice = [].splice;
7
7
 
8
8
  k = require("./");
@@ -11,6 +11,8 @@
11
11
 
12
12
  ({prettyCode, prettyRel} = require("./pretty"));
13
13
 
14
+ fs = require("fs");
15
+
14
16
  console.log("Very! experimental repl shell for 'k-language'...");
15
17
 
16
18
  help = function() {
@@ -27,7 +29,7 @@
27
29
 
28
30
  (function(val, buffer) {
29
31
  return process.stdin.on('data', function(data) {
30
- var e, file, first, i, last, len, line, ref, rest, results, todo;
32
+ var e, file, first, i, kScript, last, len, line, ref, rest, results, todo;
31
33
  [first, ...rest] = data.toString("utf8").split("\n");
32
34
  buffer.push(first);
33
35
  if (rest.length > 0) {
@@ -46,7 +48,8 @@
46
48
  } else if (line.match(re)) {
47
49
  file = line.match(re)[1];
48
50
  console.log(`-- loading file: ${file} ...`);
49
- val = k.compile_file(file)(val);
51
+ kScript = fs.readFileSync(file).toString();
52
+ val = k.compile(kScript)(val);
50
53
  results.push(console.log(`=> ${JSON.stringify(val)}`));
51
54
  } else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
52
55
  results.push(console.log(JSON.stringify(run.defs, " ", 2)));
@@ -0,0 +1,128 @@
1
+ // Generated by CoffeeScript 2.7.0
2
+ (function() {
3
+ var add_code, add_rel, as_ref, codes, comp, identity, 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_empty_rel = function(rel) {
23
+ return rel.op === "union" && rel.union.length === 0;
24
+ };
25
+
26
+ is_full_rel = function(rel) {
27
+ switch (rel.op) {
28
+ case "int":
29
+ case "str":
30
+ case "identity":
31
+ return true;
32
+ case "comp":
33
+ return rel.comp.every(is_full_rel);
34
+ case "product":
35
+ case "vector":
36
+ return Object.values(rel[rel.op]).every(is_full_rel);
37
+ default:
38
+ return false;
39
+ }
40
+ };
41
+
42
+ comp = function(e1, e2) {
43
+ if (is_identity_rel(e1)) {
44
+ return e2;
45
+ }
46
+ if (is_identity_rel(e2)) {
47
+ return e1;
48
+ }
49
+ if (is_empty_rel(e1)) {
50
+ return e1;
51
+ }
52
+ if (is_empty_rel(e2)) {
53
+ return e2;
54
+ }
55
+ if (e1.op === "comp" && e2.op === "comp") {
56
+ return {
57
+ op: "comp",
58
+ comp: [].concat(e1.comp, e2.comp)
59
+ };
60
+ }
61
+ if (e1.op === "comp") {
62
+ return {
63
+ op: "comp",
64
+ comp: [].concat(e1.comp, [e2])
65
+ };
66
+ }
67
+ if (e2.op === "comp") {
68
+ return {
69
+ op: "comp",
70
+ comp: [].concat([e1], e2.comp)
71
+ };
72
+ }
73
+ return {
74
+ op: "comp",
75
+ comp: [e1, e2]
76
+ };
77
+ };
78
+
79
+ union = function(rels) {
80
+ var list;
81
+ list = [];
82
+ (function() {
83
+ var i, j, len, len1, new_rels, rel;
84
+ for (i = 0, len = rels.length; i < len; i++) {
85
+ rel = rels[i];
86
+ new_rels = rel.op === "union" ? rel.union : [rel];
87
+ for (j = 0, len1 = new_rels.length; j < len1; j++) {
88
+ rel = new_rels[j];
89
+ list.push(rel);
90
+ if (is_full_rel(rel)) {
91
+ return;
92
+ }
93
+ }
94
+ }
95
+ })();
96
+ if (list.length === 1) {
97
+ return list[0];
98
+ }
99
+ return {
100
+ op: "union",
101
+ union: list
102
+ };
103
+ };
104
+
105
+ as_ref = function(codeExp) {
106
+ var newName;
107
+ if (codeExp.code === "ref") {
108
+ return codeExp.ref;
109
+ }
110
+ newName = `:${Object.keys(codes).length}`;
111
+ codes[newName] = codeExp;
112
+ return newName;
113
+ };
114
+
115
+ add_rel = function(name, rel) {
116
+ if (rels[name] == null) {
117
+ rels[name] = [];
118
+ }
119
+ return rels[name].push(rel);
120
+ };
121
+
122
+ add_code = function(name, code) {
123
+ return codes[name] = code;
124
+ };
125
+
126
+ module.exports = {identity, comp, union, rels, codes, add_rel, add_code, as_ref};
127
+
128
+ }).call(this);
package/test.cjs ADDED
@@ -0,0 +1,162 @@
1
+ // Generated by CoffeeScript 2.7.0
2
+ (function() {
3
+ var k, k_expression, k_fn;
4
+
5
+ k = require("./index.cjs");
6
+ console.log(k);
7
+
8
+ console.log('var k = require("k");');
9
+
10
+ console.log("");
11
+
12
+ [
13
+ ["ANYTHING...",
14
+ '()'],
15
+ ["ANYTHING...",
16
+ '{"ala" name, 23 age}'],
17
+ [
18
+ {
19
+ year: 2002,
20
+ age: 19
21
+ },
22
+ '[.year, .age]'
23
+ ],
24
+ ["duplicate me",
25
+ '[(), ()]'],
26
+ ["nesting",
27
+ '[[[()]]]'],
28
+ ["nesting and accessing",
29
+ '[[()]] {() nested, .0.0 val}'],
30
+ [
31
+ {
32
+ test: "parse integer"
33
+ },
34
+ '0000'
35
+ ],
36
+ [
37
+ {
38
+ x: 3,
39
+ y: 4
40
+ },
41
+ '[.y,.x] PLUS'
42
+ ]
43
+ ].map(function([data, script]) {
44
+ console.log(`k_expression = '${script}';`);
45
+ console.log(`k.run(k_expression,${JSON.stringify(data)});`);
46
+ console.log(`// RETURNS: ${JSON.stringify(k.run(script, data))}`);
47
+ return console.log("");
48
+ });
49
+
50
+ k_expression = '{.name nom, <[.age, 18] GT .0, [.age, 12] GT "ado", "enfant"> age}';
51
+
52
+ k_fn = k.compile(k_expression);
53
+
54
+ console.log(`var k_fn = k.compile('${k_expression}');`);
55
+
56
+ console.log("");
57
+
58
+ [
59
+ {
60
+ age: 23,
61
+ name: "Emily"
62
+ },
63
+ {
64
+ age: 16,
65
+ name: "Katrina"
66
+ },
67
+ {
68
+ age: 2,
69
+ name: "Mark"
70
+ }
71
+ ].map(function(data) {
72
+ console.log(`k_fn(${JSON.stringify(data)});`);
73
+ console.log(`// RETURNS: ${JSON.stringify(k_fn(data))}`);
74
+ return console.log("");
75
+ });
76
+
77
+ k_expression = "$t = < i: int, t: [ t ] > ; <$t, $int>";
78
+
79
+ k_fn = k.compile(k_expression);
80
+
81
+ console.log(`var k_fn = k.compile('${k_expression}');`);
82
+
83
+ console.log("");
84
+
85
+ [
86
+ 1,
87
+ {
88
+ i: 1
89
+ },
90
+ [
91
+ {
92
+ i: 2
93
+ },
94
+ {
95
+ i: 3
96
+ },
97
+ {
98
+ t: []
99
+ }
100
+ ],
101
+ {
102
+ "t": [
103
+ {
104
+ "i": 2
105
+ },
106
+ {
107
+ "i": 3
108
+ },
109
+ {
110
+ "t": []
111
+ }
112
+ ]
113
+ }
114
+ ].map(function(data) {
115
+ console.log(`k_fn(${JSON.stringify(data)});`);
116
+ console.log(`// RETURNS: ${JSON.stringify(k_fn(data))}`);
117
+ return console.log("");
118
+ });
119
+
120
+ k_expression = "$ < < [ int ] ints, [ bool ] bools > list, string None>";
121
+
122
+ k_fn = k.compile(k_expression);
123
+
124
+ console.log(`var k_fn = k.compile('${k_expression}');`);
125
+
126
+ console.log("");
127
+
128
+ [
129
+ {
130
+ None: "None"
131
+ },
132
+ {
133
+ list: {
134
+ ints: []
135
+ }
136
+ },
137
+ {
138
+ list: {
139
+ ints: [1,
140
+ 2,
141
+ 3]
142
+ }
143
+ },
144
+ {
145
+ list: {
146
+ bools: [true,
147
+ false]
148
+ }
149
+ },
150
+ {
151
+ list: {
152
+ bools: [true,
153
+ 0]
154
+ }
155
+ }
156
+ ].map(function(data) {
157
+ console.log(`k_fn(${JSON.stringify(data)});`);
158
+ console.log(`// RETURNS: ${JSON.stringify(k_fn(data))}`);
159
+ return console.log("");
160
+ });
161
+
162
+ }).call(this);