@fraczak/k 4.0.0 → 4.0.1
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/README.md +1 -1
- package/package.json +1 -1
- package/patterns.mjs +1 -1
- package/repl.mjs +145 -134
- package/run.mjs +1 -1
- package/symbol-table.mjs +1 -1
package/README.md
CHANGED
|
@@ -177,7 +177,7 @@ is not supported in _code derivation_ (explained below).
|
|
|
177
177
|
"[1,2.11,0.3e-32]" --> [1,2.11,3e-33]
|
|
178
178
|
|
|
179
179
|
- other predefined parial functions are: `DIV`, `FDIV`, `CONS`,
|
|
180
|
-
`SNOC`, `toDateMsec`, `toDateStr`, and `_log!`.
|
|
180
|
+
`SNOC`, `toVEC`, `toDateMsec`, `toDateStr`, and `_log!`.
|
|
181
181
|
|
|
182
182
|
---
|
|
183
183
|
|
package/package.json
CHANGED
package/patterns.mjs
CHANGED
|
@@ -360,7 +360,7 @@ function patterns(representatives, rels) {
|
|
|
360
360
|
rel.patterns[0] = rootDef.typePatternGraph.addNewNode({pattern: '[]'}, {"vector-member": [rel.patterns[1]]});
|
|
361
361
|
break;
|
|
362
362
|
case "toVEC":
|
|
363
|
-
rel.patterns[0] = rootDef.typePatternGraph.
|
|
363
|
+
rel.patterns[0] = rootDef.typePatternGraph.getTypeId('string')
|
|
364
364
|
rel.patterns[1] = rootDef.typePatternGraph.addNewNode({pattern: '[]'}, {"vector-member": [rel.patterns[0]]});
|
|
365
365
|
break;
|
|
366
366
|
case "toDateMsec":
|
package/repl.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import readline from "node:readline";
|
|
3
|
+
import fs from "node:fs";
|
|
2
4
|
|
|
3
5
|
import k from "./index.mjs";
|
|
4
6
|
import {run, closeVector } from "./run.mjs";
|
|
5
7
|
import { prettyCode, prettyRel, patterns2filters } from "./pretty.mjs";
|
|
6
8
|
import { find } from "./codes.mjs";
|
|
7
9
|
|
|
8
|
-
import fs from "node:fs";
|
|
9
10
|
|
|
10
11
|
const splice = [].splice;
|
|
11
12
|
|
|
@@ -15,7 +16,7 @@ const help = function () {
|
|
|
15
16
|
console.log(" --h print help");
|
|
16
17
|
console.log(" --a print codes and relations");
|
|
17
18
|
console.log(" --c print aliased codes");
|
|
18
|
-
console.log(" --C
|
|
19
|
+
console.log(" --C code print code definition");
|
|
19
20
|
console.log(" --r print rels");
|
|
20
21
|
console.log(" --p pretty-print last value");
|
|
21
22
|
console.log(" --pp print last value via node.js 'console.log'");
|
|
@@ -23,7 +24,7 @@ const help = function () {
|
|
|
23
24
|
console.log(" --g reg get the value from register 'reg'");
|
|
24
25
|
console.log(" --regs print registers names");
|
|
25
26
|
console.log(" --l file.k load 'file.k'");
|
|
26
|
-
console.log(" --x rel
|
|
27
|
+
console.log(" --x rel print out the type pattern of 'rel'");
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
help();
|
|
@@ -34,140 +35,150 @@ const re__g = /^[ \n\t]*(?:--g[ ]+)(.+)[ ]*?$/;
|
|
|
34
35
|
const re__x = /^[ \n\t]*(?:--x[ ]+)(.+)[ ]*?$/;
|
|
35
36
|
const re__C = /^[ \n\t]*(?:--C[ ]+)(.+)[ ]*?$/;
|
|
36
37
|
|
|
37
|
-
const
|
|
38
|
+
const rl = readline.createInterface({
|
|
39
|
+
input: process.stdin,
|
|
40
|
+
output: process.stdout,
|
|
41
|
+
prompt: ''
|
|
42
|
+
});
|
|
38
43
|
|
|
39
|
-
(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
44
|
+
rl.prompt();
|
|
45
|
+
|
|
46
|
+
rl.on('line', (line) => {
|
|
47
|
+
// console.log(`Received: ${line}`);
|
|
48
|
+
evaluate(line);
|
|
49
|
+
rl.prompt();
|
|
50
|
+
}).on('close', () => {
|
|
51
|
+
console.log('REPL closed');
|
|
52
|
+
process.exit(0);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
let val = {};
|
|
56
|
+
const registers = {};
|
|
57
|
+
const buffer = [];
|
|
58
|
+
function evaluate(line) {
|
|
59
|
+
if (line.trim().endsWith('\\')) {
|
|
60
|
+
buffer.push(line.slice(0, -1));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
line = [...buffer, line].join('\n');
|
|
64
|
+
buffer.length = 0;
|
|
65
|
+
try {
|
|
66
|
+
if (val === void 0) {
|
|
67
|
+
val = {};
|
|
68
|
+
}
|
|
69
|
+
if (line.match(/^[ \n\t]*(?:--h)?$/)) {
|
|
70
|
+
// --h
|
|
71
|
+
help();
|
|
72
|
+
} else if (line.match(/^[ \n\t]*\^$/)) {
|
|
73
|
+
val = closeVector(val);
|
|
74
|
+
console.log(`=> ${JSON.stringify(val)}`);
|
|
75
|
+
} else if (line.match(re__l)) {
|
|
76
|
+
// --l
|
|
77
|
+
const file = line.match(re__l)[1];
|
|
78
|
+
console.log(` -- loading file: ${file} ...`);
|
|
79
|
+
const kScript = fs.readFileSync(file).toString();
|
|
80
|
+
console.log(` ----------------------------- compiling file: ${file} ...`);
|
|
81
|
+
// console.log(kScript);
|
|
82
|
+
val = k.compile("+++\n" + kScript + "\n()")(val);
|
|
83
|
+
console.log(`=> ${JSON.stringify(val)}`);
|
|
84
|
+
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
85
|
+
// --a
|
|
86
|
+
val = run.defs;
|
|
87
|
+
console.log(val);
|
|
88
|
+
// --r
|
|
89
|
+
} else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
|
|
90
|
+
console.log(
|
|
91
|
+
(function (defs, result) {
|
|
92
|
+
if (defs == null) {
|
|
93
|
+
return result;
|
|
54
94
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
console.log(`=> ${JSON.stringify(val)}`);
|
|
61
|
-
} else if (line.match(re__l)) {
|
|
62
|
-
// --l
|
|
63
|
-
file = line.match(re__l)[1];
|
|
64
|
-
console.log(` -- loading file: ${file} ...`);
|
|
65
|
-
kScript = fs.readFileSync(file).toString();
|
|
66
|
-
console.log(` ----------------------------- compiling file: ${file} ...`);
|
|
67
|
-
// console.log(kScript);
|
|
68
|
-
val = k.compile("+++" + kScript + "\n()")(val);
|
|
69
|
-
console.log(`=> ${JSON.stringify(val)}`);
|
|
70
|
-
} else if (line.match(/^[ \n\t]*(?:--a)?$/)) {
|
|
71
|
-
// --a
|
|
72
|
-
val = run.defs;
|
|
73
|
-
console.log(val);
|
|
74
|
-
// --r
|
|
75
|
-
} else if (line.match(/^[ \n\t]*(?:--r)?$/)) {
|
|
76
|
-
console.log(
|
|
77
|
-
(function (defs, result) {
|
|
78
|
-
if (defs == null) {
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
-
return (function (prettyRel) {
|
|
82
|
-
var ref1, relName;
|
|
83
|
-
ref1 = defs.rels;
|
|
84
|
-
for (relName in ref1) {
|
|
85
|
-
result[relName] = prettyRel(ref1[relName].def);
|
|
86
|
-
}
|
|
87
|
-
return result;
|
|
88
|
-
})(
|
|
89
|
-
prettyRel.bind(
|
|
90
|
-
null,
|
|
91
|
-
prettyCode.bind(null, defs.representatives)
|
|
92
|
-
)
|
|
93
|
-
);
|
|
94
|
-
})(run.defs, {}));
|
|
95
|
-
// --c
|
|
96
|
-
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
97
|
-
console.log(
|
|
98
|
-
(function (defs, result) {
|
|
99
|
-
for (const codeName of Object.keys(defs.representatives || {})) {
|
|
100
|
-
const codeExp = find(defs.representatives[codeName] || codeName);
|
|
101
|
-
result[codeName] = prettyCode(
|
|
102
|
-
defs.representatives,
|
|
103
|
-
codeExp
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
return result;
|
|
107
|
-
})(run.defs || {}, {})
|
|
108
|
-
);
|
|
109
|
-
// --C
|
|
110
|
-
} else if (line.match(re__C)) {
|
|
111
|
-
let codeName = line.match(re__C)[1];
|
|
112
|
-
if (codeName.startsWith("?"))
|
|
113
|
-
codeName = codeName.slice(1);
|
|
114
|
-
if (codeName.startsWith("$"))
|
|
115
|
-
codeName = codeName.slice(1);
|
|
116
|
-
const canonicalName = run.defs.representatives[codeName] || codeName;
|
|
117
|
-
const codeExp = find( canonicalName );
|
|
118
|
-
console.log(` $ ${canonicalName} = ${prettyCode(run.defs.representatives, codeExp)}; -- ${codeExp.def}`);
|
|
119
|
-
// --pp
|
|
120
|
-
} else if (line.match(/^[ \n\t]*(?:--pp)?$/)) {
|
|
121
|
-
console.log(val);
|
|
122
|
-
// --p
|
|
123
|
-
} else if (line.match(/^[ \n\t]*(?:--p)?$/)) {
|
|
124
|
-
console.log(JSON.stringify(val, null, 2));
|
|
125
|
-
// --regs
|
|
126
|
-
} else if (line.match(/^[ \n\t]*(?:--regs)?$/)) {
|
|
127
|
-
console.log(` -- registers: ${Object.keys(registers).join(", ")}`);
|
|
128
|
-
// --s
|
|
129
|
-
} else if (line.match(re__s)) {
|
|
130
|
-
let reg = line.match(re__s)[1];
|
|
131
|
-
registers[reg] = val;
|
|
132
|
-
console.log(` -- current value stored in register: '${reg}'`);
|
|
133
|
-
// --g
|
|
134
|
-
} else if (line.match(re__g)) {
|
|
135
|
-
console.log(" -- getting value from register...");
|
|
136
|
-
let reg = line.match(re__g)[1];
|
|
137
|
-
val = registers[reg];
|
|
138
|
-
console.log(val);
|
|
139
|
-
// --x
|
|
140
|
-
} else if (line.match(re__x)) {
|
|
141
|
-
const relName = line.match(re__x)[1];
|
|
142
|
-
const rel = run.defs?.rels[relName];
|
|
143
|
-
if (!rel) {
|
|
144
|
-
console.log(` -- relation '${relName}' not found`);
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
const filters = patterns2filters(rel.typePatternGraph, ...rel.def.patterns);
|
|
148
|
-
// console.log(filters);
|
|
149
|
-
// console.log(" variables:", JSON.stringify(variables));
|
|
150
|
-
// console.log(JSON.stringify({filters, variables}, null, 2));
|
|
151
|
-
// for (const filter of filters) {
|
|
152
|
-
const pcodef= prettyCode.bind(null, run.defs.representatives);
|
|
153
|
-
const filtersStr = filters.map( x => prettyRel(pcodef, {op: "filter", filter: x}));
|
|
154
|
-
console.log(` ${relName} : ${filtersStr[0]} --> ${filtersStr[1]}`);
|
|
155
|
-
// }
|
|
156
|
-
// ------ k code
|
|
157
|
-
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
158
|
-
try {
|
|
159
|
-
val = k.run(`+++ ${line}\n ()`, val);
|
|
160
|
-
console.log(`=> ${JSON.stringify(val)}`);
|
|
161
|
-
} catch (error) {
|
|
162
|
-
e = error;
|
|
163
|
-
console.error("ERROR:");
|
|
164
|
-
console.error(e);
|
|
95
|
+
return (function (prettyRel) {
|
|
96
|
+
let ref1, relName;
|
|
97
|
+
ref1 = defs.rels;
|
|
98
|
+
for (relName in ref1) {
|
|
99
|
+
result[relName] = prettyRel(ref1[relName].def);
|
|
165
100
|
}
|
|
101
|
+
return result;
|
|
102
|
+
})(
|
|
103
|
+
prettyRel.bind(
|
|
104
|
+
null,
|
|
105
|
+
prettyCode.bind(null, defs.representatives)
|
|
106
|
+
)
|
|
107
|
+
);
|
|
108
|
+
})(run.defs, {}));
|
|
109
|
+
// --c
|
|
110
|
+
} else if (line.match(/^[ \n\t]*(?:--c)?$/)) {
|
|
111
|
+
console.log(
|
|
112
|
+
(function (defs, result) {
|
|
113
|
+
for (const codeName of Object.keys(defs.representatives || {})) {
|
|
114
|
+
const codeExp = find(defs.representatives[codeName] || codeName);
|
|
115
|
+
result[codeName] = prettyCode(
|
|
116
|
+
defs.representatives,
|
|
117
|
+
codeExp
|
|
118
|
+
);
|
|
166
119
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
120
|
+
return result;
|
|
121
|
+
})(run.defs || {}, {})
|
|
122
|
+
);
|
|
123
|
+
// --C
|
|
124
|
+
} else if (line.match(re__C)) {
|
|
125
|
+
let codeName = line.match(re__C)[1];
|
|
126
|
+
if (codeName.startsWith("?"))
|
|
127
|
+
codeName = codeName.slice(1);
|
|
128
|
+
if (codeName.startsWith("$"))
|
|
129
|
+
codeName = codeName.slice(1);
|
|
130
|
+
const canonicalName = run.defs.representatives[codeName] || codeName;
|
|
131
|
+
const codeExp = find( canonicalName );
|
|
132
|
+
console.log(` $ ${canonicalName} = ${prettyCode(run.defs.representatives, codeExp)}; -- ${codeExp.def}`);
|
|
133
|
+
// --pp
|
|
134
|
+
} else if (line.match(/^[ \n\t]*(?:--pp)?$/)) {
|
|
135
|
+
console.log(val);
|
|
136
|
+
// --p
|
|
137
|
+
} else if (line.match(/^[ \n\t]*(?:--p)?$/)) {
|
|
138
|
+
console.log(JSON.stringify(val, null, 2));
|
|
139
|
+
// --regs
|
|
140
|
+
} else if (line.match(/^[ \n\t]*(?:--regs)?$/)) {
|
|
141
|
+
console.log(` -- registers: ${Object.keys(registers).join(", ")}`);
|
|
142
|
+
// --s
|
|
143
|
+
} else if (line.match(re__s)) {
|
|
144
|
+
let reg = line.match(re__s)[1];
|
|
145
|
+
registers[reg] = val;
|
|
146
|
+
console.log(` -- current value stored in register: '${reg}'`);
|
|
147
|
+
// --g
|
|
148
|
+
} else if (line.match(re__g)) {
|
|
149
|
+
console.log(" -- getting value from register...");
|
|
150
|
+
let reg = line.match(re__g)[1];
|
|
151
|
+
val = registers[reg];
|
|
152
|
+
console.log(val);
|
|
153
|
+
// --x
|
|
154
|
+
} else if (line.match(re__x)) {
|
|
155
|
+
const relName = line.match(re__x)[1];
|
|
156
|
+
const rel = run.defs?.rels[relName];
|
|
157
|
+
if (!rel) {
|
|
158
|
+
console.log(` -- relation '${relName}' not found`);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const filters = patterns2filters(rel.typePatternGraph, ...rel.def.patterns);
|
|
162
|
+
// console.log(filters);
|
|
163
|
+
// console.log(" variables:", JSON.stringify(variables));
|
|
164
|
+
// console.log(JSON.stringify({filters, variables}, null, 2));
|
|
165
|
+
// for (const filter of filters) {
|
|
166
|
+
const pcodef= prettyCode.bind(null, run.defs.representatives);
|
|
167
|
+
const filtersStr = filters.map( x => prettyRel(pcodef, {op: "filter", filter: x}));
|
|
168
|
+
console.log(` ${relName} : ${filtersStr[0]} --> ${filtersStr[1]}`);
|
|
169
|
+
// }
|
|
170
|
+
// ------ k code
|
|
171
|
+
} else if (!line.match(/^[ \n\t]*(?:#.*)?$/)) {
|
|
172
|
+
try {
|
|
173
|
+
val = k.run(`+++\n${line}\n()`, val);
|
|
174
|
+
console.log(`=> ${JSON.stringify(val)}`);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
e = error;
|
|
177
|
+
console.error("ERROR:");
|
|
178
|
+
console.error(e);
|
|
170
179
|
}
|
|
171
180
|
}
|
|
172
|
-
})
|
|
173
|
-
|
|
181
|
+
} catch (e) {
|
|
182
|
+
console.error(e.message);
|
|
183
|
+
}
|
|
184
|
+
}
|
package/run.mjs
CHANGED
package/symbol-table.mjs
CHANGED
|
@@ -124,7 +124,7 @@ class SymbolTable {
|
|
|
124
124
|
|
|
125
125
|
add_rel(name, rel) {
|
|
126
126
|
if (this.rels[name] != undefined)
|
|
127
|
-
console.error(`SymbolTable: rel ${name}
|
|
127
|
+
console.error(`SymbolTable: rel '${name}' is redefined (lines ${rel.start?.line}:${rel.start?.column}...${rel.end?.line}:${rel.end?.column})`);
|
|
128
128
|
|
|
129
129
|
this.rels[name] = {def: rel};
|
|
130
130
|
}
|