Mercurial > urweb
changeset 165:e52dfb1e6b19
Datatypes through cjrize, modulo decoding
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Tue, 29 Jul 2008 13:50:53 -0400 |
parents | 6847741e1f5f |
children | a991431b77eb |
files | src/cjr.sml src/cjr_env.sml src/cjr_print.sml src/cjrize.sml |
diffstat | 4 files changed, 74 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/src/cjr.sml Tue Jul 29 13:32:07 2008 -0400 +++ b/src/cjr.sml Tue Jul 29 13:50:53 2008 -0400 @@ -56,6 +56,7 @@ datatype decl' = DStruct of int * (string * typ) list + | DDatatype of string * int * (string * int * typ option) list | DVal of string * int * typ * exp | DFun of string * int * (string * typ) list * typ * exp | DFunRec of (string * int * (string * typ) list * typ * exp) list
--- a/src/cjr_env.sml Tue Jul 29 13:32:07 2008 -0400 +++ b/src/cjr_env.sml Tue Jul 29 13:50:53 2008 -0400 @@ -118,7 +118,16 @@ fun declBinds env (d, loc) = case d of - DVal (x, n, t, _) => pushENamed env x n t + DDatatype (x, n, xncs) => + let + val env = pushTNamed env x n NONE + in + foldl (fn ((x', n', NONE), env) => pushENamed env x' n' (TNamed n, loc) + | ((x', n', SOME t), env) => pushENamed env x' n' (TFun (t, (TNamed n, loc)), loc)) + env xncs + end + | DStruct (n, xts) => pushStruct env n xts + | DVal (x, n, t, _) => pushENamed env x n t | DFun (fx, n, args, ran, _) => let val t = foldl (fn ((_, arg), t) => (TFun (arg, t), loc)) ran args @@ -132,6 +141,6 @@ in pushENamed env fx n t end) env vis - | DStruct (n, xts) => pushStruct env n xts + end
--- a/src/cjr_print.sml Tue Jul 29 13:32:07 2008 -0400 +++ b/src/cjr_print.sml Tue Jul 29 13:50:53 2008 -0400 @@ -70,7 +70,9 @@ string "__lws_", string (Int.toString i)] | TNamed n => - (string ("__lwt_" ^ #1 (E.lookupTNamed env n) ^ "_" ^ Int.toString n) + (box [string "struct", + space, + string ("__lwt_" ^ #1 (E.lookupTNamed env n) ^ "_" ^ Int.toString n ^ "*")] handle CjrEnv.UnboundNamed _ => string ("__lwt_UNBOUND__" ^ Int.toString n)) | TFfi (m, x) => box [string "lw_", string m, string "_", string x] @@ -191,6 +193,51 @@ string ";", newline]) xts, string "};"] + | DDatatype (x, n, xncs) => + let + val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE + | (x, n, SOME t) => SOME (x, n, t)) xncs + in + box [string "enum", + space, + string ("__lwe_" ^ x ^ "_" ^ Int.toString n), + space, + string "{", + space, + p_list_sep (box [string ",", space]) (fn (x, n, _) => string ("__lwc_" ^ x ^ "_" ^ Int.toString n)) xncs, + space, + string "};", + newline, + newline, + string "struct", + space, + string ("_lwd_" ^ x ^ "_" ^ Int.toString n), + space, + string "{", + newline, + string "enum", + space, + string ("__lwe_" ^ x ^ "_" ^ Int.toString n), + space, + string "tag;", + newline, + box (case xncsArgs of + [] => [] + | _ => [string "union", + space, + string "{", + newline, + p_list_sep newline (fn (x, n, t) => box [p_typ env t, + space, + string ("__lwc_" ^ x), + string ";"]) xncsArgs, + newline, + string "}", + space, + string "data;", + newline]), + string "};"] + end | DVal (x, n, t, e) => box [p_typ env t,
--- a/src/cjrize.sml Tue Jul 29 13:32:07 2008 -0400 +++ b/src/cjrize.sml Tue Jul 29 13:50:53 2008 -0400 @@ -160,7 +160,20 @@ fun cifyDecl ((d, loc), sm) = case d of - L.DDatatype _ => raise Fail "Cjrize DDatatype" + L.DDatatype (x, n, xncs) => + let + val (xncs, sm) = ListUtil.foldlMap (fn ((x, n, to), sm) => + case to of + NONE => ((x, n, NONE), sm) + | SOME t => + let + val (t, sm) = cifyTyp (t, sm) + in + ((x, n, SOME t), sm) + end) sm xncs + in + (SOME (L'.DDatatype (x, n, xncs), loc), NONE, sm) + end | L.DVal (x, n, t, e, _) => let