annotate lib/js/urweb.js @ 1058:86b831978b8d

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