-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.mjs
204 lines (186 loc) · 6.33 KB
/
pretty.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { find } from "./codes.mjs";
import { TypePatternGraph } from "./typing.mjs";
function compareAs(fn) {
return function (a, b) {
[a, b] = [a, b].map(fn);
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
}
const nameRE = /^[a-zA-Z0-9_][a-zA-Z0-9_?!]*$/;
function prettyCode_labels (representatives, label_ref_map) {
return Object.keys(label_ref_map)
.sort( compareAs((x) => x) )
.map( (label) => {
const plabel = nameRE.test(label) ? label : `'${label}'`;
return `${prettyCode(representatives, {
code: "ref",
ref: label_ref_map[label],
})} ${plabel}`;
})
.join(", ");
};
function prettyCode (representatives, codeExp) {
switch (codeExp.code) {
case "ref":
const name = representatives[codeExp.ref] || codeExp.ref;
if (name.startsWith(":")) {
return prettyCode(representatives, find(name));
} else {
return name;
}
case "product":
return `{${prettyCode_labels(representatives, codeExp.product)}}`;
case "union":
return `< ${prettyCode_labels(representatives, codeExp.union)} >`;
case "vector":
return `[${prettyCode(representatives, {code:'ref', ref:codeExp.vector})}]`;
// return `[${representatives[codeExp.vector] || codeExp.vector}]`;
default:
return ":error";
}
};
function prettyFilter (prettyCode, filter) {
const fieldsStr = () => {
const fields = Object.keys(filter.fields).map( (key) =>
`${prettyFilter(prettyCode, filter.fields[key])} ${key}`
);
if (filter.open) fields.push("...");
return fields.join(", ");
}
switch (filter.type) {
case "code":
return `$${prettyCode({code: "ref",ref: filter.code,})}`;
case "name":
return filter.name;
case "vector":
return `[${prettyFilter(prettyCode, filter.vector)}]${filter.name ? "=" + filter.name : ""}`;
case null:
if (filter.name && Object.keys(filter.fields).length == 0)
return filter.name;
return `(${fieldsStr()})${filter.name ? "=" + filter.name : ""}`;
case "union":
return `<${fieldsStr()}>${filter.name ? "=" + filter.name : ""}`;
case "product":
return `{${fieldsStr()}}${filter.name ? "=" + filter.name : ""}`;
}
throw new Error(`Unknown filter type ${filter.type} in ${JSON.stringify(filter)}`);
}
function prettyRel (prettyCode, exp) {
"use strict";
const pretty = (exp) => {
switch (exp.op) {
case "filter":
return `?${prettyFilter(prettyCode, exp.filter)}`;
case "pipe":
return `|`;
case "caret":
return `(${pretty(exp.caret)} ^)`;
case "vector":
return `[${exp.vector.map(pretty).join(", ")}]`;
case "union":
return `<${exp.union.map(pretty).join(", ")}>`;
case "ref":
return exp.ref;
case "identity":
return "()";
case "comp":
return exp.comp.map(pretty).join(" ");
case "str":
return `'${exp.str}'`;
case "int":
return exp.int;
case "dot":
if ("number" === typeof exp.dot) {
return `.${exp.dot}`;
} else if (nameRE.test(exp.dot)) {
return `.${exp.dot}`;
} else {
return `'${exp.dot}'`;
}
break;
case "code":
return `$${prettyCode({
code: "ref",
ref: exp.code,
})}`;
case "product":
return (function (labelled) {
return `{${labelled.join(", ")}}`;
})(
exp.product.map(function ({ label, exp }) {
if (nameRE.test(label)) {
return `${pretty(exp)} ${label}`;
} else {
return `${pretty(exp)} '${label}'`;
}
})
);
}
};
return pretty(exp);
};
function patterns2filters(typePatternGraph, ...patternIds) {
const newTypePatternGraph = new TypePatternGraph();
const renamed = typePatternGraph.clone(patternIds, newTypePatternGraph);
const newPatternIds = patternIds.map((id) => newTypePatternGraph.find(renamed[id]));
// inDegree - all open patterns, i.e. (...), {...}, <...>,
// with inDegree > 1 are variables
const inDegree = newTypePatternGraph.patterns.nodes.map((x) => 0);
newPatternIds.forEach( id => inDegree[id]++);
newTypePatternGraph.edges.forEach( outEdges =>
Object.values( outEdges ).forEach( destMap =>
Object.values(destMap).forEach( dst =>
inDegree[dst]++ )
)
);
// variables - variables which will have to be added as extra filters
const variables = inDegree.reduce( (variables, degree, i) => {
if (degree > 1) variables[i] = "fresh";
return variables;
}, {});
const buildFilter = (path, patternId, i) => {
let named_filter = {};
const pattern = newTypePatternGraph.get_pattern(patternId);
if (variables[patternId] && pattern.pattern != 'type') {
const name = `X${patternId}`;
if (variables[patternId] == "done")
return { type: 'name', name: name };
variables[patternId] = "done";
named_filter = { name: name };
}
const edges = newTypePatternGraph.edges[patternId];
const fields = () =>
Object.keys(edges).reduce( (fields, key) =>{
fields[key] = buildFilter([...path, key], Object.values(edges[key])[0], i);
return fields;
}, {});
switch (pattern.pattern) {
case 'type':
return {type: "code", code: pattern.type, ...named_filter};
case '[]':
return {
type: "vector",
vector: buildFilter([...path, "vector-member"], Object.values(edges["vector-member"])[0], i),
...named_filter
};
case '(...)':
return { type: null, open: true, fields: fields(), ...named_filter };
case '{...}':
return { type: 'product', open: true, fields: fields(), ...named_filter };
case '<...>':
return { type: 'union', open: true, fields: fields(), ...named_filter };
case '()':
return { type: null, fields: fields(), ...named_filter };
case '{}':
return { type: 'product', fields: fields(), ...named_filter };
case '<>':
return { type: 'union', fields: fields(), ...named_filter };
}
};
const filters = newPatternIds.map( (id, i) => buildFilter([], id, i) );
return filters;
}
export default { prettyCode, prettyRel, patterns2filters };
export { prettyCode, prettyRel, patterns2filters };