@fraczak/k 4.0.4 → 5.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/BIG_PICTURE.md +28 -21
- package/TODO.md +18 -0
- package/TYPING.md +2 -2
- package/export.mjs +177 -0
- package/index.mjs +3 -15
- package/package.json +1 -1
- package/patterns.mjs +22 -8
- package/pretty.mjs +26 -41
- package/repl.mjs +45 -25
- package/run.mjs +2 -0
- package/k-repl.cast~ +0 -1939
package/repl.mjs
CHANGED
|
@@ -6,14 +6,17 @@ import k from "./index.mjs";
|
|
|
6
6
|
import {run, closeVector } from "./run.mjs";
|
|
7
7
|
import { prettyCode, prettyRel, patterns2filters } from "./pretty.mjs";
|
|
8
8
|
import { find } from "./codes.mjs";
|
|
9
|
+
import { exportRelation } from "./export.mjs";
|
|
9
10
|
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
let DEBUG_FLAG = false;
|
|
12
13
|
|
|
13
14
|
console.log("Very! experimental repl shell for 'k-language'...");
|
|
14
15
|
|
|
15
16
|
const help = function () {
|
|
16
|
-
|
|
17
|
+
console.log(` --debug toggle debug flag: [${DEBUG_FLAG?'ON':'OFF'}]`);
|
|
18
|
+
if (DEBUG_FLAG)
|
|
19
|
+
console.log(" --load loads codes and relations as value");
|
|
17
20
|
console.log(" --c print aliased codes");
|
|
18
21
|
console.log(" --C code print 'code' definition");
|
|
19
22
|
console.log(" --r print relations");
|
|
@@ -88,40 +91,42 @@ function evaluate(line) {
|
|
|
88
91
|
// console.log(kScript);
|
|
89
92
|
val = k.compile("+++\n" + kScript + "\n()")(val);
|
|
90
93
|
printVal();
|
|
91
|
-
} else if (line.match(/^[ \n\t]*--debug$/)) {
|
|
92
94
|
// --debug
|
|
95
|
+
} else if (line.match(/^[ \n\t]*--debug$/)) {
|
|
96
|
+
DEBUG_FLAG = ! DEBUG_FLAG;
|
|
97
|
+
// --load
|
|
98
|
+
} else if (line.match(/^[ \n\t]*--load$/)) {
|
|
93
99
|
val = run.defs;
|
|
94
100
|
console.log(val);
|
|
95
101
|
// --r
|
|
96
102
|
} else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
|
|
97
|
-
console.log(
|
|
103
|
+
console.log("BUILT-IN:", run.builtin);
|
|
104
|
+
console.log("USER-DEFINED:",
|
|
98
105
|
(function (defs, result) {
|
|
99
106
|
if (defs == null) {
|
|
100
107
|
return result;
|
|
101
108
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
})(
|
|
109
|
-
prettyRel.bind(
|
|
110
|
-
null,
|
|
111
|
-
prettyCode.bind(null, defs.representatives)
|
|
112
|
-
)
|
|
113
|
-
);
|
|
109
|
+
|
|
110
|
+
for (const relName in defs.rels) {
|
|
111
|
+
if (relName != "__main__")
|
|
112
|
+
result[relName] = prettyRel(defs.rels[relName].def);
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
114
115
|
})(run.defs, {}));
|
|
115
116
|
// --c
|
|
116
117
|
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
117
118
|
console.log(
|
|
118
119
|
(function (defs, result) {
|
|
119
120
|
for (const codeName of Object.keys(defs.representatives || {})) {
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
121
|
+
const codeRep = defs.representatives[codeName] || codeName;
|
|
122
|
+
if (codeName == codeRep) {
|
|
123
|
+
result[codeName] = prettyCode(
|
|
124
|
+
defs.representatives,
|
|
125
|
+
find(codeRep)
|
|
126
|
+
);
|
|
127
|
+
} else if (! codeName.startsWith(":")) {
|
|
128
|
+
result[codeName] = codeRep;
|
|
129
|
+
}
|
|
125
130
|
}
|
|
126
131
|
return result;
|
|
127
132
|
})(run.defs || {}, {})
|
|
@@ -161,18 +166,33 @@ function evaluate(line) {
|
|
|
161
166
|
const relName = line.match(re__R)[1];
|
|
162
167
|
const rel = run.defs?.rels[relName];
|
|
163
168
|
if (!rel) {
|
|
169
|
+
if (relName in run.builtin) {
|
|
170
|
+
console.log(` -- builtin relation: ${relName}\n ${relName} = ${run.builtin[relName]}`);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
164
173
|
console.log(` -- relation '${relName}' not found`);
|
|
165
174
|
return;
|
|
166
175
|
}
|
|
176
|
+
const canonicalRelName = run.defs.relAlias[relName];
|
|
167
177
|
const filters = patterns2filters(rel.typePatternGraph, ...rel.def.patterns);
|
|
168
178
|
// console.log(filters);
|
|
169
179
|
// console.log(" variables:", JSON.stringify(variables));
|
|
170
180
|
// console.log(JSON.stringify({filters, variables}, null, 2));
|
|
171
181
|
// for (const filter of filters) {
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
console.log(` ${relName} : ${filtersStr[0]} --> ${filtersStr[1]}`);
|
|
175
|
-
console.log(` ${relName} = ${prettyRel(
|
|
182
|
+
const filtersStr = filters.map( x => prettyRel( {op: "filter", filter: x}));
|
|
183
|
+
// console.log(` -- canonical relation name: ${canonicalRelName} --`);
|
|
184
|
+
// console.log(` ${relName} : ${filtersStr[0]} --> ${filtersStr[1]}`);
|
|
185
|
+
// console.log(` ${relName} = ${prettyRel(rel.def)};`);
|
|
186
|
+
// console.log(` ${relName} = ${prettyRel(rel.simplified)};`);
|
|
187
|
+
console.log({
|
|
188
|
+
name: canonicalRelName,
|
|
189
|
+
type: `${filtersStr[0]} --> ${filtersStr[1]}`,
|
|
190
|
+
def: prettyRel(rel.simplified)
|
|
191
|
+
});
|
|
192
|
+
if (DEBUG_FLAG) {
|
|
193
|
+
val = exportRelation(run.defs.rels, run.defs.relAlias, relName);
|
|
194
|
+
console.log(val);
|
|
195
|
+
}
|
|
176
196
|
// }
|
|
177
197
|
// ------ k code
|
|
178
198
|
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|