Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] revised syntax and immediate objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Matt Gushee <matt@g...>
Subject: Re: [Caml-list] revised syntax and immediate objects
Jonathan Roewen wrote:

 > What is the syntax for immediate objects in ocaml when using revised 
syntax?

Not sure offhand, but ...

 > I'd like to try make a camlp4 syntax extension that uses immediate
 > objects,

I've done that. I'll show you the key section of the code below, and I 
can send you the complete file if you like. It may not be 100% correct, 
but I've been using it in an application and so far it works. I don't 
know if I can *explain* it, though ... I wrote it several months ago, 
and I'm coming to think that CamlP4, like Perl, is a write-only language ;-)

[BTW, *_si in function names means "structure item", and *_csi means 
"class structure item"]


let object_body loc decls =
   let sub_objects =
     let sos =
       List.fold_right
         ( fun idat lst ->
           match idat.sub_obj with
           | None -> lst
           | Some so -> so::lst )
         decls [] in
     <:class_str_item< declare $list:sos$ end >>
   and accessors =
     let accs =
       List.fold_right
         ( fun idat lst ->
           match idat.setter with
           | None -> idat.getter :: lst
           | Some se -> idat.getter :: se :: lst )
         decls [] in
     <:class_str_item< declare $list:accs$ end >> in
   (sub_objects, accessors)

let subconf_csi loc key decls =
   let sub_objects, accessors = object_body loc decls
   and pself = <:patt< self >>
   and inheritance =
     <:class_str_item<
       inherit sub_config data defaults path as super
     >> in
   let obj_expr =
     MLast.ExObj
       (loc, Some pself, [inheritance; sub_objects; accessors]) in
   let keylist_expr = <:expr< [$str:key$] >> in
   let path_bind_expr =
     <:expr<
       let path = path @ $keylist_expr$ in $obj_expr$
     >>
   and oname = key ^ "_" in
   Some <:class_str_item< value $lid:oname$ = $path_bind_expr$ >>

let rootconf_si loc cname decls =
   let sub_objects, accessors = object_body loc decls
   and pself = <:patt< self >>
   and inheritance =
     <:class_str_item<
       inherit root_config srcs dest data defaults as super
     >> in
   let oe =
     MLast.ExObj
       (loc, Some pself, [inheritance; sub_objects; accessors]) in
   <:str_item<
       value $lid:cname$ srcs dest =
         let data = Dict.create ()
         and path = [] in $oe$
   >>

let main_si loc cname decls =
   write_example decls;
   let os = <:str_item< open Rascl >>
   and oc = <:str_item< open ConfigObject >>
   and od = <:str_item< open Dict >>
   and defcreate = <:str_item< value defaults = create () >>
   and defsetup0 = top_doin_expr loc [] decls in
   let defsetup = <:str_item< $exp:defsetup0$ >>
   and cl = rootconf_si loc cname decls in
   let all = [os; oc; od; defcreate; defsetup; cl] in
   <:str_item< declare $list:all$ end >>



-- 
Matt Gushee
: Bantam - lightweight file manager : matt.gushee.net/software/bantam/ :
: RASCL's A Simple Configuration Language :     matt.gushee.net/rascl/ :