diff src/cjr_print.sml @ 1782:61c7eb1d3867

Support fancy expressions in module-level 'val' declarations
author Adam Chlipala <adam@chlipala.net>
date Wed, 18 Jul 2012 17:29:13 -0400
parents 95dd9f427bb2
children 3d922a28370b
line wrap: on
line diff
--- a/src/cjr_print.sml	Fri Jul 13 09:01:01 2012 -0400
+++ b/src/cjr_print.sml	Wed Jul 18 17:29:13 2012 -0400
@@ -2320,6 +2320,8 @@
              string "}"]
     end
 
+val global_initializers : Print.PD.pp_desc list ref = ref []
+
 fun p_decl env (dAll as (d, _) : decl) =
     case d of
         DStruct (n, xts) =>
@@ -2414,14 +2416,15 @@
       | DDatatypeForward _ => box []
 
       | DVal (x, n, t, e) =>
-        box [p_typ env t,
-             space,
-             string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
-             space,
-             string "=",
-             space,
-             p_exp env e,
-             string ";"]
+        (global_initializers := box [string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
+                                     space,
+                                     string "=",
+                                     space,
+                                     p_exp env e,
+                                     string ";"] :: !global_initializers;
+         box [p_typ env t,
+              space,
+              string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n ^ ";")])
       | DFun vi => p_fun false env vi
       | DFunRec vis =>
         let
@@ -2565,7 +2568,8 @@
                   unurlifies := IS.empty;
                   urlifies := IS.empty;
                   urlifiesL := IS.empty;
-                  self := NONE)
+                  self := NONE;
+                  global_initializers := [])
 
         val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
                                                let
@@ -3474,7 +3478,12 @@
              newline,
              string "static void uw_initializer(uw_context ctx) {",
              newline,
-             box [p_list_sep (box []) (fn (x1, x2, e) => box [string "({",
+             box [string "uw_begin_initializing(ctx);",
+                  newline,
+                  p_list_sep newline (fn x => x) (rev (!global_initializers)),
+                  string "uw_end_initializing(ctx);",
+                  newline,
+                  p_list_sep (box []) (fn (x1, x2, e) => box [string "({",
                                                               newline,
                                                               string "uw_unit __uwr_",
                                                               string x1,