diff options
author | 2008-09-07 10:48:51 -0400 | |
---|---|---|
committer | 2008-09-07 10:48:51 -0400 | |
commit | e5636bb18cfe24bb8aa8dd0df64dfe781622371c (patch) | |
tree | 73dd7a8f6b59a3bf6347d879b7bdb46bc8747ed6 | |
parent | dbb9192edae68feb230b5b231fcaf10fd68103e8 (diff) |
'show' type class; htmlification optimizations
-rw-r--r-- | include/urweb.h | 8 | ||||
-rw-r--r-- | lib/basis.urs | 7 | ||||
-rw-r--r-- | src/c/urweb.c | 57 | ||||
-rw-r--r-- | src/mono_opt.sml | 27 | ||||
-rw-r--r-- | src/monoize.sml | 24 | ||||
-rw-r--r-- | tests/show.ur | 6 | ||||
-rw-r--r-- | tests/show.urp | 5 |
7 files changed, 133 insertions, 1 deletions
diff --git a/include/urweb.h b/include/urweb.h index 68ac4fc6..3995585e 100644 --- a/include/urweb.h +++ b/include/urweb.h @@ -30,9 +30,15 @@ char *lw_get_optional_input(lw_context, int name); 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); + +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); diff --git a/lib/basis.urs b/lib/basis.urs index 14b6d464..22a033a0 100644 --- a/lib/basis.urs +++ b/lib/basis.urs @@ -27,6 +27,13 @@ val intToString : int -> string 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 *) diff --git a/src/c/urweb.c b/src/c/urweb.c index 7ed582da..9836e502 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -507,6 +507,44 @@ lw_Basis_string lw_Basis_unurlifyString(lw_context ctx, char **s) { } +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 @@ void lw_Basis_htmlifyString_w(lw_context ctx, lw_Basis_string s) { } } +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; diff --git a/src/mono_opt.sml b/src/mono_opt.sml index 8be532aa..510f4c56 100644 --- a/src/mono_opt.sml +++ b/src/mono_opt.sml @@ -55,6 +55,8 @@ val attrifyString = String.translate (fn #"\"" => """ val urlifyInt = attrifyInt val urlifyFloat = attrifyFloat +val htmlifyInt = attrifyInt +val htmlifyFloat = attrifyFloat val htmlifyString = String.translate (fn ch => case ch of #"<" => "<" | #"&" => "&" @@ -149,6 +151,31 @@ fun exp e = 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) => diff --git a/src/monoize.sml b/src/monoize.sml index 5fd344d4..4e2340a7 100644 --- a/src/monoize.sml +++ b/src/monoize.sml @@ -80,6 +80,9 @@ fun monoType env = (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 @@ fun monoExp (env, st, fm) (all as (e, loc)) = 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 diff --git a/tests/show.ur b/tests/show.ur new file mode 100644 index 00000000..6a22fe64 --- /dev/null +++ b/tests/show.ur @@ -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> diff --git a/tests/show.urp b/tests/show.urp new file mode 100644 index 00000000..fa69257a --- /dev/null +++ b/tests/show.urp @@ -0,0 +1,5 @@ +debug +database dbname=test +exe /tmp/webapp + +show |