From e5636bb18cfe24bb8aa8dd0df64dfe781622371c Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Sun, 7 Sep 2008 10:48:51 -0400 Subject: 'show' type class; htmlification optimizations --- src/c/urweb.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mono_opt.sml | 27 +++++++++++++++++++++++++++ src/monoize.sml | 24 ++++++++++++++++++++++++ 3 files changed, 108 insertions(+) (limited to 'src') 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 -- cgit v1.2.3