-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (79 loc) · 2 KB
/
index.js
File metadata and controls
105 lines (79 loc) · 2 KB
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
"use strict";
const fs = require("fs");
const path = require("path");
const template = fs.readFileSync(path.join(__dirname, "template.txt"), "utf8");
let mkwire, mktwins, getfv, rename;
function psi(shared, list)
{
shared.forEach((twins, atom) => {
const left = twins.left;
const right = twins.right;
const wire = mkwire();
const agent = `\\share(${atom}, ${wire})`;
const amb = `\\amb(${right}, ${agent}, ${wire})`;
list.push(`${left} = ${amb}`);
});
}
function rho(fv, root, end, list)
{
fv.forEach((ref, atom) => {
const next = mkwire();
const agent = `\\bind(${next}, ${ref}, ${root})`;
list.push(`${atom} = ${agent}`);
root = next;
});
list.push(`${root} = ${end}`);
}
function gamma(obj, root, list)
{
const node = obj.node;
if ("atom" == node) {
if (obj.free) {
const name = `this.mkid("${obj.name}")`;
const agent = `\\atom_{${name}}`;
list.push(`${root} = ${agent}`);
} else {
const name = obj.name;
list.push(`${root} = ${name}`);
}
} else if ("abst" == node) {
const id = obj.bound;
const body = obj.body;
const fv = getfv(body);
const wire = mkwire();
const agent = fv.has(id) ? id : "\\erase";
const tree = `\\lambda(${agent}, ${wire})`;
fv.delete(id);
fv.forEach((proper, atom, map) => {
map.set(atom, mkwire());
});
rename(body, fv);
rho(fv, root, tree, list);
gamma(body, wire, list);
} else if ("appl" == node) {
const wleft = mkwire();
const wright = mkwire();
const left = obj.left;
const right = obj.right;
const shared = mktwins(left, right);
const agent = `\\outapp(${wleft}, ${wright})`;
list.push(`${root} = ${agent}`);
gamma(left, wleft, list);
gamma(right, wright, list);
psi(shared, list);
}
}
function encode(generic, term)
{
const inconfig = [
"\\read_{this.mkhole()}(!print) = root"
];
mkwire = generic.mkwire;
mktwins = generic.mktwins;
getfv = generic.getfv;
rename = generic.rename;
gamma(term, "root", inconfig);
inconfig.inet = template;
return inconfig;
}
module.exports = encode;