On 3/30/07, Hugo Ferreira <[EMAIL PROTECTED]> wrote: > Hello Nicolas, > > Nicolas Pouillard wrote: > snip.. > >> > >> let _ = Logic.print term in > >> ... > >> and I would like to change that to: > >> > >> let _ = Logic.print env term in > >> ... > >> where env is declared by the extension. > >> > >> Looking at the Pcaml module I would say I am after a Pcaml.expr (Because > >> at first I thought we have a "let" label here, so this should be it). > >> > >> | "expr1" LEFTA > >> ... > >> | "let"; OPT "rec"; LIST1 let_binding SEP "and"; "in"; expr LEVEL "top" > > > > Please don't do this. Not that way... > > > > You should traverse the AST and then substitute function calls. > > > > Hmm.. I seem to understand less that I imagined about camlp4 (old > version). The only solution I found was: > > Pcaml.expr: LEVEL "apply" [ > [ e1 = LIDENT; e2 = SELF -> subst_func loc e1 e2 > | OPT UIDENT ; "." ; e1 = LIDENT; e2 = SELF -> subst_func loc e1 e2 ] > ]; > > where "subst_func loc e1 e2" simply checks for "e1 = print". (Still need > to work on "OPT UIDENT ; ." to accept only the "Logical" module though.) > Is this what you mean by traversing the AST?
No, here you are extending the language to treat differently something. Some times that's simple enough to be acceptable but overriding the application rule is not a good idea. > > In the new camlp4 one can do that easily with filters: > > > > open Camlp4.PreCast;; > > AstFilters.register_str_item_filter begin > > Ast.map_expr begin function > > | <:[EMAIL PROTECTED]< Logic.print $e$ >> -> <:[EMAIL PROTECTED]< > > Logic.print env $e$ >> > > | e -> e > > end > > end#str_item;; > > > > Interesting. Is this all that is needed? Yes > Doesn't one require an EXTEND? That's my point. This << Logic.print expr >> is valid OCaml so you don't need to extend the syntax. > Note that I am stuck with 3.09.3 and the previous version of ocamlp4. If so you should look at http://sylvain.le-gall.net/ocaml-ast-analyze.html That also provides some generic tool to process the AST. > Now I wonder how much work will be required to port my stuff to the new > version. It depends (but it worths its). > Venting: Is it just me or is an IQ above 180 required for working with > camlp4 (and certain Ocaml constructs). Took the whole morning to find > the solution above! You're not alone... Camlp4 is hard to understand... -- Nicolas Pouillard --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "ocaml-developer" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/ocaml-developer?hl=en For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html -~----------~----~----~----~------~----~------~--~---
