(* Copyright (c) 2008, Jacques Garrigue All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3 The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *) (* $Id: pa_oo.ml,v 1.3 2008/05/02 06:30:09 garrigue Exp $ *) (* To compile: ocamlc -I +camlp4 -c -pp 'camlp4o pa_extend.cmo q_MLast.cmo' pa_oo.ml To use: ocaml camlp4o.cma pa_oo.cmo or ocamlc -pp 'camlp4o -I . pa_oo.cmo' *) let expand_access _loc mut id e kind = let id' = id^"'" in let decl = <:class_str_item< value $opt: mut <> None$ $lid:id$ = $e$ >> and reader = <:class_str_item< method $id$ = $lid:id$ >> and writer = <:class_str_item< method $"set_"^id$ $lid:id'$ = $lid:id$ := $lid:id'$ >> in let l = decl :: match kind with None -> [] | Some k -> match k with `R -> [reader] | `W -> [writer] | `RW -> [reader;writer] in <:class_str_item< declare $list:l$ end >> let expand_set _loc e1 e2 = match Pa_o.bigarray_set _loc e1 e2 with Some e -> e | None -> match e1 with <:expr< $o$ # $x$ >> -> <:expr< $o$ # $"set_"^x$ $e2$ >> | _ -> <:expr< $e1$ := $e2$ >> DELETE_RULE Pcaml.expr: SELF; "<-"; Pcaml.expr LEVEL "expr1" END;; EXTEND GLOBAL: Pcaml.class_str_item Pcaml.expr; Pcaml.class_str_item: [ [ "val"; mf = OPT "mutable"; lab = LIDENT; e = cvalue_binding; kind = OPT [ "with"; k = [ "reader" -> `R | "writer" -> `W | "accessor" -> `RW ] -> k] -> expand_access _loc mf lab e kind ] ]; cvalue_binding: [ [ "="; e = Pcaml.expr -> e | ":"; t = Pcaml.ctyp; "="; e = Pcaml.expr -> <:expr< ($e$ : $t$) >> ] ]; Pcaml.expr: LEVEL ":=" [ [ e1 = SELF; "<-"; e2 = Pcaml.expr LEVEL "expr1" -> expand_set _loc e1 e2 ] ]; Pcaml.expr: LEVEL "simple" [ [ "{|"; cf = LIST1 obj_record SEP ";"; "|}" -> (* self = OPT [ "("; p = patt; ":"; t = ctyp; ")" -> <:patt< ($p$ : $t$) >> | "("; p = patt; ")" -> <:patt< $p$ >> ]; *) MLast.ExObj (_loc, None, cf) ] ]; obj_record: [ [ "inherit"; ce = Pcaml.class_expr -> <:class_str_item< inherit $ce$ >> | mf = OPT "mutable"; lab = LIDENT; ty = OPT [ ":"; t = Pcaml.ctyp -> t]; "="; e = Pcaml.expr LEVEL "expr1" -> expand_access _loc mf lab e (Some(if mf = None then `R else `RW)) ] ]; END;;