-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint.js
39 lines (32 loc) · 788 Bytes
/
print.js
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
var atom = (x) => String(x);
var variable = (x) => x;
var structure = (x) => {
if (Array.isArray(x)) {
if (x.length === 1)
return atom(x[0]);
var s = atom(x[0]) + "(" + structure(x[1])
for (var i=2; i<x.length; i++)
s += ", " + structure(x[i]);
return s + ")";
}
return variable(x);
}
var rule = (x) => {
if (x.length === 1)
return structure(x[0]) + ".\n";
var s = structure(x[0]) + " :- " + structure(x[1]);
for (var i=2; i<x.length; i++)
s += ",\n " + structure(x[i])
return s + ".\n";
}
var rules = (x) => x.map(rule).join("");
var printers = {
atom: atom,
variable: variable,
structure: structure,
rule: rule,
rules: rules,
prolog: rules,
query: structure
}
module.exports = (label, x) => printers[label](x);