diff src/jscomp.sml @ 585:35471f067980

Reactive record pattern
author Adam Chlipala <adamc@hcoop.net>
date Thu, 01 Jan 2009 11:04:09 -0500
parents 101eb0058136
children 1c969230ee7f
line wrap: on
line diff
--- 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