annotate lib/js/urweb.js @ 603:b1064de2b1f9

dlist example working
author Adam Chlipala <adamc@hcoop.net>
date Fri, 16 Jan 2009 15:49:10 -0500
parents 1d34d916c206
children 20c083327364
rev   line source
adamc@580 1 function cons(v, ls) {
adamc@580 2 return { n : ls, v : v };
adamc@580 3 }
adamc@579 4 function callAll(ls) {
adamc@585 5 for (; ls; ls = ls.n)
adamc@579 6 ls.v();
adamc@579 7 }
adamc@574 8
adamc@580 9 function sc(v) {
adamc@580 10 return {v : v, h : null};
adamc@580 11 }
adamc@580 12 function sv(s, v) {
adamc@580 13 s.v = v;
adamc@580 14 callAll(s.h);
adamc@580 15 }
adamc@601 16 function sg(s) {
adamc@601 17 return s.v;
adamc@601 18 }
adamc@579 19
adamc@580 20 function ss(s) {
adamc@580 21 return s;
adamc@580 22 }
adamc@580 23 function sr(v) {
adamc@580 24 return {v : v, h : null};
adamc@580 25 }
adamc@580 26 function sb(x,y) {
adamc@580 27 var z = y(x.v);
adamc@580 28 var s = {v : z.v, h : null};
adamc@580 29
adamc@580 30 function reZ() {
adamc@580 31 z.h = cons(function() { s.v = z.v; callAll(s.h); }, z.h);
adamc@580 32 }
adamc@580 33
adamc@580 34 x.h = cons(function() { z = y(x.v); reZ(); s.v = z.v; callAll(s.h); }, x.h);
adamc@580 35 reZ();
adamc@580 36
adamc@580 37 return s;
adamc@580 38 }
adamc@571 39
adamc@603 40 function lastParent(pos) {
adamc@600 41 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@600 42 pos = pos.lastChild;
adamc@600 43
adamc@600 44 return pos.parentNode;
adamc@600 45 }
adamc@600 46
adamc@603 47 var parents = null;
adamc@603 48
adamc@603 49 function pushParent(node) {
adamc@603 50 parents = cons(node, parents);
adamc@603 51 }
adamc@603 52
adamc@603 53 function popParent() {
adamc@603 54 if (parents)
adamc@603 55 parents = parents.n;
adamc@603 56 else
adamc@603 57 alert("popParent: stack underflow");
adamc@603 58 }
adamc@603 59
adamc@603 60 function curParent() {
adamc@603 61 return lastParent(parents ? parents.v : document);
adamc@603 62 }
adamc@603 63
adamc@603 64 function populate(node, html) {
adamc@603 65 node.innerHTML = html;
adamc@603 66
adamc@603 67 var scripts = node.getElementsByTagName("script");
adamc@603 68 var len = scripts.length;
adamc@603 69 for (var i = 0; i < len; ++i) {
adamc@603 70 pushParent(scripts[i].parentNode);
adamc@603 71 eval(scripts[i].textContent);
adamc@603 72 popParent();
adamc@603 73 }
adamc@603 74 }
adamc@603 75
adamc@571 76 function dyn(s) {
adamc@571 77 var x = document.createElement("span");
adamc@571 78 x.innerHTML = s.v;
adamc@603 79 curParent().appendChild(x);
adamc@603 80 s.h = cons(function() { populate(x, s.v) }, s.h);
adamc@571 81 }
adamc@582 82
adamc@598 83 function inp(t, s) {
adamc@598 84 var x = document.createElement(t);
adamc@598 85 x.value = s.v;
adamc@603 86 curParent().appendChild(x);
adamc@598 87 s.h = cons(function() { x.value = s.v }, s.h);
adamc@598 88 x.onkeyup = function() { sv(s, x.value) };
adamc@598 89 }
adamc@598 90
adamc@597 91 function eh(x) {
adamc@597 92 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 93 }
adamc@597 94
adamc@582 95 function ts(x) { return x.toString() }
adamc@586 96 function bs(b) { return (b ? "True" : "False") }
adamc@586 97
adamc@584 98 function pf() { alert("Pattern match failure") }
adamc@589 99
adamc@603 100 var closures = [];
adamc@603 101
adamc@603 102 function ca(f) {
adamc@603 103 var n = closures.length;
adamc@603 104 closures[n] = f;
adamc@603 105 return n;
adamc@603 106 }
adamc@603 107
adamc@603 108 function cr(n) {
adamc@603 109 return closures[n]();
adamc@603 110 }
adamc@603 111