@fraczak/k 2.1.3 → 2.1.5

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.
Files changed (4) hide show
  1. package/README.md +5 -6
  2. package/package.json +1 -1
  3. package/repl.mjs +20 -19
  4. package/run.mjs +1 -0
package/README.md CHANGED
@@ -185,6 +185,7 @@ can be written as `[.toto, .titi, 123]`.
185
185
 
186
186
  _Codes_ (prefixed by `$`) can be defined by taged union and product. E.g.:
187
187
 
188
+
188
189
  $nat = <nat 1, {} 0>;
189
190
  $pair = {nat x, nat y};
190
191
 
@@ -217,7 +218,7 @@ i.e., turn into a regular vector using `CARET` (`^`) operator. E.g.:
217
218
  [1,2,3] --> []
218
219
  ```
219
220
 
220
- The PIPE operator can be used, e.g., to define a Cartesian product:
221
+ The PIPE operator can be used for defining the Cartesian product:
221
222
 
222
223
  ```k
223
224
  [.0 |, .1 |] ^ :
@@ -241,13 +242,11 @@ times value `x` appears in the list. (see `Examples/list-comprehension.k` for a
241
242
  $ int;
242
243
  ```
243
244
 
244
- **WARNING**: The `CARET` operator makes composition not associative, i.e., if you use
245
- nested _list comprehension_ you cannot drop paranthesis anymore.
246
- E.g., `| (| PLUS ^ .0) ^` and `| | PLUS ^ .0 ^` are two different functions:
245
+ **WARNING**: When using `CARET` operator paranthesis may be required, e.g., like in:
247
246
 
248
247
  ```k
249
- { |(| PLUS ^ .0) ^ x, | | PLUS ^ .0 ^ y }:
250
- [[[1,2],[3,4]],[[5,6],[7,8]]] --> {"x":[3,11],"y":[3,7]}
248
+ | ( [|,|] ^ ) ^ :
249
+ [[1,2],[3,4]] --> [[[1,1],[1,2],[2,1],[2,2]],[[3,3],[3,4],[4,3],[4,4]]]
251
250
  ```
252
251
 
253
252
  ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
4
4
  "description": "k-language for JSON-like data transformation",
5
5
  "main": "index.mjs",
6
6
  "scripts": {
package/repl.mjs CHANGED
@@ -46,30 +46,31 @@ const re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
46
46
  file = line.match(re)[1];
47
47
  console.log(`-- loading file: ${file} ...`);
48
48
  kScript = fs.readFileSync(file).toString();
49
- val = k.compile(kScript)(val);
49
+ val = k.compile(kScript + "\n()")(val);
50
50
  console.log(`=> ${JSON.stringify(val)}`);
51
51
  } else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
52
52
  console.log(JSON.stringify(run.defs, " ", 2));
53
53
  } else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
54
- (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);
54
+ console.log(
55
+ (function (defs, result) {
56
+ if (defs == null) {
57
+ return result;
64
58
  }
65
- return result;
66
- })(
67
- prettyRel.bind(
68
- null,
69
- prettyCode.bind(null, defs.codes, defs.representatives)
70
- )
71
- );
72
- })(run.defs, {})
59
+ return (function (prettyRel) {
60
+ var ref1, relExps, relName;
61
+ ref1 = defs.rels;
62
+ for (relName in ref1) {
63
+ relExps = ref1[relName];
64
+ result[relName] = relExps.map(prettyRel);
65
+ }
66
+ return result;
67
+ })(
68
+ prettyRel.bind(
69
+ null,
70
+ prettyCode.bind(null, defs.codes, defs.representatives)
71
+ )
72
+ );
73
+ })(run.defs, {}));
73
74
  } else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
74
75
  console.log(
75
76
  (function (defs, result) {
package/run.mjs CHANGED
@@ -164,6 +164,7 @@ function run(exp, value) {
164
164
  exp = exp.comp[exp.comp.length - 1];
165
165
  continue;
166
166
  case "union":
167
+ if (exp.union.length === 0) return;
167
168
  for (let i = 0, len = exp.union.length -1; i < len; i++) {
168
169
  const result = run(exp.union[i], value);
169
170
  if (result !== undefined) {