@fraczak/k 3.0.2 → 3.1.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/BIG_PICTURE.md +15 -6
- package/Code-derivation-tests/blowup.mjs +27 -0
- package/Code-derivation-tests/bnat.mjs +4 -2
- package/Code-derivation-tests/built-in.mjs +1 -1
- package/Code-derivation-tests/dot.mjs +28 -3
- package/Code-derivation-tests/index.mjs +2 -2
- package/Code-derivation-tests/loop.mjs +1 -1
- package/Code-derivation-tests/nat.mjs +2 -2
- package/Code-derivation-tests/not-not.mjs +2 -2
- package/Code-derivation-tests/product.mjs +9 -5
- package/Code-derivation-tests/rlz.mjs +2 -2
- package/Code-derivation-tests/vector.mjs +8 -14
- package/Graph.mjs +95 -0
- package/TYPING.md +368 -0
- package/aaa.txt +203033 -0
- package/codes.mjs +60 -8
- package/hash.mjs +6 -5
- package/index.mjs +7 -38
- package/k.mjs +3 -3
- package/package.json +1 -1
- package/parser.jison +14 -6
- package/parser.mjs +68 -62
- package/patterns.mjs +386 -534
- package/repl.mjs +13 -7
- package/run.mjs +7 -5
- package/symbol-table.mjs +44 -41
- package/test-filters.mjs +3 -3
- package/typing-test.mjs +40 -0
- package/typing.mjs +528 -0
- package/TODO.md +0 -54
package/repl.mjs
CHANGED
|
@@ -15,7 +15,8 @@ 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
|
-
console.log(" --
|
|
18
|
+
console.log(" --p pretty-print last value");
|
|
19
|
+
console.log(" --pp pretty-print last value (abrevaited)");
|
|
19
20
|
console.log(" --s reg store the last value in register 'reg'");
|
|
20
21
|
console.log(" --g reg get the value from register 'reg'");
|
|
21
22
|
console.log(" --regs print registers names");
|
|
@@ -56,7 +57,9 @@ const registers = {};
|
|
|
56
57
|
file = line.match(re__l)[1];
|
|
57
58
|
console.log(` -- loading file: ${file} ...`);
|
|
58
59
|
kScript = fs.readFileSync(file).toString();
|
|
59
|
-
|
|
60
|
+
console.log(` ----------------------------- compiling file: ${file} ...`);
|
|
61
|
+
// console.log(kScript);
|
|
62
|
+
val = k.compile("+++" + kScript + "\n()")(val);
|
|
60
63
|
console.log(`=> ${JSON.stringify(val)}`);
|
|
61
64
|
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
62
65
|
// --a
|
|
@@ -69,11 +72,10 @@ const registers = {};
|
|
|
69
72
|
return result;
|
|
70
73
|
}
|
|
71
74
|
return (function (prettyRel) {
|
|
72
|
-
var ref1,
|
|
75
|
+
var ref1, relName;
|
|
73
76
|
ref1 = defs.rels;
|
|
74
77
|
for (relName in ref1) {
|
|
75
|
-
|
|
76
|
-
result[relName] = relExps.map(prettyRel);
|
|
78
|
+
result[relName] = prettyRel(ref1[relName].def);
|
|
77
79
|
}
|
|
78
80
|
return result;
|
|
79
81
|
})(
|
|
@@ -102,10 +104,14 @@ const registers = {};
|
|
|
102
104
|
return result;
|
|
103
105
|
})(run.defs, {})
|
|
104
106
|
);
|
|
105
|
-
// --
|
|
107
|
+
// --pp
|
|
106
108
|
} else if (line.match(/^[ \n\t]*(?:--pp)?$/)) {
|
|
107
109
|
console.log(val);
|
|
108
110
|
// --s
|
|
111
|
+
// --p
|
|
112
|
+
} else if (line.match(/^[ \n\t]*(?:--p)?$/)) {
|
|
113
|
+
console.log(JSON.stringify(val, null, 2));
|
|
114
|
+
// --s
|
|
109
115
|
} else if (line.match(/^[ \n\t]*(?:--regs)?$/)) {
|
|
110
116
|
console.log(` -- registers: ${Object.keys(registers).join(", ")}`);
|
|
111
117
|
} else if (line.match(re__s)) {
|
|
@@ -121,7 +127,7 @@ const registers = {};
|
|
|
121
127
|
// ------ k code
|
|
122
128
|
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
123
129
|
try {
|
|
124
|
-
val = k.run(
|
|
130
|
+
val = k.run(`+++ ${line} ()`, val);
|
|
125
131
|
console.log(`=> ${JSON.stringify(val)}`);
|
|
126
132
|
} catch (error) {
|
|
127
133
|
e = error;
|
package/run.mjs
CHANGED
|
@@ -54,13 +54,14 @@ const builtin = {
|
|
|
54
54
|
},
|
|
55
55
|
FDIV: ([x, y]) => x / y,
|
|
56
56
|
CONCAT: (strs) => strs.join(""),
|
|
57
|
+
toVEC: (str) => [...str],
|
|
57
58
|
true: () => true,
|
|
58
59
|
false: () => false,
|
|
59
60
|
null: () => null,
|
|
60
61
|
toJSON: (x) => JSON.stringify(x),
|
|
61
62
|
fromJSON: (x) => JSON.parse(x),
|
|
62
|
-
CONS: (
|
|
63
|
-
SNOC: (x) => (x.length > 1 ?
|
|
63
|
+
CONS: ({car, cdr}) => { try { return [car, ...cdr]; } catch (e) { return undefined; } },
|
|
64
|
+
SNOC: (x) => (x.length > 1 ? {car: x[0], cdr: x.slice(1)} : undefined),
|
|
64
65
|
toDateMsec: (x) => new Date(x).getTime(),
|
|
65
66
|
toDateStr: (x) => new Date(x).toISOString(),
|
|
66
67
|
};
|
|
@@ -131,10 +132,10 @@ function run(exp, value) {
|
|
|
131
132
|
case "str":
|
|
132
133
|
case "int":
|
|
133
134
|
return exp[exp.op];
|
|
134
|
-
case "ref":
|
|
135
|
+
case "ref": {
|
|
135
136
|
const defn = run.defs.rels[exp.ref];
|
|
136
|
-
if (defn !=
|
|
137
|
-
exp = defn
|
|
137
|
+
if (defn != undefined) {
|
|
138
|
+
exp = defn.def;
|
|
138
139
|
continue;
|
|
139
140
|
}
|
|
140
141
|
const builtin_func = builtin[exp.ref];
|
|
@@ -142,6 +143,7 @@ function run(exp, value) {
|
|
|
142
143
|
return builtin_func(value);
|
|
143
144
|
}
|
|
144
145
|
throw(`Unknown ref: '${exp.ref}'`);
|
|
146
|
+
}
|
|
145
147
|
case "dot":
|
|
146
148
|
// a hack to allow something like 'null . null' or '0 . 0' to work by returning unit
|
|
147
149
|
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
package/symbol-table.mjs
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
},
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const identity = {
|
|
10
|
-
op: "identity",
|
|
11
|
-
};
|
|
1
|
+
import hash from "./hash.mjs";
|
|
2
|
+
|
|
3
|
+
const unitCode = hash('$C0={};');
|
|
4
|
+
|
|
5
|
+
const identity = {op: "identity"}
|
|
12
6
|
|
|
13
7
|
function is_identity_rel(rel) {
|
|
14
8
|
return rel.op === "identity";
|
|
@@ -46,7 +40,9 @@ function is_full_rel(rel) {
|
|
|
46
40
|
case "vector":
|
|
47
41
|
return Object.values(rel[rel.op]).every(is_full_rel);
|
|
48
42
|
case "product":
|
|
49
|
-
return Object.values(rel[rel.op]).every(({ exp }) =>
|
|
43
|
+
return Object.values(rel[rel.op]).every(({ exp }) =>
|
|
44
|
+
is_full_rel(exp)
|
|
45
|
+
);
|
|
50
46
|
default:
|
|
51
47
|
return false;
|
|
52
48
|
}
|
|
@@ -81,7 +77,7 @@ function comp_first(e1, e2) {
|
|
|
81
77
|
};
|
|
82
78
|
}
|
|
83
79
|
|
|
84
|
-
function comp
|
|
80
|
+
function comp(e1, e2) {
|
|
85
81
|
const result = comp_first(e1, e2);
|
|
86
82
|
if (result.op !== "comp") return result;
|
|
87
83
|
result.comp = result.comp.reduceRight(
|
|
@@ -91,7 +87,8 @@ function comp (e1, e2) {
|
|
|
91
87
|
return result;
|
|
92
88
|
}
|
|
93
89
|
|
|
94
|
-
|
|
90
|
+
|
|
91
|
+
function union(rels) {
|
|
95
92
|
const list = [];
|
|
96
93
|
label: for (const rel of rels) {
|
|
97
94
|
const new_rels = rel.op === "union" ? rel.union : [rel];
|
|
@@ -105,32 +102,38 @@ function union (rels) {
|
|
|
105
102
|
op: "union",
|
|
106
103
|
union: list,
|
|
107
104
|
};
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
function as_ref (codeExp) {
|
|
111
|
-
if (codeExp.code === "ref") return codeExp.ref;
|
|
112
|
-
const newName = `:${Object.keys(codes).length}`;
|
|
113
|
-
codes[newName] = codeExp;
|
|
114
|
-
return newName;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
function add_rel (name, rel) {
|
|
118
|
-
if (rels[name] == null) rels[name] = [];
|
|
119
|
-
rels[name].push(rel);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
function add_code (name, code) {
|
|
123
|
-
codes[name] = code;
|
|
124
105
|
}
|
|
125
106
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
107
|
+
class SymbolTable {
|
|
108
|
+
constructor() {
|
|
109
|
+
this.rels = {}; // name -> {def: rel_exp}
|
|
110
|
+
this.codes = {
|
|
111
|
+
[unitCode]: {
|
|
112
|
+
code: "product",
|
|
113
|
+
product: {},
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
as_ref(codeExp) {
|
|
119
|
+
if (codeExp.code === "ref") return codeExp.ref;
|
|
120
|
+
const newName = `:${Object.keys(this.codes).length}`;
|
|
121
|
+
this.codes[newName] = codeExp;
|
|
122
|
+
return newName;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
add_rel(name, rel) {
|
|
126
|
+
if (this.rels[name] != undefined)
|
|
127
|
+
console.error(`SymbolTable: rel ${name} already defined (lines ${rel.start?.line}:${rel.start?.column}...${rel.end?.line}:${rel.end?.column})`);
|
|
128
|
+
|
|
129
|
+
this.rels[name] = {def: rel};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
add_code(name, code) {
|
|
133
|
+
this.codes[name] = code;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export default { SymbolTable, comp, union, identity };
|
|
138
|
+
export { SymbolTable, comp, union, identity };
|
|
139
|
+
|
package/test-filters.mjs
CHANGED
|
@@ -23,7 +23,7 @@ const annotated = k.annotate(`
|
|
|
23
23
|
const test1 = unify_with_code(
|
|
24
24
|
annotated.codes,
|
|
25
25
|
annotated.representatives["pair"],
|
|
26
|
-
annotated.rels['f1']
|
|
26
|
+
annotated.rels['f1'].def.filter,
|
|
27
27
|
{}
|
|
28
28
|
);
|
|
29
29
|
console.log(JSON.stringify(test1, null, 2));
|
|
@@ -31,7 +31,7 @@ console.log(JSON.stringify(test1, null, 2));
|
|
|
31
31
|
const test2 = unify_with_code(
|
|
32
32
|
annotated.codes,
|
|
33
33
|
annotated.representatives["pair"],
|
|
34
|
-
annotated.rels['f2']
|
|
34
|
+
annotated.rels['f2'].def.filter,
|
|
35
35
|
{}
|
|
36
36
|
);
|
|
37
37
|
console.log(JSON.stringify(test2, null, 2));
|
|
@@ -39,7 +39,7 @@ console.log(JSON.stringify(test2, null, 2));
|
|
|
39
39
|
const test3 = unify_with_code(
|
|
40
40
|
annotated.codes,
|
|
41
41
|
annotated.representatives["list"],
|
|
42
|
-
annotated.rels['f3']
|
|
42
|
+
annotated.rels['f3'].def.filter,
|
|
43
43
|
{}
|
|
44
44
|
);
|
|
45
45
|
console.log(JSON.stringify(test3, null, 2));
|
package/typing-test.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import t from './typing.mjs';
|
|
2
|
+
// const t = await import('./typing.mjs');
|
|
3
|
+
const g = new t.typePatternGraph();
|
|
4
|
+
|
|
5
|
+
const pu = {pattern: '(...)', fields: []};
|
|
6
|
+
|
|
7
|
+
// x = .x; { { 5 x } x i, { true x } x b }
|
|
8
|
+
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
|
9
|
+
|
|
10
|
+
for (var i = 0; i < 16; i++) { g.addNewNode(pu); }
|
|
11
|
+
|
|
12
|
+
g.addNewNode({pattern: '(...)'}, {x: [1]}); // 16
|
|
13
|
+
g.unify("y:input", 0,16); // 17
|
|
14
|
+
|
|
15
|
+
//
|
|
16
|
+
g.unify('main:input',2,3,4,9,10); // 18
|
|
17
|
+
|
|
18
|
+
g.addNewNode({pattern: '{}'}, {i: [8], b: [14]}); // 19
|
|
19
|
+
g.unify("main:output", 15,19); // 20
|
|
20
|
+
|
|
21
|
+
g.unify('comp:chain',6,7); // 21
|
|
22
|
+
g.unify('comp:chain',12,13); // 22
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
// {5 x}
|
|
26
|
+
g.addNewNode({pattern: '<...>'}, {x: [5]}); // 23
|
|
27
|
+
g.unify('variant:output',6,23); // 24
|
|
28
|
+
|
|
29
|
+
// {true x}
|
|
30
|
+
g.addNewNode({pattern: '<...>'}, {x: [11]}); // 25
|
|
31
|
+
g.unify('variant:output',12,25); // 26
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// 5
|
|
35
|
+
g.addNewNode({pattern: 'type', type: 'int'}); // 27
|
|
36
|
+
g.unify('5:outout',5,27); // 28
|
|
37
|
+
|
|
38
|
+
// true
|
|
39
|
+
g.addNewNode({pattern: 'type', type: 'bool'}); // 29
|
|
40
|
+
g.unify('true:output',11,29); // 30
|