@fraczak/k 2.1.6 → 3.0.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/repl.mjs CHANGED
@@ -15,12 +15,20 @@ const help = function () {
15
15
  console.log(" --a print codes and relations");
16
16
  console.log(" --c print codes");
17
17
  console.log(" --r print rels");
18
- return console.log(" --l file.k load 'file.k'");
18
+ console.log(" --pp pretty-print last value");
19
+ console.log(" --s reg store the last value in register 'reg'");
20
+ console.log(" --g reg get the value from register 'reg'");
21
+ console.log(" --regs print registers names");
22
+ console.log(" --l file.k load 'file.k'");
19
23
  };
20
24
 
21
25
  help();
22
26
 
23
- const re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
27
+ const re__l = /^[ \n\t]*(?:--l[ ]+)(.+)[ ]*?$/;
28
+ const re__s = /^[ \n\t]*(?:--s[ ]+)(.+)[ ]*?$/;
29
+ const re__g = /^[ \n\t]*(?:--g[ ]+)(.+)[ ]*?$/;
30
+
31
+ const registers = {};
24
32
 
25
33
  (function (val, buffer) {
26
34
  return process.stdin.on("data", function (data) {
@@ -38,18 +46,22 @@ const re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
38
46
  val = {};
39
47
  }
40
48
  if (line.match(/^[ \n\t]*(?:--h)?$/)) {
49
+ // --h
41
50
  help();
42
51
  } else if (line.match(/^[ \n\t]*\^$/)) {
43
52
  val = closeVector(val);
44
53
  console.log(`=> ${JSON.stringify(val)}`);
45
- } else if (line.match(re)) {
46
- file = line.match(re)[1];
47
- console.log(`-- loading file: ${file} ...`);
54
+ } else if (line.match(re__l)) {
55
+ // --l
56
+ file = line.match(re__l)[1];
57
+ console.log(` -- loading file: ${file} ...`);
48
58
  kScript = fs.readFileSync(file).toString();
49
59
  val = k.compile(kScript + "\n()")(val);
50
60
  console.log(`=> ${JSON.stringify(val)}`);
51
61
  } else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
52
- console.log(JSON.stringify(run.defs, " ", 2));
62
+ // --a
63
+ val = run.defs;
64
+ console.log(val);
53
65
  } else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
54
66
  console.log(
55
67
  (function (defs, result) {
@@ -90,6 +102,23 @@ const re = /^[ \n\t]*(?:--l[ ]+)(.+[^ ])[ ]*?$/;
90
102
  return result;
91
103
  })(run.defs, {})
92
104
  );
105
+ // --p
106
+ } else if (line.match(/^[ \n\t]*(?:--pp)?$/)) {
107
+ console.log(val);
108
+ // --s
109
+ } else if (line.match(/^[ \n\t]*(?:--regs)?$/)) {
110
+ console.log(` -- registers: ${Object.keys(registers).join(", ")}`);
111
+ } else if (line.match(re__s)) {
112
+ let reg = line.match(re__s)[1];
113
+ registers[reg] = val;
114
+ console.log(` -- current value stored in register: '${reg}'`);
115
+ // --g
116
+ } else if (line.match(re__g)) {
117
+ console.log(" -- getting value from register...");
118
+ let reg = line.match(re__g)[1];
119
+ val = registers[reg];
120
+ console.log(val);
121
+ // ------ k code
93
122
  } else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
94
123
  try {
95
124
  val = k.run(`${line} ()`, val);
package/run.mjs CHANGED
@@ -59,7 +59,7 @@ const builtin = {
59
59
  null: () => null,
60
60
  toJSON: (x) => JSON.stringify(x),
61
61
  fromJSON: (x) => JSON.parse(x),
62
- CONS: ([x, y]) => { try { return [x, ...y]; } catch (e) { return undefined; } },
62
+ CONS: (arg) => { try { return [arg[0], ...arg[1]]; } catch (e) { return undefined; } },
63
63
  SNOC: (x) => (x.length > 1 ? [x[0], x.slice(1)] : undefined),
64
64
  toDateMsec: (x) => new Date(x).getTime(),
65
65
  toDateStr: (x) => new Date(x).toISOString(),
@@ -254,6 +254,9 @@ function run(exp, value) {
254
254
  assert(isOpen(result), "CARET (^): Only an 'open' vector can be closed.");
255
255
  return [...result];
256
256
  }
257
+ case "filter": {
258
+ return value;
259
+ }
257
260
  default:
258
261
  assert(false,`Unknown operation: '${exp.op}'`);
259
262
 
package/symbol-table.mjs CHANGED
@@ -97,7 +97,7 @@ function union (rels) {
97
97
  const new_rels = rel.op === "union" ? rel.union : [rel];
98
98
  for (const new_rel of new_rels) {
99
99
  list.push(new_rel);
100
- if (is_full_rel(new_rel)) break label;
100
+ // if (is_full_rel(new_rel)) break label;
101
101
  }
102
102
  }
103
103
  if (list.length === 1) return list[0];
@@ -0,0 +1,45 @@
1
+ import { unify_with_code } from './filters.mjs';
2
+ import k from './index.mjs';
3
+ // { compile, run, parse, annotate };
4
+
5
+ const annotated = k.annotate(`
6
+ $nat = < {} _, nat s >;
7
+ $pair = { nat x, nat y };
8
+ $list = < {} empty, { nat x, list xs } list>;
9
+ z = {{} _} $nat;
10
+ s = { $nat s} $nat;
11
+ f1 = ? { n x, ...};
12
+ f2 = ? { <...> y, ...};
13
+ f3 = ? < x empty, { $MWqNxWxHMy xs, ...} list >;
14
+ {}
15
+ `);
16
+
17
+
18
+
19
+ // console.log(JSON.stringify(annotated, null, 2));
20
+
21
+
22
+
23
+ const test1 = unify_with_code(
24
+ annotated.codes,
25
+ annotated.representatives["pair"],
26
+ annotated.rels['f1'][0].filter,
27
+ {}
28
+ );
29
+ console.log(JSON.stringify(test1, null, 2));
30
+
31
+ const test2 = unify_with_code(
32
+ annotated.codes,
33
+ annotated.representatives["pair"],
34
+ annotated.rels['f2'][0].filter,
35
+ {}
36
+ );
37
+ console.log(JSON.stringify(test2, null, 2));
38
+
39
+ const test3 = unify_with_code(
40
+ annotated.codes,
41
+ annotated.representatives["list"],
42
+ annotated.rels['f3'][0].filter,
43
+ {}
44
+ );
45
+ console.log(JSON.stringify(test3, null, 2));
@@ -0,0 +1,38 @@
1
+ import k from './index.mjs';
2
+ import { encodeCodeToString } from './codes.mjs';
3
+ import hash from './hash.mjs';
4
+ [
5
+ `
6
+ $nat = < {} zero, nat succ>;
7
+ $pair = {nat x, nat y};
8
+ $b = {int"x", bool y};
9
+ $vector = [int];
10
+ $tree = <{nat value, tree left, tree right} binary, {nat value} leaf, {nat value, tree tree} unary, [tree] list_of_trees>;
11
+
12
+ succ = { $nat succ} $nat;
13
+ plus = $pair <
14
+ {.x.zero stop, .y result} .result,
15
+ {.x.succ x, .y succ y } plus
16
+ > $nat;
17
+ {{} zero} succ succ {() x, () y} plus
18
+ `
19
+ ].map(function (script) {
20
+ console.log(`k_expression = '${script}';`);
21
+ const annotated = k.annotate(script);
22
+ for (const code in annotated.codes) {
23
+ console.log(`CODE: ${code}`);
24
+ const s = encodeCodeToString(code,annotated.codes);
25
+ console.log(` - DEFS: ${s}`);;
26
+ const annotated2 = k.annotate(s+" {}");;
27
+ const s2 = encodeCodeToString(annotated2.representatives["C0"],annotated2.codes);
28
+ console.log(` - reparsed FINGERPRINT: ${hash(s2)}`);
29
+ if ((s === s2) && (hash(s) === hash(s2))) {
30
+ console.log('--- OK');
31
+ } else {
32
+ console.log('--- ERROR');
33
+ }
34
+
35
+ console.log('-------------------');
36
+ }
37
+ });
38
+
package/test.mjs CHANGED
@@ -34,7 +34,7 @@ import k from "./index.mjs";
34
34
  });
35
35
 
36
36
  let k_expression =
37
- '{.name nom, <[.age, 18] GT .0, [.age, 12] GT "ado", "enfant"> age}';
37
+ '{.name nom, <[.age, 18] GT "old", [.age, 12] GT "ado", "enfant"> age}';
38
38
 
39
39
  let k_fn = k.compile(k_expression);
40
40
 
@@ -61,48 +61,6 @@ console.log("");
61
61
  return console.log("");
62
62
  });
63
63
 
64
- k_expression = "$t = < i: int, t: [ t ] > ; <$t, $int>";
65
-
66
- k_fn = k.compile(k_expression);
67
-
68
- console.log(`var k_fn = k.compile('${k_expression}');`);
69
-
70
- console.log("");
71
-
72
- [
73
- 1,
74
- {
75
- i: 1,
76
- },
77
- [
78
- {
79
- i: 2,
80
- },
81
- {
82
- i: 3,
83
- },
84
- {
85
- t: [],
86
- },
87
- ],
88
- {
89
- t: [
90
- {
91
- i: 2,
92
- },
93
- {
94
- i: 3,
95
- },
96
- {
97
- t: [],
98
- },
99
- ],
100
- },
101
- ].map(function (data) {
102
- console.log(`k_fn(${JSON.stringify(data)});`);
103
- console.log(`// RETURNS: ${JSON.stringify(k_fn(data))}`);
104
- return console.log("");
105
- });
106
64
 
107
65
  k_expression = "$ < < [ int ] ints, [ bool ] bools > list, string None>";
108
66