comparison lib/js/urweb.js @ 976:68eda5b0636d

demo/react works with interpretation
author Adam Chlipala <adamc@hcoop.net>
date Tue, 22 Sep 2009 17:12:20 -0400
parents 8fe576c0bee9
children 2ecf308772ba
comparison
equal deleted inserted replaced
975:8fe576c0bee9 976:68eda5b0636d
80 function runHandlers(kind, ls, arg) { 80 function runHandlers(kind, ls, arg) {
81 if (ls == null) 81 if (ls == null)
82 alert(kind + ": " + arg); 82 alert(kind + ": " + arg);
83 for (; ls; ls = ls.next) 83 for (; ls; ls = ls.next)
84 try { 84 try {
85 ls.data(arg)(null); 85 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
86 } catch (v) { } 86 } catch (v) { }
87 } 87 }
88 88
89 var errorHandlers = null; 89 var errorHandlers = null;
90 90
112 } 112 }
113 } 113 }
114 114
115 var disconnectHandlers = null; 115 var disconnectHandlers = null;
116 116
117 function flift(f) {
118 return {c:"l", b:f};
119 }
120
117 function onDisconnect(f) { 121 function onDisconnect(f) {
118 disconnectHandlers = cons(function (_){return f}, disconnectHandlers); 122 disconnectHandlers = cons(flift(f), disconnectHandlers);
119 } 123 }
120 124
121 function discon() { 125 function discon() {
122 runHandlers("Disconnect", disconnectHandlers, null); 126 runHandlers("Disconnect", disconnectHandlers, null);
123 } 127 }
124 128
125 var connectHandlers = null; 129 var connectHandlers = null;
126 130
127 function onConnectFail(f) { 131 function onConnectFail(f) {
128 connectHandlers = cons(function (_){return f}, connectHandlers); 132 connectHandlers = cons(flift(f), connectHandlers);
129 } 133 }
130 134
131 function conn() { 135 function conn() {
132 runHandlers("Connect", connectHandlers, null); 136 runHandlers("Connect", connectHandlers, null);
133 } 137 }
205 209
206 210
207 211
208 // Dynamic tree management 212 // Dynamic tree management
209 213
214 var trace = "";
215
210 function populate(node) { 216 function populate(node) {
211 var s = node.signal; 217 var s = node.signal;
212 var oldSources = node.sources; 218 var oldSources = node.sources;
213 try { 219 try {
214 var sr = s(); 220 var sr = exec0(s.env, s.body);
215 var newSources = sr.sources; 221 var newSources = sr._sources;
216 222
217 for (var sp = oldSources; sp; sp = sp.next) 223 for (var sp = oldSources; sp; sp = sp.next)
218 if (!member(sp.data, newSources)) 224 if (!member(sp.data, newSources))
219 sp.data.dyns = remove(node, sp.data.dyns); 225 sp.data.dyns = remove(node, sp.data.dyns);
220 226
221 for (var sp = newSources; sp; sp = sp.next) 227 for (var sp = newSources; sp; sp = sp.next)
222 if (!member(sp.data, oldSources)) 228 if (!member(sp.data, oldSources))
223 sp.data.dyns = cons(node, sp.data.dyns); 229 sp.data.dyns = cons(node, sp.data.dyns);
224 230
225 node.sources = newSources; 231 node.sources = newSources;
226 node.recreate(sr.data); 232 node.recreate(sr._data);
227 } catch (v) { 233 } catch (v) {
228 doExn(v); 234 doExn(v);
229 } 235 }
230 } 236 }
231 237
242 function sg(s) { 248 function sg(s) {
243 return s.data; 249 return s.data;
244 } 250 }
245 251
246 function ss(s) { 252 function ss(s) {
247 return function() { return {sources : cons(s, null), data : s.data } }; 253 return {env:cons(s, null), body:{c:"r", l:
254 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
255 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
248 } 256 }
249 function sr(v) { 257 function sr(v) {
250 return function() { return {sources : null, data : v } }; 258 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
251 } 259 }
252 function sb(x,y) { 260 function sb(x,y) {
253 return function() { 261 return {env:cons(y,cons(x,null)),
254 var xr = x(); 262 body:{c:"=",
255 var yr = y(xr.data)(); 263 e1:{c:"a", f:{c:"v", n:1}, x:{c:"c", v:null}},
256 return {sources : union(xr.sources, yr.sources), data : yr.data}; 264 e2:{c:"=",
257 }; 265 e1:{c:"a",
266 f:{c:"a", f:{c:"v", n:1}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
267 x:{c:"c", v:null}},
268 e2:{c:"r", l:cons(
269 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
270 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
271 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
258 } 272 }
259 function scur(s) { 273 function scur(s) {
260 return s().data; 274 return exec(s).data;
261 } 275 }
262 276
263 function lastParent() { 277 function lastParent() {
264 var pos = document; 278 var pos = document;
265 279
838 852
839 function exec0(env, e) { 853 function exec0(env, e) {
840 var stack = null; 854 var stack = null;
841 855
842 while (true) { 856 while (true) {
857 if (!e.c)
858 throw ("Trace: " + trace);
859 trace += "|" + e.c;
843 switch (e.c) { 860 switch (e.c) {
844 case "c": 861 case "c":
845 var v = e.v; 862 var v = e.v;
846 if (stack == null) 863 if (stack == null)
847 return v; 864 return v;
861 if (fr.a == null) { 878 if (fr.a == null) {
862 e = {c: "c", v: fr.f.apply(null, fr.args)}; 879 e = {c: "c", v: fr.f.apply(null, fr.args)};
863 stack = stack.next; 880 stack = stack.next;
864 } else { 881 } else {
865 e = fr.a.data; 882 e = fr.a.data;
866 if (e == null) alert("Oh no!");
867 fr.a = fr.a.next; 883 fr.a = fr.a.next;
868 } 884 }
869 break; 885 break;
870 case "a1": 886 case "a1":
871 e = fr.x; 887 e = fr.x;
905 case ";": 921 case ";":
906 e = fr.e2; 922 e = fr.e2;
907 stack = stack.next; 923 stack = stack.next;
908 break; 924 break;
909 case "=": 925 case "=":
926 stack = cons({c: "a3", env: env}, stack.next);
910 env = cons(v, env); 927 env = cons(v, env);
911 e = fr.e2; 928 e = fr.e2;
912 stack = cons({c: "a3", env: env}, stack.next);
913 break; 929 break;
914 case "m": 930 case "m":
915 var ps; 931 var ps;
916 for (ps = fr.p; ps != null; ps = ps.next) { 932 for (ps = fr.p; ps != null; ps = ps.next) {
917 var r = execP(env, ps.data.p, v); 933 var r = execP(env, ps.data.p, v);
994 throw ("Unknown Ur expression kind " + e.c); 1010 throw ("Unknown Ur expression kind " + e.c);
995 } 1011 }
996 } 1012 }
997 } 1013 }
998 1014
1015 function execD(e) {
1016 return exec0(null, e);
1017 }
1018
999 function exec(e) { 1019 function exec(e) {
1000 var r = exec0(null, e); 1020 var r = exec0(null, e);
1001 1021
1002 if (r != null && r.body != null) 1022 if (r != null && r.body != null)
1003 return function(v) { return exec0(cons(v, r.env), r.body); }; 1023 return function(v) { return exec0(cons(v, r.env), r.body); };