-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoder.js
194 lines (176 loc) · 6.68 KB
/
coder.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
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
Coder = new function() {
var symtab = [];
function resolve(node) {
if (node.type != "ident")
return node;
for (var i = symtab.length - 1; i >= 0; --i) {
var sym = symtab[i];
if (sym.name.text == node.text)
return resolve(sym.bind);
}
throw "Reference to undefined symbol " + node.text;
}
function wrap(node) {
with (resolve(node))
switch (type) {
case "\\":
var body;
if (!params.some(function (param) {
return param.strict;
}))
return node;
body = node;
params.forEach(function (param) {
body = {
type : "@",
fun : body,
arg : param.name
};
});
return {
type : "\\",
params : params.map(function (param) {
return {
type : "param",
strict : false,
name : param.name
};
}), body : body
};
default:
return node;
}
}
function code(node, strict) {
if (!node)
alert("poof");
with (node)
switch (type) {
case "let":
case "letrec":
defs.forEach(function (def) {
symtab.push(def);
});
res = "function " + "(" + ")" + "{" +
code(defs) +
"return " + code(expr, true) + ";" +
"}" + "(" + ")";
defs.forEach(function (def) {
symtab.pop();
});
return res;
case "defs":
return node.map(code).join("");
case "=":
var res = code(bind);
if (bind.type == "\\") {
var index = -1;
for (node = bind.body; node.type == "@"; node = node.fun)
--index;
if (node.type == "ident"
&& bind.params.some(function (param) {
++index;
return param.name.text == node.text;
}) && index >= 0)
res = "function " + "(" + ")" + "{" +
"var " + "fun" + "=" + res + ";" +
"fun.index" + "=" + index + ";" +
"return " + "fun" + ";" +
"}" + "(" + ")";
}
return "var " + code(name) + "=" + res + ";\n";
case "\\":
var res;
params.forEach(function(param) {
symtab.push({
name : param.name,
bind : param
});
});
res = "function " + "(" + params.map(code) + ")" + "{" +
"return " + code(body, true) + ";" +
"}";
params.forEach(function (param) {
symtab.pop();
});
return res;
case "param":
return code(node.name);
case "?":
return "(" + code(pred, true) + ")" + "?" +
"(" + code(lhs, true) + ")" + ":" +
"(" + code(rhs, true) + ")";
case "#":
return "(" + code(lhs, true) + ")" + text +
"(" + code(rhs, true) + ")";
case "select":
var res = "";
for (var i = 0; i < args.length; ++i) {
var arg = args[i];
res += "case " + i + ":";
if (arg.type == "\\") {
var params = arg.params;
params.forEach(function(param) {
symtab.push({
name : param.name,
bind : param
});
});
res += params.reduce(function (acc, param, index) {
var res = "_[1][" + index + "]";
if (param.strict)
res = "Sapl.eval" + "(" + res + ")";
return acc + "var " + code(param.name) + "=" + res + ";";
}, "");
res += "return " + code(arg.body, true) + ";";
params.forEach(function (param) {
symtab.pop();
});
} else
res += "return " + code(arg) + ";";
}
return "function " + "(" + ")" + "{" +
"var " + "_" + "=" + code(fun, true) + ";" +
"switch " + "(" + "(_[0] || _).index" + ")" + "{" +
res +
"}" +
"}" + "(" + ")";
return res;
case "@":
var head = [],
tail = [],
params;
for (; node.fun; node = node.fun)
tail.push(node.arg);
params = resolve(node).params;
if (params) {
params.forEach(function (param) {
var arg;
arg = tail.pop();
if (arg)
head.push(code(wrap(arg), param.strict));
});
}
tail = tail.reverse().map(wrap).map(code);
res = code(node);
if (strict && params && params.length <= head.length) {
res = res + "(" + head.join() + ")";
} else
tail = head.concat(tail);
if (tail.length > 0) {
res = "[" + res + "," + "[" + tail.join() + "]" + "]";
if (strict)
res = "Sapl.eval" + "(" + res + ")";
}
return res;
case "const":
return node.text;
case "ident":
var res = node.text;
if (strict && !resolve(node).strict)
return "Sapl.eval" + "(" + res + ")";
return res;
}
}
this.code = code;
}();