changeset 286:ffe5b01908ae

'show' type class; htmlification optimizations
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 10:48:51 -0400 (2008-09-07)
parents e89076c41c39
children 3ed7a7c7b060
files include/urweb.h lib/basis.urs src/c/urweb.c src/mono_opt.sml src/monoize.sml tests/show.ur tests/show.urp
diffstat 7 files changed, 133 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Sun Sep 07 10:20:42 2008 -0400
+++ b/include/urweb.h	Sun Sep 07 10:48:51 2008 -0400
@@ -30,9 +30,15 @@
 
 void lw_write(lw_context, const char*);
 
+char *lw_Basis_htmlifyInt(lw_context, lw_Basis_int);
+char *lw_Basis_htmlifyFloat(lw_context, lw_Basis_float);
+char *lw_Basis_htmlifyString(lw_context, lw_Basis_string);
+char *lw_Basis_htmlifyBool(lw_context, lw_Basis_bool);
 
-char *lw_Basis_htmlifyString(lw_context, lw_Basis_string);
+void lw_Basis_htmlifyInt_w(lw_context, lw_Basis_int);
+void lw_Basis_htmlifyFloat_w(lw_context, lw_Basis_float);
 void lw_Basis_htmlifyString_w(lw_context, lw_Basis_string);
+void lw_Basis_htmlifyBool_w(lw_context, lw_Basis_bool);
 
 char *lw_Basis_attrifyInt(lw_context, lw_Basis_int);
 char *lw_Basis_attrifyFloat(lw_context, lw_Basis_float);
--- a/lib/basis.urs	Sun Sep 07 10:20:42 2008 -0400
+++ b/lib/basis.urs	Sun Sep 07 10:48:51 2008 -0400
@@ -27,6 +27,13 @@
 val floatToString : float -> string
 val boolToString : bool -> string
 
+class show
+val show : t ::: Type -> show t -> t -> string
+val show_int : show int
+val show_float : show float
+val show_string : show string
+val show_bool : show bool
+
 
 (** SQL *)
 
--- a/src/c/urweb.c	Sun Sep 07 10:20:42 2008 -0400
+++ b/src/c/urweb.c	Sun Sep 07 10:48:51 2008 -0400
@@ -507,6 +507,44 @@
 }
 
 
+char *lw_Basis_htmlifyInt(lw_context ctx, lw_Basis_int n) {
+  int len;
+  char *r;
+
+  lw_check_heap(ctx, INTS_MAX);
+  r = ctx->heap_front;
+  sprintf(r, "%lld%n", n, &len);
+  ctx->heap_front += len+1;
+  return r;
+}
+
+void lw_Basis_htmlifyInt_w(lw_context ctx, lw_Basis_int n) {
+  int len;
+
+  lw_check(ctx, INTS_MAX);
+  sprintf(ctx->page_front, "%lld%n", n, &len);
+  ctx->page_front += len;
+}
+
+char *lw_Basis_htmlifyFloat(lw_context ctx, lw_Basis_float n) {
+  int len;
+  char *r;
+
+  lw_check_heap(ctx, FLOATS_MAX);
+  r = ctx->heap_front;
+  sprintf(r, "%g%n", n, &len);
+  ctx->heap_front += len+1;
+  return r;
+}
+
+void lw_Basis_htmlifyFloat_w(lw_context ctx, lw_Basis_float n) {
+  int len;
+
+  lw_check(ctx, FLOATS_MAX);
+  sprintf(ctx->page_front, "%g%n", n, &len);
+  ctx->page_front += len;
+}
+
 char *lw_Basis_htmlifyString(lw_context ctx, lw_Basis_string s) {
   char *r, *s2;
 
@@ -565,6 +603,25 @@
   }
 }
 
