annotate lib/js/urweb.js @ 1310:f0909fb3848f

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