changeset 585:35471f067980

Reactive record pattern
author Adam Chlipala <adamc@hcoop.net>
date Thu, 01 Jan 2009 11:04:09 -0500
parents 101eb0058136
children 1c969230ee7f
files jslib/urweb.js src/jscomp.sml tests/stypes.ur
diffstat 3 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/jslib/urweb.js	Thu Jan 01 10:49:42 2009 -0500
+++ b/jslib/urweb.js	Thu Jan 01 11:04:09 2009 -0500
@@ -2,7 +2,7 @@
   return { n : ls, v : v };
 }
 function callAll(ls) {
-  for (; ls; ls = ls.next)
+  for (; ls; ls = ls.n)
     ls.v();
 }
 
--- a/src/jscomp.sml	Thu Jan 01 10:49:42 2009 -0500
+++ b/src/jscomp.sml	Thu Jan 01 11:04:09 2009 -0500
@@ -154,13 +154,13 @@
                              ^ "\"")
                       | _ => str (Prim.toString p)
 
-                fun jsPat inner (p, _) succ fail =
+                fun jsPat depth inner (p, _) succ fail =
                     case p of
                         PWild => succ
-                      | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d,"),
+                      | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d" ^ Int.toString depth ^ ","),
                                           succ,
                                           str ")"]
-                      | PPrim p => strcat [str "(d==",
+                      | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
                                            jsPrim p,
                                            str "?",
                                            succ,
@@ -173,18 +173,21 @@
                             val (_, succ) = foldl
                                             (fn ((x, p, _), (inner, succ)) =>
                                                 (inner + E.patBindsN p,
-                                                 jsPat inner p succ fail))
+                                                 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
+                                                              ^ Int.toString depth ^ "._" ^ x ^ ","),
+                                                         jsPat (depth+1) inner p succ fail,
+                                                         str ")"]))
                                             (inner, succ) xps
                         in
                             succ
                         end
-                      | PNone _ => strcat [str "(d?",
+                      | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
                                            fail,
                                            str ":",
                                            succ,
                                            str ")"]
-                      | PSome (_, p) => strcat [str "(d?",
-                                                jsPat inner p succ fail,
+                      | PSome (_, p) => strcat [str ("(d" ^ Int.toString depth ^ "?"),
+                                                jsPat depth inner p succ fail,
                                                 str ":",
                                                 fail,
                                                 str ")"]
@@ -285,7 +288,7 @@
                     let
                         val locals = List.tabulate
                                      (varDepth e,
-                                   fn i => str ("var _" ^ Int.toString (len + inner + i) ^ ";"))
+                                   fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
                         val (e, st) = jsE (inner + 1) (e, st)
                     in
                         (strcat (str ("function(_"
@@ -369,7 +372,7 @@
                                                               str "pf()"
                                                           else
                                                               str ("c" ^ Int.toString (i+1) ^ "()")
-                                                      val c = jsPat inner p e fail
+                                                      val c = jsPat 0 inner p e fail
                                                   in
                                                       (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
                                                                c,
@@ -382,7 +385,7 @@
                     in
                         (strcat (str "("
                                  :: List.revAppend (cases,
-                                                    [str "d=",
+                                                    [str "d0=",
                                                      e,
                                                      str ",c0())"])), st)
                     end
--- a/tests/stypes.ur	Thu Jan 01 10:49:42 2009 -0500
+++ b/tests/stypes.ur	Thu Jan 01 11:04:09 2009 -0500
@@ -10,7 +10,11 @@
 
       <dyn signal={n <- signal sFloat; return <xml>{[n + 1.0]}</xml>}/> <a onclick={set sFloat 4.56}>Change</a><br/>
 
-      <dyn signal={p <- signal sBoth; return <xml>{[p.1]}, {[p.2]}</xml>}/> <a onclick={set sBoth (8, 100.001)}>Change</a><br/>
+      <dyn signal={p <- signal sBoth; return <xml>{[p.1]}, {[p.2]}</xml>}/>;
+      <dyn signal={p <- signal sBoth; case p of
+                                          (7, _) => return <xml>Initial</xml>
+                                        | (fst, snd) => return <xml>{[fst]}, {[snd]}</xml>}/>
+      <a onclick={set sBoth (8, 100.001)}>Change</a><br/>
 
       <dyn signal={o <- signal sOpt; case o of
                                          None => return <xml>None</xml>