adamc@1036
|
1 // Detect browser quirks that we should be aware of.
|
adamc@1036
|
2
|
adamc@1036
|
3 function needsDynPrefix() {
|
adamc@1036
|
4 var span = document.createElement("span");
|
adamc@1036
|
5 span.innerHTML = "<script>alert('test');</script>";
|
adamc@1036
|
6 var scripts = span.getElementsByTagName("script");
|
adamc@1036
|
7 return scripts.length == 0;
|
adamc@1036
|
8 }
|
adamc@1036
|
9
|
adamc@1036
|
10 var dynPrefix = needsDynPrefix() ? "<span style=\"display:none\">A</span>" : "";
|
adamc@1036
|
11
|
adamc@970
|
12 // Function versions of operators
|
adamc@970
|
13
|
adamc@970
|
14 function not(x) { return !x; }
|
adamc@970
|
15 function neg(x) { return -x; }
|
adamc@970
|
16
|
adamc@970
|
17 function eq(x, y) { return x == y; }
|
adamc@970
|
18 function plus(x, y) { return x + y; }
|
adamc@970
|
19 function minus(x, y) { return x - y; }
|
adamc@970
|
20 function times(x, y) { return x * y; }
|
adamc@970
|
21 function div(x, y) { return x / y; }
|
adamc@970
|
22 function mod(x, y) { return x % y; }
|
adamc@970
|
23 function lt(x, y) { return x < y; }
|
adamc@970
|
24 function le(x, y) { return x <= y; }
|
adamc@970
|
25
|
adamc@692
|
26 // Lists
|
adamc@692
|
27
|
adamc@580
|
28 function cons(v, ls) {
|
adamc@690
|
29 return { next : ls, data : v };
|
adamc@690
|
30 }
|
adamc@969
|
31 function rev(ls) {
|
adamc@969
|
32 var acc = null;
|
adamc@969
|
33 for (; ls; ls = ls.next)
|
adamc@969
|
34 acc = cons(ls.data, acc);
|
adamc@969
|
35 return acc;
|
adamc@969
|
36 }
|
adamc@690
|
37 function concat(ls1, ls2) {
|
adamc@969
|
38 var acc = ls2;
|
adamc@969
|
39 ls1 = rev(ls1);
|
adamc@969
|
40 for (; ls1; ls1 = ls1.next)
|
adamc@969
|
41 acc = cons(ls1.data, acc);
|
adamc@969
|
42 return acc;
|
adamc@690
|
43 }
|
adamc@690
|
44 function member(x, ls) {
|
adamc@690
|
45 for (; ls; ls = ls.next)
|
adamc@690
|
46 if (ls.data == x)
|
adamc@690
|
47 return true;
|
adamc@690
|
48 return false;
|
adamc@690
|
49 }
|
adamc@690
|
50 function remove(x, ls) {
|
adamc@969
|
51 var acc = null;
|
adamc@969
|
52
|
adamc@969
|
53 for (; ls; ls = ls.next)
|
adamc@969
|
54 if (ls.data == x)
|
adamc@969
|
55 return concat(acc, ls.next);
|
adamc@969
|
56 else
|
adamc@969
|
57 acc = cons(ls.data, acc);
|
adamc@969
|
58
|
adamc@969
|
59 return ls;
|
adamc@690
|
60 }
|
adamc@690
|
61 function union(ls1, ls2) {
|
adamc@969
|
62 var acc = ls2;
|
adamc@969
|
63
|
adamc@969
|
64 for (; ls1; ls1 = ls1.next)
|
adamc@969
|
65 if (!member(ls1.data, ls2))
|
adamc@969
|
66 acc = cons(ls1.data, acc);
|
adamc@969
|
67
|
adamc@969
|
68 return acc;
|
adamc@580
|
69 }
|
adamc@703
|
70 function length(ls) {
|
adamc@969
|
71 var acc = 0;
|
adamc@969
|
72
|
adamc@969
|
73 for (; ls; ls = ls.next)
|
adamc@969
|
74 ++acc;
|
adamc@969
|
75
|
adamc@969
|
76 return acc;
|
adamc@703
|
77 }
|
adamc@670
|
78
|
adamc@690
|
79
|
adamc@729
|
80 // Error handling
|
adamc@729
|
81
|
adamc@729
|
82 function whine(msg) {
|
adamc@729
|
83 alert(msg);
|
adamc@729
|
84 throw msg;
|
adamc@729
|
85 }
|
adamc@729
|
86
|
adamc@810
|
87 function pf(loc) {
|
adamc@835
|
88 throw ("Pattern match failure (" + loc + ")");
|
adamc@729
|
89 }
|
adamc@729
|
90
|
adamc@798
|
91 function runHandlers(kind, ls, arg) {
|
adamc@798
|
92 if (ls == null)
|
adamc@798
|
93 alert(kind + ": " + arg);
|
adamc@729
|
94 for (; ls; ls = ls.next)
|
adamc@729
|
95 try {
|
adamc@976
|
96 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
|
adamc@729
|
97 } catch (v) { }
|
adamc@729
|
98 }
|
adamc@729
|
99
|
adamc@729
|
100 var errorHandlers = null;
|
adamc@729
|
101
|
adamc@729
|
102 function onError(f) {
|
adamc@729
|
103 errorHandlers = cons(f, errorHandlers);
|
adamc@729
|
104 }
|
adamc@729
|
105
|
adamc@729
|
106 function er(s) {
|
adamc@798
|
107 runHandlers("Error", errorHandlers, s);
|
adamc@729
|
108 throw {uw_error: s};
|
adamc@729
|
109 }
|
adamc@729
|
110
|
adamc@729
|
111 var failHandlers = null;
|
adamc@729
|
112
|
adamc@729
|
113 function onFail(f) {
|
adamc@729
|
114 failHandlers = cons(f, failHandlers);
|
adamc@729
|
115 }
|
adamc@729
|
116
|
adamc@729
|
117 function doExn(v) {
|
adamc@729
|
118 if (v == null || v.uw_error == null) {
|
adamc@1039
|
119 var s = (v == null ? "null" : v.message ? v.message : v.toString());
|
adamc@969
|
120 if (v != null && v.fileName && v.lineNumber)
|
adamc@969
|
121 s += " (" + v.fileName + ":" + v.lineNumber + ")";
|
adamc@798
|
122 runHandlers("Fail", failHandlers, s);
|
adamc@729
|
123 }
|
adamc@729
|
124 }
|
adamc@729
|
125
|
adamc@729
|
126 var disconnectHandlers = null;
|
adamc@729
|
127
|
adamc@976
|
128 function flift(f) {
|
adamc@977
|
129 return {env:cons(f,null), body:{c:"v", n:1}};
|
adamc@976
|
130 }
|
adamc@976
|
131
|
adamc@729
|
132 function onDisconnect(f) {
|
adamc@976
|
133 disconnectHandlers = cons(flift(f), disconnectHandlers);
|
adamc@729
|
134 }
|
adamc@729
|
135
|
adamc@729
|
136 function discon() {
|
adamc@798
|
137 runHandlers("Disconnect", disconnectHandlers, null);
|
adamc@729
|
138 }
|
adamc@729
|
139
|
adamc@729
|
140 var connectHandlers = null;
|
adamc@729
|
141
|
adamc@729
|
142 function onConnectFail(f) {
|
adamc@976
|
143 connectHandlers = cons(flift(f), connectHandlers);
|
adamc@729
|
144 }
|
adamc@729
|
145
|
adamc@729
|
146 function conn() {
|
adamc@798
|
147 runHandlers("Connect", connectHandlers, null);
|
adamc@729
|
148 }
|
adamc@729
|
149
|
adamc@729
|
150 var serverHandlers = null;
|
adamc@729
|
151
|
adamc@729
|
152 function onServerError(f) {
|
adamc@729
|
153 serverHandlers = cons(f, serverHandlers);
|
adamc@729
|
154 }
|
adamc@729
|
155
|
adamc@729
|
156 function servErr(s) {
|
adamc@824
|
157 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
|
adamc@729
|
158 }
|
adamc@729
|
159
|
adamc@729
|
160
|
adamc@692
|
161 // Embedding closures in XML strings
|
adamc@692
|
162
|
adamc@693
|
163 function cs(f) {
|
adamc@693
|
164 return {closure: f};
|
adamc@693
|
165 }
|
adamc@693
|
166
|
adamc@693
|
167 function isWeird(v) {
|
adamc@693
|
168 return v.closure != null || v.cat1 != null;
|
adamc@693
|
169 }
|
adamc@693
|
170
|
adamc@692
|
171 function cat(s1, s2) {
|
adamc@693
|
172 if (isWeird(s1) || isWeird(s2))
|
adamc@693
|
173 return {cat1: s1, cat2: s2};
|
adamc@693
|
174 else
|
adamc@692
|
175 return s1 + s2;
|
adamc@692
|
176 }
|
adamc@692
|
177
|
adamc@692
|
178 var closures = [];
|
adamc@703
|
179 var freeClosures = null;
|
adamc@692
|
180
|
adamc@692
|
181 function newClosure(f) {
|
adamc@703
|
182 var n;
|
adamc@703
|
183 if (freeClosures == null) {
|
adamc@703
|
184 n = closures.length;
|
adamc@703
|
185 } else {
|
adamc@703
|
186 n = freeClosures.data;
|
adamc@703
|
187 freeClosures = freeClosures.next;
|
adamc@703
|
188 }
|
adamc@692
|
189 closures[n] = f;
|
adamc@692
|
190 return n;
|
adamc@692
|
191 }
|
adamc@692
|
192
|
adamc@703
|
193 function freeClosure(n) {
|
adamc@703
|
194 closures[n] = null;
|
adamc@703
|
195 freeClosures = cons(n, freeClosures);
|
adamc@703
|
196 }
|
adamc@703
|
197
|
adamc@692
|
198 function cr(n) {
|
adamc@975
|
199 return closures[n];
|
adamc@692
|
200 }
|
adamc@692
|
201
|
adamc@703
|
202 function flatten(cls, tr) {
|
adamc@693
|
203 if (tr.cat1 != null)
|
adamc@703
|
204 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
|
adamc@703
|
205 else if (tr.closure != null) {
|
adamc@703
|
206 var cl = newClosure(tr.closure);
|
adamc@703
|
207 cls.v = cons(cl, cls.v);
|
adamc@703
|
208 return "cr(" + cl + ")";
|
adamc@703
|
209 } else
|
adamc@692
|
210 return tr;
|
adamc@692
|
211 }
|
adamc@692
|
212
|
adamc@728
|
213 function flattenLocal(s) {
|
adamc@728
|
214 var cls = {v : null};
|
adamc@728
|
215 var r = flatten(cls, s);
|
adamc@728
|
216 for (cl = cls.v; cl != null; cl = cl.next)
|
adamc@728
|
217 freeClosure(cl.data);
|
adamc@728
|
218 return r;
|
adamc@728
|
219 }
|
adamc@728
|
220
|
adamc@692
|
221
|
adamc@692
|
222
|
adamc@692
|
223 // Dynamic tree management
|
adamc@692
|
224
|
adamc@690
|
225 function populate(node) {
|
adamc@690
|
226 var s = node.signal;
|
adamc@690
|
227 var oldSources = node.sources;
|
adamc@729
|
228 try {
|
adamc@977
|
229 var sr = execF(s, null);
|
adamc@976
|
230 var newSources = sr._sources;
|
adamc@690
|
231
|
adamc@729
|
232 for (var sp = oldSources; sp; sp = sp.next)
|
adamc@729
|
233 if (!member(sp.data, newSources))
|
adamc@729
|
234 sp.data.dyns = remove(node, sp.data.dyns);
|
adamc@690
|
235
|
adamc@729
|
236 for (var sp = newSources; sp; sp = sp.next)
|
adamc@729
|
237 if (!member(sp.data, oldSources))
|
adamc@729
|
238 sp.data.dyns = cons(node, sp.data.dyns);
|
adamc@690
|
239
|
adamc@729
|
240 node.sources = newSources;
|
adamc@976
|
241 node.recreate(sr._data);
|
adamc@729
|
242 } catch (v) {
|
adamc@729
|
243 doExn(v);
|
adamc@729
|
244 }
|
adamc@579
|
245 }
|
adamc@574
|
246
|
adamc@580
|
247 function sc(v) {
|
adamc@690
|
248 return {data : v, dyns : null};
|
adamc@580
|
249 }
|
adamc@580
|
250 function sv(s, v) {
|
adamc@690
|
251 s.data = v;
|
adamc@967
|
252
|
adamc@690
|
253 for (var ls = s.dyns; ls; ls = ls.next)
|
adamc@690
|
254 if (!ls.dead)
|
adamc@690
|
255 populate(ls.data);
|
adamc@580
|
256 }
|
adamc@601
|
257 function sg(s) {
|
adamc@690
|
258 return s.data;
|
adamc@601
|
259 }
|
adamc@579
|
260
|
adamc@580
|
261 function ss(s) {
|
adamc@976
|
262 return {env:cons(s, null), body:{c:"r", l:
|
adamc@976
|
263 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
|
adamc@976
|
264 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
|
adamc@580
|
265 }
|
adamc@580
|
266 function sr(v) {
|
adamc@976
|
267 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
|
adamc@580
|
268 }
|
adamc@580
|
269 function sb(x,y) {
|
adamc@976
|
270 return {env:cons(y,cons(x,null)),
|
adamc@976
|
271 body:{c:"=",
|
adamc@977
|
272 e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}},
|
adamc@976
|
273 e2:{c:"=",
|
adamc@976
|
274 e1:{c:"a",
|
adamc@977
|
275 f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
|
adamc@976
|
276 x:{c:"c", v:null}},
|
adamc@976
|
277 e2:{c:"r", l:cons(
|
adamc@976
|
278 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
|
adamc@976
|
279 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
|
adamc@976
|
280 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
|
adamc@580
|
281 }
|
adamc@841
|
282 function scur(s) {
|
adamc@982
|
283 return execF(s, null)._data;
|
adamc@841
|
284 }
|
adamc@571
|
285
|
adamc@604
|
286 function lastParent() {
|
adamc@604
|
287 var pos = document;
|
adamc@604
|
288
|
adamc@600
|
289 while (pos.lastChild && pos.lastChild.nodeType == 1)
|
adamc@600
|
290 pos = pos.lastChild;
|
adamc@600
|
291
|
adamc@1039
|
292 pos = pos.parentNode;
|
adamc@1039
|
293
|
adamc@1039
|
294 return pos;
|
adamc@600
|
295 }
|
adamc@600
|
296
|
adamc@898
|
297 function parent() {
|
adamc@898
|
298 return thisScript ? thisScript.parentNode : lastParent();
|
adamc@898
|
299 }
|
adamc@898
|
300
|
adamc@604
|
301 function addNode(node) {
|
adamc@899
|
302 if (thisScript)
|
adamc@899
|
303 thisScript.parentNode.replaceChild(node, thisScript);
|
adamc@899
|
304 else
|
adamc@604
|
305 lastParent().appendChild(node);
|
adamc@603
|
306 }
|
adamc@603
|
307
|
adamc@690
|
308 var thisScript = null;
|
adamc@690
|
309
|
adamc@604
|
310 function runScripts(node) {
|
adamc@899
|
311 if (node.getElementsByTagName) {
|
adamc@899
|
312 var savedScript = thisScript;
|
adamc@603
|
313
|
adamc@899
|
314 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
|
adamc@899
|
315 var len = scripts.length;
|
adamc@899
|
316 for (var i = 0; i < len; ++i)
|
adamc@899
|
317 scriptsCopy[i] = scripts[i];
|
adamc@899
|
318 for (var i = 0; i < len; ++i) {
|
adamc@899
|
319 thisScript = scriptsCopy[i];
|
adamc@1039
|
320
|
adamc@899
|
321 try {
|
adamc@1036
|
322 eval(thisScript.text);
|
adamc@899
|
323 } catch (v) {
|
adamc@899
|
324 doExn(v);
|
adamc@899
|
325 }
|
adamc@899
|
326 if (thisScript.parentNode)
|
adamc@901
|
327 thisScript.parentNode.removeChild(thisScript);
|
adamc@729
|
328 }
|
adamc@899
|
329
|
adamc@899
|
330 thisScript = savedScript;
|
adamc@604
|
331 }
|
adamc@603
|
332 }
|
adamc@603
|
333
|
adamc@603
|
334
|
adamc@692
|
335 // Dynamic tree entry points
|
adamc@692
|
336
|
adamc@967
|
337 function killScript(scr) {
|
adamc@967
|
338 scr.dead = true;
|
adamc@967
|
339 for (var ls = scr.sources; ls; ls = ls.next)
|
adamc@967
|
340 ls.data.dyns = remove(scr, ls.data.dyns);
|
adamc@967
|
341 for (var ls = scr.closures; ls; ls = ls.next)
|
adamc@967
|
342 freeClosure(ls.data);
|
adamc@967
|
343 }
|
adamc@967
|
344
|
adamc@901
|
345 function dyn(pnode, s) {
|
adamc@901
|
346 var x = document.createElement("script");
|
adamc@690
|
347 x.dead = false;
|
adamc@690
|
348 x.signal = s;
|
adamc@690
|
349 x.sources = null;
|
adamc@703
|
350 x.closures = null;
|
adamc@899
|
351
|
adamc@902
|
352 var firstChild = null;
|
adamc@899
|
353
|
adamc@690
|
354 x.recreate = function(v) {
|
adamc@703
|
355 for (var ls = x.closures; ls; ls = ls.next)
|
adamc@703
|
356 freeClosure(ls.data);
|
adamc@692
|
357
|
adamc@902
|
358 var next;
|
adamc@902
|
359 for (var child = firstChild; child && child != x; child = next) {
|
adamc@902
|
360 next = child.nextSibling;
|
adamc@1039
|
361
|
adamc@967
|
362 killScript(child);
|
adamc@901
|
363 if (child.getElementsByTagName) {
|
adamc@901
|
364 var arr = child.getElementsByTagName("script");
|
adamc@967
|
365 for (var i = 0; i < arr.length; ++i)
|
adamc@967
|
366 killScript(arr[i]);
|
adamc@847
|
367 }
|
adamc@847
|
368
|
adamc@901
|
369 if (child.parentNode)
|
adamc@901
|
370 child.parentNode.removeChild(child);
|
adamc@899
|
371 }
|
adamc@690
|
372
|
adamc@703
|
373 var cls = {v : null};
|
adamc@1039
|
374 var html = flatten(cls, v);
|
adamc@1039
|
375 if (pnode != "table" && pnode != "tr")
|
adamc@1039
|
376 html = dynPrefix + html;
|
adamc@703
|
377 x.closures = cls.v;
|
adamc@899
|
378
|
adamc@901
|
379 if (pnode == "table") {
|
adamc@901
|
380 var dummy = document.createElement("body");
|
adamc@901
|
381 dummy.innerHTML = "<table>" + html + "</table>";
|
adamc@901
|
382 runScripts(dummy);
|
adamc@901
|
383 var table = x.parentNode;
|
adamc@901
|
384
|
adamc@946
|
385 if (table) {
|
adamc@946
|
386 firstChild = null;
|
adamc@946
|
387 var tbody;
|
adamc@1039
|
388
|
adamc@1039
|
389 var arr = dummy.getElementsByTagName("tbody");
|
adamc@1039
|
390
|
adamc@1039
|
391 var tbody;
|
adamc@1039
|
392 if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) {
|
adamc@946
|
393 tbody = arr[0];
|
adamc@1039
|
394 var next;
|
adamc@1039
|
395 for (var node = dummy.firstChild.firstChild; node; node = next) {
|
adamc@1039
|
396 next = node.nextSibling;
|
adamc@1039
|
397
|
adamc@1039
|
398 if (node.tagName != "TBODY")
|
adamc@1039
|
399 tbody.appendChild(node);
|
adamc@1039
|
400 }
|
adamc@1039
|
401 } else
|
adamc@946
|
402 tbody = dummy.firstChild;
|
adamc@945
|
403
|
adamc@946
|
404 var next;
|
adamc@946
|
405 firstChild = document.createElement("script");
|
adamc@946
|
406 table.insertBefore(firstChild, x);
|
adamc@946
|
407 for (var node = tbody.firstChild; node; node = next) {
|
adamc@946
|
408 next = node.nextSibling;
|
adamc@946
|
409 table.insertBefore(node, x);
|
adamc@946
|
410 }
|
adamc@901
|
411 }
|
adamc@903
|
412 } else if (pnode == "tr") {
|
adamc@903
|
413 var dummy = document.createElement("body");
|
adamc@903
|
414 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
|
adamc@903
|
415 runScripts(dummy);
|
adamc@903
|
416 var table = x.parentNode;
|
adamc@903
|
417
|
adamc@946
|
418 if (table) {
|
adamc@946
|
419 var arr = dummy.getElementsByTagName("tr");
|
adamc@946
|
420 firstChild = null;
|
adamc@1039
|
421 var tr;
|
adamc@946
|
422 if (arr.length > 0 && table != null)
|
adamc@1039
|
423 tr = arr[0];
|
adamc@946
|
424 else
|
adamc@1039
|
425 tr = dummy.firstChild;
|
adamc@946
|
426
|
adamc@946
|
427 var next;
|
adamc@903
|
428 firstChild = document.createElement("script");
|
adamc@903
|
429 table.insertBefore(firstChild, x);
|
adamc@1039
|
430 for (var node = tr.firstChild; node; node = next) {
|
adamc@903
|
431 next = node.nextSibling;
|
adamc@903
|
432 table.insertBefore(node, x);
|
adamc@903
|
433 }
|
adamc@903
|
434 }
|
adamc@901
|
435 } else {
|
adamc@902
|
436 firstChild = document.createElement("span");
|
adamc@902
|
437 firstChild.innerHTML = html;
|
adamc@902
|
438 runScripts(firstChild);
|
adamc@900
|
439 if (x.parentNode)
|
adamc@902
|
440 x.parentNode.insertBefore(firstChild, x);
|
adamc@899
|
441 }
|
adamc@690
|
442 };
|
adamc@899
|
443
|
adamc@899
|
444 addNode(x);
|
adamc@690
|
445 populate(x);
|
adamc@571
|
446 }
|
adamc@582
|
447
|
adamc@1040
|
448 function input(x, s, recreate, type) {
|
adamc@1039
|
449 if (type) x.type = type;
|
adamc@690
|
450 x.dead = false;
|
adamc@690
|
451 x.signal = ss(s);
|
adamc@690
|
452 x.sources = null;
|
adamc@817
|
453 x.recreate = recreate(x);
|
adamc@899
|
454 addNode(x);
|
adamc@690
|
455 populate(x);
|
adamc@816
|
456
|
adamc@816
|
457 return x;
|
adamc@816
|
458 }
|
adamc@816
|
459
|
adamc@816
|
460 function inp(s) {
|
adamc@1040
|
461 var x = input(document.createElement("input"), s,
|
adamc@1040
|
462 function(x) { return function(v) { if (x.value != v) x.value = v; }; });
|
adamc@816
|
463 x.value = s.data;
|
adamc@816
|
464 x.onkeyup = function() { sv(s, x.value) };
|
adamc@816
|
465
|
adamc@816
|
466 return x;
|
adamc@816
|
467 }
|
adamc@816
|
468
|
adamc@816
|
469 function sel(s, content) {
|
adamc@1040
|
470 var dummy = document.createElement("span");
|
adamc@1040
|
471 dummy.innerHTML = "<select>" + content + "</select>";
|
adamc@1040
|
472 var x = input(dummy.firstChild, s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
|
adamc@816
|
473 x.value = s.data;
|
adamc@835
|
474 if (x.value != s.data)
|
adamc@835
|
475 sv(s, x.value);
|
adamc@816
|
476 x.onchange = function() { sv(s, x.value) };
|
adamc@797
|
477
|
adamc@606
|
478 return x;
|
adamc@598
|
479 }
|
adamc@598
|
480
|
adamc@817
|
481 function chk(s) {
|
adamc@1040
|
482 var x = input(document.createElement("input"), s,
|
adamc@1040
|
483 function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox");
|
adamc@1041
|
484 x.defaultChecked = x.checked = s.data;
|
adamc@1041
|
485 x.onclick = function() { sv(s, x.checked) };
|
adamc@817
|
486
|
adamc@817
|
487 return x;
|
adamc@817
|
488 }
|
adamc@817
|
489
|
adamc@800
|
490 function addOnChange(x, f) {
|
adamc@800
|
491 var old = x.onchange;
|
adamc@800
|
492 x.onchange = function() { old(); f (); };
|
adamc@800
|
493 }
|
adamc@800
|
494
|
adamc@692
|
495
|
adamc@692
|
496 // Basic string operations
|
adamc@692
|
497
|
adamc@597
|
498 function eh(x) {
|
adamc@800
|
499 if (x == null)
|
adamc@800
|
500 return "NULL";
|
adamc@800
|
501 else
|
adamc@800
|
502 return x.split("&").join("&").split("<").join("<").split(">").join(">");
|
adamc@597
|
503 }
|
adamc@597
|
504
|
adamc@582
|
505 function ts(x) { return x.toString() }
|
adamc@586
|
506 function bs(b) { return (b ? "True" : "False") }
|
adamc@586
|
507
|
adamc@1038
|
508 function sub(s, i) { return s.charAt(i); }
|
adamc@828
|
509 function suf(s, i) { return s.substring(i); }
|
adamc@828
|
510 function slen(s) { return s.length; }
|
adamc@829
|
511 function sidx(s, ch) {
|
adamc@829
|
512 var r = s.indexOf(ch);
|
adamc@829
|
513 if (r == -1)
|
adamc@829
|
514 return null;
|
adamc@829
|
515 else
|
adamc@829
|
516 return r;
|
adamc@829
|
517 }
|
adamc@831
|
518 function sspn(s, chs) {
|
adamc@831
|
519 for (var i = 0; i < s.length; ++i)
|
adamc@1038
|
520 if (chs.indexOf(s.charAt(i)) != -1)
|
adamc@831
|
521 return i;
|
adamc@831
|
522
|
adamc@831
|
523 return null;
|
adamc@831
|
524 }
|
adamc@829
|
525 function schr(s, ch) {
|
adamc@829
|
526 var r = s.indexOf(ch);
|
adamc@829
|
527 if (r == -1)
|
adamc@829
|
528 return null;
|
adamc@829
|
529 else
|
adamc@829
|
530 return s.substring(r);
|
adamc@829
|
531 }
|
adamc@829
|
532 function ssub(s, start, len) {
|
adamc@829
|
533 return s.substring(start, start+len);
|
adamc@829
|
534 }
|
adamc@821
|
535
|
adamc@649
|
536 function pi(s) {
|
adamc@649
|
537 var r = parseInt(s);
|
adamc@649
|
538 if (r.toString() == s)
|
adamc@649
|
539 return r;
|
adamc@649
|
540 else
|
adamc@729
|
541 er("Can't parse int: " + s);
|
adamc@649
|
542 }
|
adamc@649
|
543
|
adamc@649
|
544 function pfl(s) {
|
adamc@649
|
545 var r = parseFloat(s);
|
adamc@649
|
546 if (r.toString() == s)
|
adamc@649
|
547 return r;
|
adamc@649
|
548 else
|
adamc@729
|
549 er("Can't parse float: " + s);
|
adamc@649
|
550 }
|
adamc@649
|
551
|
adamc@838
|
552 function pio(s) {
|
adamc@838
|
553 var r = parseInt(s);
|
adamc@838
|
554 if (r.toString() == s)
|
adamc@838
|
555 return r;
|
adamc@838
|
556 else
|
adamc@838
|
557 return null;
|
adamc@838
|
558 }
|
adamc@838
|
559
|
adamc@838
|
560 function pflo(s) {
|
adamc@838
|
561 var r = parseFloat(s);
|
adamc@838
|
562 if (r.toString() == s)
|
adamc@838
|
563 return r;
|
adamc@838
|
564 else
|
adamc@838
|
565 return null;
|
adamc@838
|
566 }
|
adamc@838
|
567
|
adamc@692
|
568 function uf(s) {
|
adamc@925
|
569 if (s.length == 0)
|
adamc@925
|
570 return "_";
|
adamc@1038
|
571 return (s.charAt(0) == '_' ? "_" : "")
|
adamc@925
|
572 + escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B");
|
adamc@691
|
573 }
|
adamc@691
|
574
|
adamc@692
|
575 function uu(s) {
|
adamc@1038
|
576 if (s.length > 0 && s.charAt(0) == '_') {
|
adamc@925
|
577 s = s.substring(1);
|
adamc@1038
|
578 } else if (s.length >= 3 && s.charAt(0) == '%' && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
|
adamc@927
|
579 s = s.substring(3);
|
adamc@905
|
580 return unescape(s.replace(new RegExp ("\\+", "g"), " "));
|
adamc@905
|
581 }
|
adamc@905
|
582
|
adamc@912
|
583 function ub(b) {
|
adamc@912
|
584 return b ? "1" : "0";
|
adamc@912
|
585 }
|
adamc@912
|
586
|
adamc@905
|
587 function uul(getToken, getData) {
|
adamc@905
|
588 var tok = getToken();
|
adamc@905
|
589 if (tok == "Nil") {
|
adamc@905
|
590 return null;
|
adamc@905
|
591 } else if (tok == "Cons") {
|
adamc@905
|
592 var d = getData();
|
adamc@905
|
593 var l = uul(getToken, getData);
|
adamc@905
|
594 return {_1:d, _2:l};
|
adamc@905
|
595 } else
|
adamc@982
|
596 whine("Can't unmarshal list (" + tok + ")");
|
adamc@692
|
597 }
|
adamc@692
|
598
|
adamc@961
|
599 function strcmp(str1, str2) {
|
adamc@961
|
600 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
|
adamc@961
|
601 }
|
adamc@692
|
602
|
adamc@603
|
603
|
adamc@692
|
604 // Remote calls
|
adamc@609
|
605
|
adamc@703
|
606 var client_id = null;
|
adamc@668
|
607 var client_pass = 0;
|
adamc@668
|
608 var url_prefix = "/";
|
adamc@673
|
609 var timeout = 60;
|
adamc@668
|
610
|
adamc@668
|
611 function getXHR(uri)
|
adamc@609
|
612 {
|
adamc@609
|
613 try {
|
adamc@609
|
614 return new XMLHttpRequest();
|
adamc@609
|
615 } catch (e) {
|
adamc@609
|
616 try {
|
adamc@609
|
617 return new ActiveXObject("Msxml2.XMLHTTP");
|
adamc@609
|
618 } catch (e) {
|
adamc@609
|
619 try {
|
adamc@609
|
620 return new ActiveXObject("Microsoft.XMLHTTP");
|
adamc@609
|
621 } catch (e) {
|
adamc@982
|
622 whine("Your browser doesn't seem to support AJAX.");
|
adamc@609
|
623 }
|
adamc@609
|
624 }
|
adamc@609
|
625 }
|
adamc@609
|
626 }
|
adamc@609
|
627
|
adamc@736
|
628 var sig = null;
|
adamc@736
|
629
|
adamc@1042
|
630 var unloading = false, inFlight = null;
|
adamc@1042
|
631
|
adamc@1042
|
632 function unload() {
|
adamc@1042
|
633 unloading = true;
|
adamc@1042
|
634
|
adamc@1042
|
635 for (; inFlight; inFlight = inFlight.next) {
|
adamc@1042
|
636 inFlight.data.abort();
|
adamc@1042
|
637 }
|
adamc@1042
|
638 }
|
adamc@1042
|
639
|
adamc@736
|
640 function requestUri(xhr, uri, needsSig) {
|
adamc@1042
|
641 if (unloading)
|
adamc@1042
|
642 return;
|
adamc@1042
|
643
|
adamc@1037
|
644 xhr.open("POST", uri, true);
|
adamc@668
|
645
|
adamc@703
|
646 if (client_id != null) {
|
adamc@668
|
647 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
|
adamc@668
|
648 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
|
adamc@668
|
649 }
|
adamc@668
|
650
|
adamc@736
|
651 if (needsSig) {
|
adamc@736
|
652 if (sig == null)
|
adamc@736
|
653 whine("Missing cookie signature!");
|
adamc@736
|
654
|
adamc@736
|
655 xhr.setRequestHeader("UrWeb-Sig", sig);
|
adamc@736
|
656 }
|
adamc@736
|
657
|
adamc@1042
|
658 inFlight = cons(xhr, inFlight);
|
adamc@668
|
659 xhr.send(null);
|
adamc@668
|
660 }
|
adamc@668
|
661
|
adamc@1042
|
662 function xhrFinished(xhr) {
|
adamc@1042
|
663 xhr.abort();
|
adamc@1042
|
664 inFlight = remove(xhr, inFlight);
|
adamc@1042
|
665 }
|
adamc@1042
|
666
|
adamc@978
|
667 function rc(prefix, uri, parse, k, needsSig) {
|
adamc@978
|
668 uri = cat(prefix, uri);
|
adamc@728
|
669 uri = flattenLocal(uri);
|
adamc@609
|
670 var xhr = getXHR();
|
adamc@609
|
671
|
adamc@609
|
672 xhr.onreadystatechange = function() {
|
adamc@612
|
673 if (xhr.readyState == 4) {
|
adamc@612
|
674 var isok = false;
|
adamc@612
|
675
|
adamc@612
|
676 try {
|
adamc@612
|
677 if (xhr.status == 200)
|
adamc@612
|
678 isok = true;
|
adamc@612
|
679 } catch (e) { }
|
adamc@612
|
680
|
adamc@729
|
681 if (isok) {
|
adamc@729
|
682 try {
|
adamc@1020
|
683 k(parse(xhr.responseText));
|
adamc@729
|
684 } catch (v) {
|
adamc@729
|
685 doExn(v);
|
adamc@729
|
686 }
|
adamc@729
|
687 } else {
|
adamc@729
|
688 conn();
|
adamc@649
|
689 }
|
adamc@1042
|
690
|
adamc@1042
|
691 xhrFinished(xhr);
|
adamc@612
|
692 }
|
adamc@609
|
693 };
|
adamc@609
|
694
|
adamc@736
|
695 requestUri(xhr, uri, needsSig);
|
adamc@609
|
696 }
|
adamc@667
|
697
|
adamc@667
|
698 function path_join(s1, s2) {
|
adamc@1038
|
699 if (s1.length > 0 && s1.charAt(s1.length-1) == '/')
|
adamc@667
|
700 return s1 + s2;
|
adamc@667
|
701 else
|
adamc@667
|
702 return s1 + "/" + s2;
|
adamc@667
|
703 }
|
adamc@667
|
704
|
adamc@670
|
705 var channels = [];
|
adamc@670
|
706
|
adamc@670
|
707 function newQueue() {
|
adamc@670
|
708 return { front : null, back : null };
|
adamc@670
|
709 }
|
adamc@670
|
710 function enqueue(q, v) {
|
adamc@670
|
711 if (q.front == null) {
|
adamc@670
|
712 q.front = cons(v, null);
|
adamc@670
|
713 q.back = q.front;
|
adamc@670
|
714 } else {
|
adamc@670
|
715 var node = cons(v, null);
|
adamc@690
|
716 q.back.next = node;
|
adamc@670
|
717 q.back = node;
|
adamc@670
|
718 }
|
adamc@670
|
719 }
|
adamc@670
|
720 function dequeue(q) {
|
adamc@670
|
721 if (q.front == null)
|
adamc@670
|
722 return null;
|
adamc@670
|
723 else {
|
adamc@690
|
724 var r = q.front.data;
|
adamc@690
|
725 q.front = q.front.next;
|
adamc@670
|
726 if (q.front == null)
|
adamc@670
|
727 q.back = null;
|
adamc@670
|
728 return r;
|
adamc@670
|
729 }
|
adamc@670
|
730 }
|
adamc@670
|
731
|
adamc@670
|
732 function newChannel() {
|
adamc@670
|
733 return { msgs : newQueue(), listeners : newQueue() };
|
adamc@670
|
734 }
|
adamc@670
|
735
|
adamc@667
|
736 function listener() {
|
adamc@668
|
737 var uri = path_join(url_prefix, ".msgs");
|
adamc@667
|
738 var xhr = getXHR();
|
adamc@673
|
739 var tid, orsc, onTimeout;
|
adamc@673
|
740
|
adamc@673
|
741 var connect = function () {
|
adamc@673
|
742 xhr.onreadystatechange = orsc;
|
adamc@673
|
743 tid = window.setTimeout(onTimeout, timeout * 500);
|
adamc@736
|
744 requestUri(xhr, uri, false);
|
adamc@673
|
745 }
|
adamc@673
|
746
|
adamc@673
|
747 orsc = function() {
|
adamc@667
|
748 if (xhr.readyState == 4) {
|
adamc@673
|
749 window.clearTimeout(tid);
|
adamc@673
|
750
|
adamc@667
|
751 var isok = false;
|
adamc@667
|
752
|
adamc@667
|
753 try {
|
adamc@667
|
754 if (xhr.status == 200)
|
adamc@667
|
755 isok = true;
|
adamc@667
|
756 } catch (e) { }
|
adamc@667
|
757
|
adamc@668
|
758 if (isok) {
|
adamc@889
|
759 var text = xhr.responseText
|
adamc@889
|
760 if (text == "")
|
adamc@889
|
761 return;
|
adamc@889
|
762 var lines = text.split("\n");
|
adamc@889
|
763
|
adamc@729
|
764 if (lines.length < 2) {
|
adamc@729
|
765 discon();
|
adamc@729
|
766 return;
|
adamc@729
|
767 }
|
adamc@669
|
768
|
adamc@669
|
769 for (var i = 0; i+1 < lines.length; i += 2) {
|
adamc@670
|
770 var chn = lines[i];
|
adamc@670
|
771 var msg = lines[i+1];
|
adamc@670
|
772
|
adamc@670
|
773 if (chn < 0)
|
adamc@670
|
774 whine("Out-of-bounds channel in message from remote server");
|
adamc@670
|
775
|
adamc@670
|
776 var ch;
|
adamc@670
|
777
|
adamc@670
|
778 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
779 ch = newChannel();
|
adamc@670
|
780 channels[chn] = ch;
|
adamc@670
|
781 } else
|
adamc@670
|
782 ch = channels[chn];
|
adamc@670
|
783
|
adamc@670
|
784 var listener = dequeue(ch.listeners);
|
adamc@670
|
785 if (listener == null) {
|
adamc@670
|
786 enqueue(ch.msgs, msg);
|
adamc@670
|
787 } else {
|
adamc@728
|
788 try {
|
adamc@728
|
789 listener(msg);
|
adamc@728
|
790 } catch (v) {
|
adamc@728
|
791 doExn(v);
|
adamc@728
|
792 }
|
adamc@670
|
793 }
|
adamc@669
|
794 }
|
adamc@669
|
795
|
adamc@1042
|
796 xhrFinished(xhr);
|
adamc@1038
|
797
|
adamc@673
|
798 connect();
|
adamc@668
|
799 }
|
adamc@667
|
800 else {
|
adamc@729
|
801 try {
|
adamc@824
|
802 if (xhr.status != 0)
|
adamc@824
|
803 servErr("Error querying remote server for messages: " + xhr.status);
|
adamc@889
|
804 } catch (e) { }
|
adamc@667
|
805 }
|
adamc@667
|
806 }
|
adamc@667
|
807 };
|
adamc@667
|
808
|
adamc@673
|
809 onTimeout = function() {
|
adamc@1042
|
810 xhrFinished(xhr);
|
adamc@673
|
811 connect();
|
adamc@673
|
812 };
|
adamc@673
|
813
|
adamc@673
|
814 connect();
|
adamc@667
|
815 }
|
adamc@670
|
816
|
adamc@670
|
817 function rv(chn, parse, k) {
|
adamc@682
|
818 if (chn == null)
|
adamc@682
|
819 return;
|
adamc@682
|
820
|
adamc@670
|
821 if (chn < 0)
|
adamc@670
|
822 whine("Out-of-bounds channel receive");
|
adamc@670
|
823
|
adamc@670
|
824 var ch;
|
adamc@670
|
825
|
adamc@670
|
826 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
827 ch = newChannel();
|
adamc@670
|
828 channels[chn] = ch;
|
adamc@670
|
829 } else
|
adamc@670
|
830 ch = channels[chn];
|
adamc@670
|
831
|
adamc@670
|
832 var msg = dequeue(ch.msgs);
|
adamc@670
|
833 if (msg == null) {
|
adamc@1021
|
834 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
|
adamc@670
|
835 } else {
|
adamc@728
|
836 try {
|
adamc@1021
|
837 k(parse(msg));
|
adamc@728
|
838 } catch (v) {
|
adamc@728
|
839 doExn(v);
|
adamc@728
|
840 }
|
adamc@670
|
841 }
|
adamc@670
|
842 }
|
adamc@693
|
843
|
adamc@978
|
844 function sl(ms, k) {
|
adamc@1021
|
845 window.setTimeout(function() { k(null); }, ms);
|
adamc@1021
|
846 }
|
adamc@1021
|
847
|
adamc@1021
|
848 function sp(e) {
|
adamc@1021
|
849 execF(e, null);
|
adamc@978
|
850 }
|
adamc@978
|
851
|
adamc@693
|
852
|
adamc@895
|
853 // Key events
|
adamc@895
|
854
|
adamc@970
|
855 var uw_event = null;
|
adamc@970
|
856
|
adamc@970
|
857 function kc() {
|
adamc@970
|
858 return window.event ? uw_event.keyCode : uw_event.which;
|
adamc@970
|
859 }
|
adamc@970
|
860
|
adamc@970
|
861
|
adamc@970
|
862 // The Ur interpreter
|
adamc@970
|
863
|
adamc@970
|
864 var urfuncs = [];
|
adamc@970
|
865
|
adamc@970
|
866 function lookup(env, n) {
|
adamc@970
|
867 while (env != null) {
|
adamc@970
|
868 if (n == 0)
|
adamc@970
|
869 return env.data;
|
adamc@970
|
870 else {
|
adamc@970
|
871 --n;
|
adamc@970
|
872 env = env.next;
|
adamc@970
|
873 }
|
adamc@970
|
874 }
|
adamc@970
|
875
|
adamc@982
|
876 whine("Out-of-bounds Ur variable reference");
|
adamc@970
|
877 }
|
adamc@970
|
878
|
adamc@974
|
879 function execP(env, p, v) {
|
adamc@974
|
880 switch (p.c) {
|
adamc@974
|
881 case "w":
|
adamc@974
|
882 return env;
|
adamc@974
|
883 case "v":
|
adamc@974
|
884 return cons(v, env);
|
adamc@974
|
885 case "c":
|
adamc@974
|
886 if (v == p.v)
|
adamc@974
|
887 return env;
|
adamc@974
|
888 else
|
adamc@974
|
889 return false;
|
adamc@974
|
890 case "s":
|
adamc@974
|
891 if (v == null)
|
adamc@974
|
892 return false;
|
adamc@974
|
893 else
|
adamc@974
|
894 return execP(env, p.p, p.n ? v.v : v);
|
adamc@974
|
895 case "1":
|
adamc@974
|
896 if (v.n != p.n)
|
adamc@974
|
897 return false;
|
adamc@974
|
898 else
|
adamc@974
|
899 return execP(env, p.p, v.v);
|
adamc@974
|
900 case "r":
|
adamc@974
|
901 for (var fs = p.l; fs != null; fs = fs.next) {
|
adamc@974
|
902 env = execP(env, fs.data.p, v["_" + fs.data.n]);
|
adamc@974
|
903 if (env == false)
|
adamc@974
|
904 return false;
|
adamc@974
|
905 }
|
adamc@974
|
906 return env;
|
adamc@974
|
907 default:
|
adamc@982
|
908 whine("Unknown Ur pattern kind" + p.c);
|
adamc@974
|
909 }
|
adamc@974
|
910 }
|
adamc@974
|
911
|
adamc@970
|
912 function exec0(env, e) {
|
adamc@1020
|
913 return exec1(env, null, e);
|
adamc@1020
|
914 }
|
adamc@1020
|
915
|
adamc@1020
|
916 function exec1(env, stack, e) {
|
adamc@1020
|
917 var stack, usedK = false;
|
adamc@970
|
918
|
adamc@983
|
919 var saveEnv = function() {
|
adamc@983
|
920 if (stack.next != null && stack.next.data.c != "<")
|
adamc@983
|
921 stack = cons({c: "<", env: env}, stack.next);
|
adamc@983
|
922 else
|
adamc@983
|
923 stack = stack.next;
|
adamc@983
|
924 };
|
adamc@983
|
925
|
adamc@970
|
926 while (true) {
|
adamc@970
|
927 switch (e.c) {
|
adamc@970
|
928 case "c":
|
adamc@970
|
929 var v = e.v;
|
adamc@970
|
930 if (stack == null)
|
adamc@970
|
931 return v;
|
adamc@970
|
932 var fr = stack.data;
|
adamc@970
|
933
|
adamc@970
|
934 switch (fr.c) {
|
adamc@970
|
935 case "s":
|
adamc@970
|
936 e = {c: "c", v: {v: v}};
|
adamc@970
|
937 stack = stack.next;
|
adamc@970
|
938 break;
|
adamc@970
|
939 case "1":
|
adamc@970
|
940 e = {c: "c", v: {n: fr.n, v: v}};
|
adamc@970
|
941 stack = stack.next;
|
adamc@970
|
942 break;
|
adamc@970
|
943 case "f":
|
adamc@970
|
944 fr.args[fr.pos++] = v;
|
adamc@970
|
945 if (fr.a == null) {
|
adamc@1035
|
946 var res;
|
adamc@1020
|
947 stack = stack.next;
|
adamc@1035
|
948
|
adamc@1035
|
949 if (fr.f.apply)
|
adamc@1035
|
950 res = fr.f.apply(null, fr.args);
|
adamc@1035
|
951 else if (fr.args.length == 0)
|
adamc@1035
|
952 res = fr.f();
|
adamc@1035
|
953 else if (fr.args.length == 1)
|
adamc@1035
|
954 res = fr.f(fr.args[0]);
|
adamc@1035
|
955 else if (fr.args.length == 2)
|
adamc@1035
|
956 res = fr.f(fr.args[0], fr.args[1]);
|
adamc@1035
|
957 else if (fr.args.length == 3)
|
adamc@1035
|
958 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
|
adamc@1035
|
959 else if (fr.args.length == 4)
|
adamc@1035
|
960 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
|
adamc@1035
|
961 else if (fr.args.length == 5)
|
adamc@1035
|
962 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
|
adamc@1035
|
963 else
|
adamc@1035
|
964 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
|
adamc@1035
|
965
|
adamc@1035
|
966 e = {c: "c", v: res};
|
adamc@1020
|
967 if (usedK) return null;
|
adamc@970
|
968 } else {
|
adamc@970
|
969 e = fr.a.data;
|
adamc@970
|
970 fr.a = fr.a.next;
|
adamc@970
|
971 }
|
adamc@970
|
972 break;
|
adamc@970
|
973 case "a1":
|
adamc@970
|
974 e = fr.x;
|
adamc@971
|
975 stack = cons({c: "a2", f: v}, stack.next);
|
adamc@970
|
976 break;
|
adamc@970
|
977 case "a2":
|
adamc@971
|
978 if (fr.f == null)
|
adamc@982
|
979 whine("Ur: applying null function");
|
adamc@971
|
980 else if (fr.f.body) {
|
adamc@983
|
981 saveEnv();
|
adamc@971
|
982 env = cons(v, fr.f.env);
|
adamc@971
|
983 e = fr.f.body;
|
adamc@971
|
984 } else {
|
adamc@971
|
985 e = {c: "c", v: fr.f(v)};
|
adamc@971
|
986 stack = stack.next;
|
adamc@971
|
987 }
|
adamc@970
|
988 break;
|
adamc@978
|
989 case "<":
|
adamc@970
|
990 env = fr.env;
|
adamc@970
|
991 stack = stack.next;
|
adamc@970
|
992 break;
|
adamc@970
|
993 case "r":
|
adamc@970
|
994 fr.fs["_" + fr.n] = v;
|
adamc@970
|
995 if (fr.l == null) {
|
adamc@970
|
996 e = {c: "c", v: fr.fs};
|
adamc@970
|
997 stack = stack.next;
|
adamc@970
|
998 } else {
|
adamc@970
|
999 fr.n = fr.l.data.n;
|
adamc@970
|
1000 e = fr.l.data.v;
|
adamc@970
|
1001 fr.l = fr.l.next;
|
adamc@970
|
1002 }
|
adamc@970
|
1003 break;
|
adamc@970
|
1004 case ".":
|
adamc@970
|
1005 e = {c: "c", v: v["_" + fr.f]};
|
adamc@970
|
1006 stack = stack.next;
|
adamc@970
|
1007 break;
|
adamc@970
|
1008 case ";":
|
adamc@970
|
1009 e = fr.e2;
|
adamc@970
|
1010 stack = stack.next;
|
adamc@970
|
1011 break;
|
adamc@970
|
1012 case "=":
|
adamc@983
|
1013 saveEnv();
|
adamc@970
|
1014 env = cons(v, env);
|
adamc@970
|
1015 e = fr.e2;
|
adamc@970
|
1016 break;
|
adamc@974
|
1017 case "m":
|
adamc@974
|
1018 var ps;
|
adamc@974
|
1019 for (ps = fr.p; ps != null; ps = ps.next) {
|
adamc@974
|
1020 var r = execP(env, ps.data.p, v);
|
adamc@974
|
1021 if (r != false) {
|
adamc@983
|
1022 saveEnv();
|
adamc@974
|
1023 env = r;
|
adamc@974
|
1024 e = ps.data.b;
|
adamc@974
|
1025 break;
|
adamc@974
|
1026 }
|
adamc@974
|
1027 }
|
adamc@974
|
1028 if (ps == null)
|
adamc@982
|
1029 whine("Match failure in Ur interpretation");
|
adamc@974
|
1030 break;
|
adamc@970
|
1031 default:
|
adamc@982
|
1032 whine("Unknown Ur continuation kind " + fr.c);
|
adamc@970
|
1033 }
|
adamc@970
|
1034
|
adamc@970
|
1035 break;
|
adamc@970
|
1036 case "v":
|
adamc@970
|
1037 e = {c: "c", v: lookup(env, e.n)};
|
adamc@970
|
1038 break;
|
adamc@970
|
1039 case "n":
|
adamc@971
|
1040 e = urfuncs[e.n];
|
adamc@970
|
1041 break;
|
adamc@970
|
1042 case "s":
|
adamc@970
|
1043 stack = cons({c: "s"}, stack);
|
adamc@970
|
1044 e = e.v;
|
adamc@970
|
1045 break;
|
adamc@970
|
1046 case "1":
|
adamc@970
|
1047 stack = cons({c: "1", n: e.n}, stack);
|
adamc@970
|
1048 e = e.v;
|
adamc@970
|
1049 break;
|
adamc@970
|
1050 case "f":
|
adamc@970
|
1051 if (e.a == null)
|
adamc@970
|
1052 e = {c: "c", v: e.f()};
|
adamc@970
|
1053 else {
|
adamc@970
|
1054 var args = [];
|
adamc@970
|
1055 stack = cons({c: "f", f: e.f, args: args, pos: 0, a: e.a.next}, stack);
|
adamc@982
|
1056 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
|
adamc@970
|
1057 e = e.a.data;
|
adamc@970
|
1058 }
|
adamc@970
|
1059 break;
|
adamc@970
|
1060 case "l":
|
adamc@970
|
1061 e = {c: "c", v: {env: env, body: e.b}};
|
adamc@970
|
1062 break;
|
adamc@970
|
1063 case "a":
|
adamc@970
|
1064 stack = cons({c: "a1", x: e.x}, stack);
|
adamc@970
|
1065 e = e.f;
|
adamc@970
|
1066 break;
|
adamc@970
|
1067 case "r":
|
adamc@970
|
1068 if (e.l == null)
|
adamc@982
|
1069 whine("Empty Ur record in interpretation");
|
adamc@970
|
1070 var fs = {};
|
adamc@970
|
1071 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
|
adamc@973
|
1072 e = e.l.data.v;
|
adamc@970
|
1073 break;
|
adamc@970
|
1074 case ".":
|
adamc@970
|
1075 stack = cons({c: ".", f: e.f}, stack);
|
adamc@970
|
1076 e = e.r;
|
adamc@970
|
1077 break;
|
adamc@970
|
1078 case ";":
|
adamc@970
|
1079 stack = cons({c: ";", e2: e.e2}, stack);
|
adamc@970
|
1080 e = e.e1;
|
adamc@970
|
1081 break;
|
adamc@970
|
1082 case "=":
|
adamc@970
|
1083 stack = cons({c: "=", e2: e.e2}, stack);
|
adamc@970
|
1084 e = e.e1;
|
adamc@970
|
1085 break;
|
adamc@974
|
1086 case "m":
|
adamc@974
|
1087 stack = cons({c: "m", p: e.p}, stack);
|
adamc@974
|
1088 e = e.e;
|
adamc@974
|
1089 break;
|
adamc@970
|
1090 case "e":
|
adamc@975
|
1091 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
|
adamc@975
|
1092 break;
|
adamc@975
|
1093 case "wc":
|
adamc@975
|
1094 env = e.env;
|
adamc@975
|
1095 e = e.body;
|
adamc@970
|
1096 break;
|
adamc@1020
|
1097 case "K":
|
adamc@1020
|
1098 { var savedStack = stack.next, savedEnv = env;
|
adamc@1020
|
1099 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
|
adamc@1020
|
1100 usedK = true;
|
adamc@1020
|
1101 break;
|
adamc@970
|
1102 default:
|
adamc@982
|
1103 whine("Unknown Ur expression kind " + e.c);
|
adamc@970
|
1104 }
|
adamc@970
|
1105 }
|
adamc@970
|
1106 }
|
adamc@970
|
1107
|
adamc@976
|
1108 function execD(e) {
|
adamc@976
|
1109 return exec0(null, e);
|
adamc@976
|
1110 }
|
adamc@976
|
1111
|
adamc@970
|
1112 function exec(e) {
|
adamc@970
|
1113 var r = exec0(null, e);
|
adamc@970
|
1114
|
adamc@975
|
1115 if (r != null && r.body != null)
|
adamc@970
|
1116 return function(v) { return exec0(cons(v, r.env), r.body); };
|
adamc@970
|
1117 else
|
adamc@970
|
1118 return r;
|
adamc@895
|
1119 }
|
adamc@895
|
1120
|
adamc@977
|
1121 function execF(f, x) {
|
adamc@977
|
1122 return exec0(cons(x, f.env), f.body);
|
adamc@977
|
1123 }
|
adamc@977
|
1124
|
adamc@895
|
1125
|
adamc@693
|
1126 // App-specific code
|
adamc@693
|
1127
|