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@1039
|
448 function input(t, s, recreate, type) {
|
adamc@598
|
449 var x = document.createElement(t);
|
adamc@1039
|
450 if (type) x.type = type;
|
adamc@690
|
451 x.dead = false;
|
adamc@690
|
452 x.signal = ss(s);
|
adamc@690
|
453 x.sources = null;
|
adamc@817
|
454 x.recreate = recreate(x);
|
adamc@899
|
455 addNode(x);
|
adamc@690
|
456 populate(x);
|
adamc@816
|
457
|
adamc@816
|
458 return x;
|
adamc@816
|
459 }
|
adamc@816
|
460
|
adamc@816
|
461 function inp(s) {
|
adamc@817
|
462 var x = input("input", s, 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@817
|
470 var x = input("select", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
|
adamc@816
|
471 x.innerHTML = content;
|
adamc@816
|
472 x.value = s.data;
|
adamc@835
|
473 if (x.value != s.data)
|
adamc@835
|
474 sv(s, x.value);
|
adamc@816
|
475 x.onchange = function() { sv(s, x.value) };
|
adamc@797
|
476
|
adamc@606
|
477 return x;
|
adamc@598
|
478 }
|
adamc@598
|
479
|
adamc@817
|
480 function chk(s) {
|
adamc@1039
|
481 var x = input("input", s, function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox");
|
adamc@817
|
482 x.checked = s.data;
|
adamc@817
|
483 x.onchange = function() { sv(s, x.checked) };
|
adamc@817
|
484
|
adamc@817
|
485 return x;
|
adamc@817
|
486 }
|
adamc@817
|
487
|
adamc@800
|
488 function addOnChange(x, f) {
|
adamc@800
|
489 var old = x.onchange;
|
adamc@800
|
490 x.onchange = function() { old(); f (); };
|
adamc@800
|
491 }
|
adamc@800
|
492
|
adamc@692
|
493
|
adamc@692
|
494 // Basic string operations
|
adamc@692
|
495
|
adamc@597
|
496 function eh(x) {
|
adamc@800
|
497 if (x == null)
|
adamc@800
|
498 return "NULL";
|
adamc@800
|
499 else
|
adamc@800
|
500 return x.split("&").join("&").split("<").join("<").split(">").join(">");
|
adamc@597
|
501 }
|
adamc@597
|
502
|
adamc@582
|
503 function ts(x) { return x.toString() }
|
adamc@586
|
504 function bs(b) { return (b ? "True" : "False") }
|
adamc@586
|
505
|
adamc@1038
|
506 function sub(s, i) { return s.charAt(i); }
|
adamc@828
|
507 function suf(s, i) { return s.substring(i); }
|
adamc@828
|
508 function slen(s) { return s.length; }
|
adamc@829
|
509 function sidx(s, ch) {
|
adamc@829
|
510 var r = s.indexOf(ch);
|
adamc@829
|
511 if (r == -1)
|
adamc@829
|
512 return null;
|
adamc@829
|
513 else
|
adamc@829
|
514 return r;
|
adamc@829
|
515 }
|
adamc@831
|
516 function sspn(s, chs) {
|
adamc@831
|
517 for (var i = 0; i < s.length; ++i)
|
adamc@1038
|
518 if (chs.indexOf(s.charAt(i)) != -1)
|
adamc@831
|
519 return i;
|
adamc@831
|
520
|
adamc@831
|
521 return null;
|
adamc@831
|
522 }
|
adamc@829
|
523 function schr(s, ch) {
|
adamc@829
|
524 var r = s.indexOf(ch);
|
adamc@829
|
525 if (r == -1)
|
adamc@829
|
526 return null;
|
adamc@829
|
527 else
|
adamc@829
|
528 return s.substring(r);
|
adamc@829
|
529 }
|
adamc@829
|
530 function ssub(s, start, len) {
|
adamc@829
|
531 return s.substring(start, start+len);
|
adamc@829
|
532 }
|
adamc@821
|
533
|
adamc@649
|
534 function pi(s) {
|
adamc@649
|
535 var r = parseInt(s);
|
adamc@649
|
536 if (r.toString() == s)
|
adamc@649
|
537 return r;
|
adamc@649
|
538 else
|
adamc@729
|
539 er("Can't parse int: " + s);
|
adamc@649
|
540 }
|
adamc@649
|
541
|
adamc@649
|
542 function pfl(s) {
|
adamc@649
|
543 var r = parseFloat(s);
|
adamc@649
|
544 if (r.toString() == s)
|
adamc@649
|
545 return r;
|
adamc@649
|
546 else
|
adamc@729
|
547 er("Can't parse float: " + s);
|
adamc@649
|
548 }
|
adamc@649
|
549
|
adamc@838
|
550 function pio(s) {
|
adamc@838
|
551 var r = parseInt(s);
|
adamc@838
|
552 if (r.toString() == s)
|
adamc@838
|
553 return r;
|
adamc@838
|
554 else
|
adamc@838
|
555 return null;
|
adamc@838
|
556 }
|
adamc@838
|
557
|
adamc@838
|
558 function pflo(s) {
|
adamc@838
|
559 var r = parseFloat(s);
|
adamc@838
|
560 if (r.toString() == s)
|
adamc@838
|
561 return r;
|
adamc@838
|
562 else
|
adamc@838
|
563 return null;
|
adamc@838
|
564 }
|
adamc@838
|
565
|
adamc@692
|
566 function uf(s) {
|
adamc@925
|
567 if (s.length == 0)
|
adamc@925
|
568 return "_";
|
adamc@1038
|
569 return (s.charAt(0) == '_' ? "_" : "")
|
adamc@925
|
570 + escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B");
|
adamc@691
|
571 }
|
adamc@691
|
572
|
adamc@692
|
573 function uu(s) {
|
adamc@1038
|
574 if (s.length > 0 && s.charAt(0) == '_') {
|
adamc@925
|
575 s = s.substring(1);
|
adamc@1038
|
576 } else if (s.length >= 3 && s.charAt(0) == '%' && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
|
adamc@927
|
577 s = s.substring(3);
|
adamc@905
|
578 return unescape(s.replace(new RegExp ("\\+", "g"), " "));
|
adamc@905
|
579 }
|
adamc@905
|
580
|
adamc@912
|
581 function ub(b) {
|
adamc@912
|
582 return b ? "1" : "0";
|
adamc@912
|
583 }
|
adamc@912
|
584
|
adamc@905
|
585 function uul(getToken, getData) {
|
adamc@905
|
586 var tok = getToken();
|
adamc@905
|
587 if (tok == "Nil") {
|
adamc@905
|
588 return null;
|
adamc@905
|
589 } else if (tok == "Cons") {
|
adamc@905
|
590 var d = getData();
|
adamc@905
|
591 var l = uul(getToken, getData);
|
adamc@905
|
592 return {_1:d, _2:l};
|
adamc@905
|
593 } else
|
adamc@982
|
594 whine("Can't unmarshal list (" + tok + ")");
|
adamc@692
|
595 }
|
adamc@692
|
596
|
adamc@961
|
597 function strcmp(str1, str2) {
|
adamc@961
|
598 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
|
adamc@961
|
599 }
|
adamc@692
|
600
|
adamc@603
|
601
|
adamc@692
|
602 // Remote calls
|
adamc@609
|
603
|
adamc@703
|
604 var client_id = null;
|
adamc@668
|
605 var client_pass = 0;
|
adamc@668
|
606 var url_prefix = "/";
|
adamc@673
|
607 var timeout = 60;
|
adamc@668
|
608
|
adamc@668
|
609 function getXHR(uri)
|
adamc@609
|
610 {
|
adamc@609
|
611 try {
|
adamc@609
|
612 return new XMLHttpRequest();
|
adamc@609
|
613 } catch (e) {
|
adamc@609
|
614 try {
|
adamc@609
|
615 return new ActiveXObject("Msxml2.XMLHTTP");
|
adamc@609
|
616 } catch (e) {
|
adamc@609
|
617 try {
|
adamc@609
|
618 return new ActiveXObject("Microsoft.XMLHTTP");
|
adamc@609
|
619 } catch (e) {
|
adamc@982
|
620 whine("Your browser doesn't seem to support AJAX.");
|
adamc@609
|
621 }
|
adamc@609
|
622 }
|
adamc@609
|
623 }
|
adamc@609
|
624 }
|
adamc@609
|
625
|
adamc@736
|
626 var sig = null;
|
adamc@736
|
627
|
adamc@736
|
628 function requestUri(xhr, uri, needsSig) {
|
adamc@1037
|
629 xhr.open("POST", uri, true);
|
adamc@668
|
630
|
adamc@703
|
631 if (client_id != null) {
|
adamc@668
|
632 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
|
adamc@668
|
633 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
|
adamc@668
|
634 }
|
adamc@668
|
635
|
adamc@736
|
636 if (needsSig) {
|
adamc@736
|
637 if (sig == null)
|
adamc@736
|
638 whine("Missing cookie signature!");
|
adamc@736
|
639
|
adamc@736
|
640 xhr.setRequestHeader("UrWeb-Sig", sig);
|
adamc@736
|
641 }
|
adamc@736
|
642
|
adamc@668
|
643 xhr.send(null);
|
adamc@668
|
644 }
|
adamc@668
|
645
|
adamc@978
|
646 function rc(prefix, uri, parse, k, needsSig) {
|
adamc@978
|
647 uri = cat(prefix, uri);
|
adamc@728
|
648 uri = flattenLocal(uri);
|
adamc@609
|
649 var xhr = getXHR();
|
adamc@609
|
650
|
adamc@609
|
651 xhr.onreadystatechange = function() {
|
adamc@612
|
652 if (xhr.readyState == 4) {
|
adamc@612
|
653 var isok = false;
|
adamc@612
|
654
|
adamc@612
|
655 try {
|
adamc@612
|
656 if (xhr.status == 200)
|
adamc@612
|
657 isok = true;
|
adamc@612
|
658 } catch (e) { }
|
adamc@612
|
659
|
adamc@729
|
660 if (isok) {
|
adamc@729
|
661 try {
|
adamc@1020
|
662 k(parse(xhr.responseText));
|
adamc@729
|
663 } catch (v) {
|
adamc@729
|
664 doExn(v);
|
adamc@729
|
665 }
|
adamc@729
|
666 } else {
|
adamc@729
|
667 conn();
|
adamc@649
|
668 }
|
adamc@612
|
669 }
|
adamc@609
|
670 };
|
adamc@609
|
671
|
adamc@736
|
672 requestUri(xhr, uri, needsSig);
|
adamc@609
|
673 }
|
adamc@667
|
674
|
adamc@667
|
675 function path_join(s1, s2) {
|
adamc@1038
|
676 if (s1.length > 0 && s1.charAt(s1.length-1) == '/')
|
adamc@667
|
677 return s1 + s2;
|
adamc@667
|
678 else
|
adamc@667
|
679 return s1 + "/" + s2;
|
adamc@667
|
680 }
|
adamc@667
|
681
|
adamc@670
|
682 var channels = [];
|
adamc@670
|
683
|
adamc@670
|
684 function newQueue() {
|
adamc@670
|
685 return { front : null, back : null };
|
adamc@670
|
686 }
|
adamc@670
|
687 function enqueue(q, v) {
|
adamc@670
|
688 if (q.front == null) {
|
adamc@670
|
689 q.front = cons(v, null);
|
adamc@670
|
690 q.back = q.front;
|
adamc@670
|
691 } else {
|
adamc@670
|
692 var node = cons(v, null);
|
adamc@690
|
693 q.back.next = node;
|
adamc@670
|
694 q.back = node;
|
adamc@670
|
695 }
|
adamc@670
|
696 }
|
adamc@670
|
697 function dequeue(q) {
|
adamc@670
|
698 if (q.front == null)
|
adamc@670
|
699 return null;
|
adamc@670
|
700 else {
|
adamc@690
|
701 var r = q.front.data;
|
adamc@690
|
702 q.front = q.front.next;
|
adamc@670
|
703 if (q.front == null)
|
adamc@670
|
704 q.back = null;
|
adamc@670
|
705 return r;
|
adamc@670
|
706 }
|
adamc@670
|
707 }
|
adamc@670
|
708
|
adamc@670
|
709 function newChannel() {
|
adamc@670
|
710 return { msgs : newQueue(), listeners : newQueue() };
|
adamc@670
|
711 }
|
adamc@670
|
712
|
adamc@667
|
713 function listener() {
|
adamc@668
|
714 var uri = path_join(url_prefix, ".msgs");
|
adamc@667
|
715 var xhr = getXHR();
|
adamc@673
|
716 var tid, orsc, onTimeout;
|
adamc@673
|
717
|
adamc@673
|
718 var connect = function () {
|
adamc@673
|
719 xhr.onreadystatechange = orsc;
|
adamc@673
|
720 tid = window.setTimeout(onTimeout, timeout * 500);
|
adamc@736
|
721 requestUri(xhr, uri, false);
|
adamc@673
|
722 }
|
adamc@673
|
723
|
adamc@673
|
724 orsc = function() {
|
adamc@667
|
725 if (xhr.readyState == 4) {
|
adamc@673
|
726 window.clearTimeout(tid);
|
adamc@673
|
727
|
adamc@667
|
728 var isok = false;
|
adamc@667
|
729
|
adamc@667
|
730 try {
|
adamc@667
|
731 if (xhr.status == 200)
|
adamc@667
|
732 isok = true;
|
adamc@667
|
733 } catch (e) { }
|
adamc@667
|
734
|
adamc@668
|
735 if (isok) {
|
adamc@889
|
736 var text = xhr.responseText
|
adamc@889
|
737 if (text == "")
|
adamc@889
|
738 return;
|
adamc@889
|
739 var lines = text.split("\n");
|
adamc@889
|
740
|
adamc@729
|
741 if (lines.length < 2) {
|
adamc@729
|
742 discon();
|
adamc@729
|
743 return;
|
adamc@729
|
744 }
|
adamc@669
|
745
|
adamc@669
|
746 for (var i = 0; i+1 < lines.length; i += 2) {
|
adamc@670
|
747 var chn = lines[i];
|
adamc@670
|
748 var msg = lines[i+1];
|
adamc@670
|
749
|
adamc@670
|
750 if (chn < 0)
|
adamc@670
|
751 whine("Out-of-bounds channel in message from remote server");
|
adamc@670
|
752
|
adamc@670
|
753 var ch;
|
adamc@670
|
754
|
adamc@670
|
755 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
756 ch = newChannel();
|
adamc@670
|
757 channels[chn] = ch;
|
adamc@670
|
758 } else
|
adamc@670
|
759 ch = channels[chn];
|
adamc@670
|
760
|
adamc@670
|
761 var listener = dequeue(ch.listeners);
|
adamc@670
|
762 if (listener == null) {
|
adamc@670
|
763 enqueue(ch.msgs, msg);
|
adamc@670
|
764 } else {
|
adamc@728
|
765 try {
|
adamc@728
|
766 listener(msg);
|
adamc@728
|
767 } catch (v) {
|
adamc@728
|
768 doExn(v);
|
adamc@728
|
769 }
|
adamc@670
|
770 }
|
adamc@669
|
771 }
|
adamc@669
|
772
|
adamc@1038
|
773 xhr.abort();
|
adamc@1038
|
774
|
adamc@673
|
775 connect();
|
adamc@668
|
776 }
|
adamc@667
|
777 else {
|
adamc@729
|
778 try {
|
adamc@824
|
779 if (xhr.status != 0)
|
adamc@824
|
780 servErr("Error querying remote server for messages: " + xhr.status);
|
adamc@889
|
781 } catch (e) { }
|
adamc@667
|
782 }
|
adamc@667
|
783 }
|
adamc@667
|
784 };
|
adamc@667
|
785
|
adamc@673
|
786 onTimeout = function() {
|
adamc@673
|
787 xhr.abort();
|
adamc@673
|
788 connect();
|
adamc@673
|
789 };
|
adamc@673
|
790
|
adamc@673
|
791 connect();
|
adamc@667
|
792 }
|
adamc@670
|
793
|
adamc@670
|
794 function rv(chn, parse, k) {
|
adamc@682
|
795 if (chn == null)
|
adamc@682
|
796 return;
|
adamc@682
|
797
|
adamc@670
|
798 if (chn < 0)
|
adamc@670
|
799 whine("Out-of-bounds channel receive");
|
adamc@670
|
800
|
adamc@670
|
801 var ch;
|
adamc@670
|
802
|
adamc@670
|
803 if (chn >= channels.length || channels[chn] == null) {
|
adamc@670
|
804 ch = newChannel();
|
adamc@670
|
805 channels[chn] = ch;
|
adamc@670
|
806 } else
|
adamc@670
|
807 ch = channels[chn];
|
adamc@670
|
808
|
adamc@670
|
809 var msg = dequeue(ch.msgs);
|
adamc@670
|
810 if (msg == null) {
|
adamc@1021
|
811 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
|
adamc@670
|
812 } else {
|
adamc@728
|
813 try {
|
adamc@1021
|
814 k(parse(msg));
|
adamc@728
|
815 } catch (v) {
|
adamc@728
|
816 doExn(v);
|
adamc@728
|
817 }
|
adamc@670
|
818 }
|
adamc@670
|
819 }
|
adamc@693
|
820
|
adamc@978
|
821 function sl(ms, k) {
|
adamc@1021
|
822 window.setTimeout(function() { k(null); }, ms);
|
adamc@1021
|
823 }
|
adamc@1021
|
824
|
adamc@1021
|
825 function sp(e) {
|
adamc@1021
|
826 execF(e, null);
|
adamc@978
|
827 }
|
adamc@978
|
828
|
adamc@693
|
829
|
adamc@895
|
830 // Key events
|
adamc@895
|
831
|
adamc@970
|
832 var uw_event = null;
|
adamc@970
|
833
|
adamc@970
|
834 function kc() {
|
adamc@970
|
835 return window.event ? uw_event.keyCode : uw_event.which;
|
adamc@970
|
836 }
|
adamc@970
|
837
|
adamc@970
|
838
|
adamc@970
|
839 // The Ur interpreter
|
adamc@970
|
840
|
adamc@970
|
841 var urfuncs = [];
|
adamc@970
|
842
|
adamc@970
|
843 function lookup(env, n) {
|
adamc@970
|
844 while (env != null) {
|
adamc@970
|
845 if (n == 0)
|
adamc@970
|
846 return env.data;
|
adamc@970
|
847 else {
|
adamc@970
|
848 --n;
|
adamc@970
|
849 env = env.next;
|
adamc@970
|
850 }
|
adamc@970
|
851 }
|
adamc@970
|
852
|
adamc@982
|
853 whine("Out-of-bounds Ur variable reference");
|
adamc@970
|
854 }
|
adamc@970
|
855
|
adamc@974
|
856 function execP(env, p, v) {
|
adamc@974
|
857 switch (p.c) {
|
adamc@974
|
858 case "w":
|
adamc@974
|
859 return env;
|
adamc@974
|
860 case "v":
|
adamc@974
|
861 return cons(v, env);
|
adamc@974
|
862 case "c":
|
adamc@974
|
863 if (v == p.v)
|
adamc@974
|
864 return env;
|
adamc@974
|
865 else
|
adamc@974
|
866 return false;
|
adamc@974
|
867 case "s":
|
adamc@974
|
868 if (v == null)
|
adamc@974
|
869 return false;
|
adamc@974
|
870 else
|
adamc@974
|
871 return execP(env, p.p, p.n ? v.v : v);
|
adamc@974
|
872 case "1":
|
adamc@974
|
873 if (v.n != p.n)
|
adamc@974
|
874 return false;
|
adamc@974
|
875 else
|
adamc@974
|
876 return execP(env, p.p, v.v);
|
adamc@974
|
877 case "r":
|
adamc@974
|
878 for (var fs = p.l; fs != null; fs = fs.next) {
|
adamc@974
|
879 env = execP(env, fs.data.p, v["_" + fs.data.n]);
|
adamc@974
|
880 if (env == false)
|
adamc@974
|
881 return false;
|
adamc@974
|
882 }
|
adamc@974
|
883 return env;
|
adamc@974
|
884 default:
|
adamc@982
|
885 whine("Unknown Ur pattern kind" + p.c);
|
adamc@974
|
886 }
|
adamc@974
|
887 }
|
adamc@974
|
888
|
adamc@970
|
889 function exec0(env, e) {
|
adamc@1020
|
890 return exec1(env, null, e);
|
adamc@1020
|
891 }
|
adamc@1020
|
892
|
adamc@1020
|
893 function exec1(env, stack, e) {
|
adamc@1020
|
894 var stack, usedK = false;
|
adamc@970
|
895
|
adamc@983
|
896 var saveEnv = function() {
|
adamc@983
|
897 if (stack.next != null && stack.next.data.c != "<")
|
adamc@983
|
898 stack = cons({c: "<", env: env}, stack.next);
|
adamc@983
|
899 else
|
adamc@983
|
900 stack = stack.next;
|
adamc@983
|
901 };
|
adamc@983
|
902
|
adamc@970
|
903 while (true) {
|
adamc@970
|
904 switch (e.c) {
|
adamc@970
|
905 case "c":
|
adamc@970
|
906 var v = e.v;
|
adamc@970
|
907 if (stack == null)
|
adamc@970
|
908 return v;
|
adamc@970
|
909 var fr = stack.data;
|
adamc@970
|
910
|
adamc@970
|
911 switch (fr.c) {
|
adamc@970
|
912 case "s":
|
adamc@970
|
913 e = {c: "c", v: {v: v}};
|
adamc@970
|
914 stack = stack.next;
|
adamc@970
|
915 break;
|
adamc@970
|
916 case "1":
|
adamc@970
|
917 e = {c: "c", v: {n: fr.n, v: v}};
|
adamc@970
|
918 stack = stack.next;
|
adamc@970
|
919 break;
|
adamc@970
|
920 case "f":
|
adamc@970
|
921 fr.args[fr.pos++] = v;
|
adamc@970
|
922 if (fr.a == null) {
|
adamc@1035
|
923 var res;
|
adamc@1020
|
924 stack = stack.next;
|
adamc@1035
|
925
|
adamc@1035
|
926 if (fr.f.apply)
|
adamc@1035
|
927 res = fr.f.apply(null, fr.args);
|
adamc@1035
|
928 else if (fr.args.length == 0)
|
adamc@1035
|
929 res = fr.f();
|
adamc@1035
|
930 else if (fr.args.length == 1)
|
adamc@1035
|
931 res = fr.f(fr.args[0]);
|
adamc@1035
|
932 else if (fr.args.length == 2)
|
adamc@1035
|
933 res = fr.f(fr.args[0], fr.args[1]);
|
adamc@1035
|
934 else if (fr.args.length == 3)
|
adamc@1035
|
935 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
|
adamc@1035
|
936 else if (fr.args.length == 4)
|
adamc@1035
|
937 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
|
adamc@1035
|
938 else if (fr.args.length == 5)
|
adamc@1035
|
939 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
|
adamc@1035
|
940 else
|
adamc@1035
|
941 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
|
adamc@1035
|
942
|
adamc@1035
|
943 e = {c: "c", v: res};
|
adamc@1020
|
944 if (usedK) return null;
|
adamc@970
|
945 } else {
|
adamc@970
|
946 e = fr.a.data;
|
adamc@970
|
947 fr.a = fr.a.next;
|
adamc@970
|
948 }
|
adamc@970
|
949 break;
|
adamc@970
|
950 case "a1":
|
adamc@970
|
951 e = fr.x;
|
adamc@971
|
952 stack = cons({c: "a2", f: v}, stack.next);
|
adamc@970
|
953 break;
|
adamc@970
|
954 case "a2":
|
adamc@971
|
955 if (fr.f == null)
|
adamc@982
|
956 whine("Ur: applying null function");
|
adamc@971
|
957 else if (fr.f.body) {
|
adamc@983
|
958 saveEnv();
|
adamc@971
|
959 env = cons(v, fr.f.env);
|
adamc@971
|
960 e = fr.f.body;
|
adamc@971
|
961 } else {
|
adamc@971
|
962 e = {c: "c", v: fr.f(v)};
|
adamc@971
|
963 stack = stack.next;
|
adamc@971
|
964 }
|
adamc@970
|
965 break;
|
adamc@978
|
966 case "<":
|
adamc@970
|
967 env = fr.env;
|
adamc@970
|
968 stack = stack.next;
|
adamc@970
|
969 break;
|
adamc@970
|
970 case "r":
|
adamc@970
|
971 fr.fs["_" + fr.n] = v;
|
adamc@970
|
972 if (fr.l == null) {
|
adamc@970
|
973 e = {c: "c", v: fr.fs};
|
adamc@970
|
974 stack = stack.next;
|
adamc@970
|
975 } else {
|
adamc@970
|
976 fr.n = fr.l.data.n;
|
adamc@970
|
977 e = fr.l.data.v;
|
adamc@970
|
978 fr.l = fr.l.next;
|
adamc@970
|
979 }
|
adamc@970
|
980 break;
|
adamc@970
|
981 case ".":
|
adamc@970
|
982 e = {c: "c", v: v["_" + fr.f]};
|
adamc@970
|
983 stack = stack.next;
|
adamc@970
|
984 break;
|
adamc@970
|
985 case ";":
|
adamc@970
|
986 e = fr.e2;
|
adamc@970
|
987 stack = stack.next;
|
adamc@970
|
988 break;
|
adamc@970
|
989 case "=":
|
adamc@983
|
990 saveEnv();
|
adamc@970
|
991 env = cons(v, env);
|
adamc@970
|
992 e = fr.e2;
|
adamc@970
|
993 break;
|
adamc@974
|
994 case "m":
|
adamc@974
|
995 var ps;
|
adamc@974
|
996 for (ps = fr.p; ps != null; ps = ps.next) {
|
adamc@974
|
997 var r = execP(env, ps.data.p, v);
|
adamc@974
|
998 if (r != false) {
|
adamc@983
|
999 saveEnv();
|
adamc@974
|
1000 env = r;
|
adamc@974
|
1001 e = ps.data.b;
|
adamc@974
|
1002 break;
|
adamc@974
|
1003 }
|
adamc@974
|
1004 }
|
adamc@974
|
1005 if (ps == null)
|
adamc@982
|
1006 whine("Match failure in Ur interpretation");
|
adamc@974
|
1007 break;
|
adamc@970
|
1008 default:
|
adamc@982
|
1009 whine("Unknown Ur continuation kind " + fr.c);
|
adamc@970
|
1010 }
|
adamc@970
|
1011
|
adamc@970
|
1012 break;
|
adamc@970
|
1013 case "v":
|
adamc@970
|
1014 e = {c: "c", v: lookup(env, e.n)};
|
adamc@970
|
1015 break;
|
adamc@970
|
1016 case "n":
|
adamc@971
|
1017 e = urfuncs[e.n];
|
adamc@970
|
1018 break;
|
adamc@970
|
1019 case "s":
|
adamc@970
|
1020 stack = cons({c: "s"}, stack);
|
adamc@970
|
1021 e = e.v;
|
adamc@970
|
1022 break;
|
adamc@970
|
1023 case "1":
|
adamc@970
|
1024 stack = cons({c: "1", n: e.n}, stack);
|
adamc@970
|
1025 e = e.v;
|
adamc@970
|
1026 break;
|
adamc@970
|
1027 case "f":
|
adamc@970
|
1028 if (e.a == null)
|
adamc@970
|
1029 e = {c: "c", v: e.f()};
|
adamc@970
|
1030 else {
|
adamc@970
|
1031 var args = [];
|
adamc@970
|
1032 stack = cons({c: "f", f: e.f, args: args, pos: 0, a: e.a.next}, stack);
|
adamc@982
|
1033 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
|
adamc@970
|
1034 e = e.a.data;
|
adamc@970
|
1035 }
|
adamc@970
|
1036 break;
|
adamc@970
|
1037 case "l":
|
adamc@970
|
1038 e = {c: "c", v: {env: env, body: e.b}};
|
adamc@970
|
1039 break;
|
adamc@970
|
1040 case "a":
|
adamc@970
|
1041 stack = cons({c: "a1", x: e.x}, stack);
|
adamc@970
|
1042 e = e.f;
|
adamc@970
|
1043 break;
|
adamc@970
|
1044 case "r":
|
adamc@970
|
1045 if (e.l == null)
|
adamc@982
|
1046 whine("Empty Ur record in interpretation");
|
adamc@970
|
1047 var fs = {};
|
adamc@970
|
1048 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
|
adamc@973
|
1049 e = e.l.data.v;
|
adamc@970
|
1050 break;
|
adamc@970
|
1051 case ".":
|
adamc@970
|
1052 stack = cons({c: ".", f: e.f}, stack);
|
adamc@970
|
1053 e = e.r;
|
adamc@970
|
1054 break;
|
adamc@970
|
1055 case ";":
|
adamc@970
|
1056 stack = cons({c: ";", e2: e.e2}, stack);
|
adamc@970
|
1057 e = e.e1;
|
adamc@970
|
1058 break;
|
adamc@970
|
1059 case "=":
|
adamc@970
|
1060 stack = cons({c: "=", e2: e.e2}, stack);
|
adamc@970
|
1061 e = e.e1;
|
adamc@970
|
1062 break;
|
adamc@974
|
1063 case "m":
|
adamc@974
|
1064 stack = cons({c: "m", p: e.p}, stack);
|
adamc@974
|
1065 e = e.e;
|
adamc@974
|
1066 break;
|
adamc@970
|
1067 case "e":
|
adamc@975
|
1068 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
|
adamc@975
|
1069 break;
|
adamc@975
|
1070 case "wc":
|
adamc@975
|
1071 env = e.env;
|
adamc@975
|
1072 e = e.body;
|
adamc@970
|
1073 break;
|
adamc@1020
|
1074 case "K":
|
adamc@1020
|
1075 { var savedStack = stack.next, savedEnv = env;
|
adamc@1020
|
1076 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
|
adamc@1020
|
1077 usedK = true;
|
adamc@1020
|
1078 break;
|
adamc@970
|
1079 default:
|
adamc@982
|
1080 whine("Unknown Ur expression kind " + e.c);
|
adamc@970
|
1081 }
|
adamc@970
|
1082 }
|
adamc@970
|
1083 }
|
adamc@970
|
1084
|
adamc@976
|
1085 function execD(e) {
|
adamc@976
|
1086 return exec0(null, e);
|
adamc@976
|
1087 }
|
adamc@976
|
1088
|
adamc@970
|
1089 function exec(e) {
|
adamc@970
|
1090 var r = exec0(null, e);
|
adamc@970
|
1091
|
adamc@975
|
1092 if (r != null && r.body != null)
|
adamc@970
|
1093 return function(v) { return exec0(cons(v, r.env), r.body); };
|
adamc@970
|
1094 else
|
adamc@970
|
1095 return r;
|
adamc@895
|
1096 }
|
adamc@895
|
1097
|
adamc@977
|
1098 function execF(f, x) {
|
adamc@977
|
1099 return exec0(cons(x, f.env), f.body);
|
adamc@977
|
1100 }
|
adamc@977
|
1101
|
adamc@895
|
1102
|
adamc@693
|
1103 // App-specific code
|
adamc@693
|
1104
|