annotate lib/js/urweb.js @ 1284:43ca083678f8

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