+lw_Basis_string lw_Basis_htmlifyBool(lw_context ctx, lw_Basis_bool b) {
+  if (b == lw_Basis_False)
+    return "False";
+  else
+    return "True";
+}
+
+void lw_Basis_htmlifyBool_w(lw_context ctx, lw_Basis_bool b) {
+  if (b == lw_Basis_False) {
+    lw_check(ctx, 6);
+    strcpy(ctx->page_front, "False");
+    ctx->page_front += 5;
+  } else {
+    lw_check(ctx, 5);
+    strcpy(ctx->page_front, "True");
+    ctx->page_front += 4;
+  }
+}
+
 lw_Basis_string lw_Basis_strcat(lw_context ctx, lw_Basis_string s1, lw_Basis_string s2) {
   int len = strlen(s1) + strlen(s2) + 1;
   char *s;
--- a/src/mono_opt.sml	Sun Sep 07 10:20:42 2008 -0400
+++ b/src/mono_opt.sml	Sun Sep 07 10:48:51 2008 -0400
@@ -55,6 +55,8 @@
 val urlifyInt = attrifyInt
 val urlifyFloat = attrifyFloat
 
+val htmlifyInt = attrifyInt
+val htmlifyFloat = attrifyFloat
 val htmlifyString = String.translate (fn ch => case ch of
                                                    #"<" => "&lt;"
                                                  | #"&" => "&amp;"
@@ -149,6 +151,31 @@
         ESeq ((EWrite (EPrim (Prim.String (s1 ^ s2)), loc), loc),
               e)
 
+      | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "intToString", [(EPrim (Prim.Int n), _)]), _)]) =>
+        EPrim (Prim.String (htmlifyInt n))
+      | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "intToString", es), _)]) =>
+        EFfiApp ("Basis", "htmlifyInt", es)
+      | EWrite (EFfiApp ("Basis", "htmlifyInt", [e]), _) =>
+        EFfiApp ("Basis", "htmlifyInt_w", [e])
+
+      | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "floatToString", [(EPrim (Prim.Float n), _)]), _)]) =>
+        EPrim (Prim.String (htmlifyFloat n))
+      | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "floatToString", es), _)]) =>
+        EFfiApp ("Basis", "htmlifyFloat", es)
+      | EWrite (EFfiApp ("Basis", "htmlifyFloat", [e]), _) =>
+        EFfiApp ("Basis", "htmlifyFloat_w", [e])
+
+      | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString",
+                                                      [(ECon (Enum, PConFfi {con = "True", ...}, NONE), _)]), _)]) =>
+        EPrim (Prim.String "True")
+      | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString",
+                                                      [(ECon (Enum, PConFfi {con = "False", ...}, NONE), _)]), _)]) =>
+        EPrim (Prim.String "False")
+      | EFfiApp ("Basis", "htmlifyString", [(EFfiApp ("Basis", "boolToString", es), _)]) =>
+        EFfiApp ("Basis", "htmlifyBool", es)
+      | EWrite (EFfiApp ("Basis", "htmlifyBool", [e]), _) =>
+        EFfiApp ("Basis", "htmlifyBool_w", [e])
+
       | EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]) =>
         EPrim (Prim.String (htmlifyString s))
       | EWrite (EFfiApp ("Basis", "htmlifyString", [(EPrim (Prim.String s), _)]), loc) =>
--- a/src/monoize.sml	Sun Sep 07 10:20:42 2008 -0400
+++ b/src/monoize.sml	Sun Sep 07 10:48:51 2008 -0400
@@ -80,6 +80,9 @@
                     (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc)
                   | L.TRecord _ => poly ()
 
+                  | L.CApp ((L.CFfi ("Basis", "show"), _), t) =>
+                    (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc)
+
                   | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) =>
                     (L'.TFfi ("Basis", "string"), loc)
                   | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) =>
@@ -461,6 +464,27 @@
             end
           | L.ECon _ => poly ()
 
+          | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
+            let
+                val t = monoType env t
+                val s = (L'.TFfi ("Basis", "string"), loc)
+            in
+                ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc),
+                           (L'.ERel 0, loc)), loc), fm)
+            end
+          | L.EFfi ("Basis", "show_int") =>
+            ((L'.EFfi ("Basis", "intToString"), loc), fm)
+          | L.EFfi ("Basis", "show_float") =>
+            ((L'.EFfi ("Basis", "floatToString"), loc), fm)
+          | L.EFfi ("Basis", "show_string") =>
+            let
+                val s = (L'.TFfi ("Basis", "string"), loc)
+            in
+                ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm)
+            end
+          | L.EFfi ("Basis", "show_bool") =>
+            ((L'.EFfi ("Basis", "boolToString"), loc), fm)
+
           | L.ECApp ((L.EFfi ("Basis", "return"), _), t) =>
             let
                 val t = monoType env t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/show.ur	Sun Sep 07 10:48:51 2008 -0400
@@ -0,0 +1,6 @@
+fun main () : transaction page = return <html><body>
+        6 = {cdata (show _ 6)}<br/>
+        12.34 = {cdata (show _ 12.34)}<br/>
+        Hi = {cdata (show _ "Hi")}<br/>
+        False = {cdata (show _ False)}<br/>
+</body></html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/show.urp	Sun Sep 07 10:48:51 2008 -0400
@@ -0,0 +1,5 @@
+debug
+database dbname=test
+exe /tmp/webapp
+
+show