caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* camlp4 3.10 questions
@ 2007-03-28 22:36 Hendrik Tews
  2007-03-29  9:48 ` [Caml-list] " Nicolas Pouillard
  0 siblings, 1 reply; 8+ messages in thread
From: Hendrik Tews @ 2007-03-28 22:36 UTC (permalink / raw)
  To: caml-list

Hi,

while doing my little camlp4 printer exercise I had the following
questions:

1. What is the difference between Register.Printer and
   Register.OCamlPrinter? I guess it boils down to the difference
   between Sig.Syntax and Sig.Camlp4Syntax?

2. There are various maps and folds on asts in Sig.Camlp4Ast. How
   can I use them?

3. Register.declare_dyn_module puts all modules in a queue
   together with a function that contains some delayed side
   effects. Where are these function called?

4. I have the impression that the users Make functors (such as in
   the printer HOWTO) are always applied to the same argument,
   namely PreCast.Syntax. Why do I have to give a functor then?

5. I saw Camlp4OCamlOriginalQuotationExpander, presumably this is
   a quotation expander for quotations in original syntax. Is
   this already complete? 

6. There are various camlp4's: camlp4 camlp4o camlp4of camlp4oof
   camlp4orf camlp4r camlp4rf camlp4boot camlp4prof. Could
   somebody explain the nameing convention? 

   camlp4boot is the one used for bootstrapping or compiling the
   camlp4 sources?

   What is camlp4prof?

7. Is there an equivalent to the old pa_o_fast.cmx?

8. How can I process multiple files with the same camlp4 process?

Bye,

Hendrik


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] camlp4 3.10 questions
  2007-03-28 22:36 camlp4 3.10 questions Hendrik Tews
@ 2007-03-29  9:48 ` Nicolas Pouillard
  2007-03-29 15:50   ` Hendrik Tews
  2007-03-29 21:47   ` Hendrik Tews
  0 siblings, 2 replies; 8+ messages in thread
From: Nicolas Pouillard @ 2007-03-29  9:48 UTC (permalink / raw)
  To: Hendrik Tews; +Cc: caml-list

On 3/29/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
> Hi,
>
> while doing my little camlp4 printer exercise I had the following
> questions:
>
> 1. What is the difference between Register.Printer and
>    Register.OCamlPrinter? I guess it boils down to the difference
>    between Sig.Syntax and Sig.Camlp4Syntax?

Yes it's mainly a difference about what your printer really needs. If
you use the camlp4 AST then you need a Sig.Camlp4Syntax as argument,
if it's not needed then you can use Sig.Syntax.

The main point is about make thing re-usable.

> 2. There are various maps and folds on asts in Sig.Camlp4Ast. How
>    can I use them?

There two things:

1/ The generic classes map and fold for the camlp4 AST.
You have to subclass it and override some cases to use it.

Here is a filter that reduces 0 + x to x and x + 0 to x.

open Camlp4.PreCast
let f =
  object (self)
     inherit Ast.map as super
     method expr = function
     | <:expr< $x$ + 0 >> | <:expr< 0 + $x$ >> -> self#expr x
     | e -> super#expr e
  end in
AstFilters.register_str_item f#str_item

2/ There is a lot of shortcuts to made the use of them easier when one
just want to hook up only one thing (expressions for instance).

The same example.

open Camlp4.PreCast
let f = Ast.map_expr begin function
     | <:expr< $x$ + 0 >> | <:expr< 0 + $x$ >> -> x
     | e -> e
end in
AstFilters.register_str_item f#str_item

There is some interesting examples in camlp4/examples

> 3. Register.declare_dyn_module puts all modules in a queue
>    together with a function that contains some delayed side
>    effects. Where are these function called?

Camlp4Bin.ml

> 4. I have the impression that the users Make functors (such as in
>    the printer HOWTO) are always applied to the same argument,
>    namely PreCast.Syntax. Why do I have to give a functor then?

To make it re-usable. Imagine that you want a camlp4 with a different
lexer, token type, or locations... By making some new modules binding
them in a new MyCamlp4PreCast and then use  printers, parsers syntax
extensions, that just require a Sig.Syntax...

> 5. I saw Camlp4OCamlOriginalQuotationExpander, presumably this is
>    a quotation expander for quotations in original syntax. Is
>    this already complete?

Complete, not really but usable yes.

> 6. There are various camlp4's: camlp4 camlp4o camlp4of camlp4oof
>    camlp4orf camlp4r camlp4rf camlp4boot camlp4prof. Could
>    somebody explain the nameing convention?

f is for full, this means that many camlp4 extensions are already
loaded (grammars, quotations, parsers, list comprehension...).

>    camlp4boot is the one used for bootstrapping or compiling the
>    camlp4 sources?

Both.

>    What is camlp4prof?

A profiling toy.

> 7. Is there an equivalent to the old pa_o_fast.cmx?

Nop :(

> 8. How can I process multiple files with the same camlp4 process?

As always ?

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] camlp4 3.10 questions
  2007-03-29  9:48 ` [Caml-list] " Nicolas Pouillard
@ 2007-03-29 15:50   ` Hendrik Tews
  2007-03-29 16:10     ` Nicolas Pouillard
  2007-03-29 21:47   ` Hendrik Tews
  1 sibling, 1 reply; 8+ messages in thread
From: Hendrik Tews @ 2007-03-29 15:50 UTC (permalink / raw)
  To: caml-list

"Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:

   On 3/29/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
   >
   > 1. What is the difference between Register.Printer and
   >    Register.OCamlPrinter? I guess it boils down to the difference
   >    between Sig.Syntax and Sig.Camlp4Syntax?

   Yes it's mainly a difference about what your printer really needs. If
   you use the camlp4 AST then you need a Sig.Camlp4Syntax as argument,
   if it's not needed then you can use Sig.Syntax.

Does this mean I have to use Camlp4Syntax and OCamlPrinter it I
want to use quotations? Or do the quotations also work on
Sig.Syntax?

   > 3. Register.declare_dyn_module puts all modules in a queue
   >    together with a function that contains some delayed side
   >    effects. Where are these function called?

   Camlp4Bin.ml

Well, I was that far. But where in Camlp4Bin?

   > 5. I saw Camlp4OCamlOriginalQuotationExpander, presumably this is
   >    a quotation expander for quotations in original syntax. Is
   >    this already complete?

   Complete, not really but usable yes.

Are there just corner cases missing or whole branches of the
syntax?

   >    What is camlp4prof?

   A profiling toy.

Well... profiling what?

Bye,

Hendrik


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] camlp4 3.10 questions
  2007-03-29 15:50   ` Hendrik Tews
@ 2007-03-29 16:10     ` Nicolas Pouillard
  0 siblings, 0 replies; 8+ messages in thread
From: Nicolas Pouillard @ 2007-03-29 16:10 UTC (permalink / raw)
  To: Hendrik Tews; +Cc: caml-list

On 3/29/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
> "Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:
>
>    On 3/29/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
>    >
>    > 1. What is the difference between Register.Printer and
>    >    Register.OCamlPrinter? I guess it boils down to the difference
>    >    between Sig.Syntax and Sig.Camlp4Syntax?
>
>    Yes it's mainly a difference about what your printer really needs. If
>    you use the camlp4 AST then you need a Sig.Camlp4Syntax as argument,
>    if it's not needed then you can use Sig.Syntax.
>
> Does this mean I have to use Camlp4Syntax and OCamlPrinter it I
> want to use quotations? Or do the quotations also work on
> Sig.Syntax?

You need Camlp4Syntax to use camlp4 quotations (like expr, patt, ctyp...).

>
>    > 3. Register.declare_dyn_module puts all modules in a queue
>    >    together with a function that contains some delayed side
>    >    effects. Where are these function called?
>
>    Camlp4Bin.ml
>
> Well, I was that far. But where in Camlp4Bin?

It's done trough Register.iter_and_take_callbacks in the function called main.

>    > 5. I saw Camlp4OCamlOriginalQuotationExpander, presumably this is
>    >    a quotation expander for quotations in original syntax. Is
>    >    this already complete?
>
>    Complete, not really but usable yes.
>
> Are there just corner cases missing or whole branches of the
> syntax?

Just corner cases, I hope.

>    >    What is camlp4prof?
>
>    A profiling toy.
>
> Well... profiling what?

There is a filter (called Camlp4Profiler) that adds to all functions
something like:

let f x y z = body

become:

let f x y z =
let () = Camlp4prof.count "f @ foo.ml line 42 ..." in body

Then the camlp4prof module store all these calls and writes
"camlp4_profiler.out" at exit.

Running the camlp4prof program dumps that file.

PS: For now that's just a toy.

Best regards,

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] camlp4 3.10 questions
  2007-03-29  9:48 ` [Caml-list] " Nicolas Pouillard
  2007-03-29 15:50   ` Hendrik Tews
@ 2007-03-29 21:47   ` Hendrik Tews
  2007-03-29 21:57     ` Nicolas Pouillard
  1 sibling, 1 reply; 8+ messages in thread
From: Hendrik Tews @ 2007-03-29 21:47 UTC (permalink / raw)
  To: caml-list

"Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:

   > 2. There are various maps and folds on asts in Sig.Camlp4Ast. How
   >    can I use them?

   2/ There is a lot of shortcuts to made the use of them easier when one
   just want to hook up only one thing (expressions for instance).

   The same example.

   open Camlp4.PreCast
   let f = Ast.map_expr begin function
       | <:expr< $x$ + 0 >> | <:expr< 0 + $x$ >> -> x
       | e -> e
   end in
   AstFilters.register_str_item f#str_item

That should be  AstFilters.register_str_item f ?

   > 8. How can I process multiple files with the same camlp4 process?

   As always ?

How is as always? Two files on the command line give the usage
info:

    camlp4o printer.cmo test.ml printer.ml
    Usage: camlp4 [load-options] [--] [other-options]
    Options:
        ....

Thanks for all the answers,

Hendrik


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] camlp4 3.10 questions
  2007-03-29 21:47   ` Hendrik Tews
@ 2007-03-29 21:57     ` Nicolas Pouillard
  2007-03-30 11:13       ` Hendrik Tews
  0 siblings, 1 reply; 8+ messages in thread
From: Nicolas Pouillard @ 2007-03-29 21:57 UTC (permalink / raw)
  To: Hendrik Tews; +Cc: caml-list

On 3/29/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
> "Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:
>
>    > 2. There are various maps and folds on asts in Sig.Camlp4Ast. How
>    >    can I use them?
>
>    2/ There is a lot of shortcuts to made the use of them easier when one
>    just want to hook up only one thing (expressions for instance).
>
>    The same example.
>
>    open Camlp4.PreCast
>    let f = Ast.map_expr begin function
>        | <:expr< $x$ + 0 >> | <:expr< 0 + $x$ >> -> x
>        | e -> e
>    end in
>    AstFilters.register_str_item f#str_item
>
> That should be  AstFilters.register_str_item f ?

Nop, giving the up to date signature of Ast.map_exp:

value map_expr : (expr -> expr) -> map;

A map value is an object that waits for a starting order.

f#str_item <:str_item< ... >>;

f#expr <:expr< ... >>;

...

Since AstFilters.register_str_item waits for a (str_item -> str_item)
function I give it f#str_item.

>    > 8. How can I process multiple files with the same camlp4 process?
>
>    As always ?
>
> How is as always? Two files on the command line give the usage
> info:
>
>     camlp4o printer.cmo test.ml printer.ml
>     Usage: camlp4 [load-options] [--] [other-options]
>     Options:
>         ....

You're right only one file (implem or interf).
So you can't by command line.

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] camlp4 3.10 questions
  2007-03-29 21:57     ` Nicolas Pouillard
@ 2007-03-30 11:13       ` Hendrik Tews
  2007-03-30 16:13         ` Nicolas Pouillard
  0 siblings, 1 reply; 8+ messages in thread
From: Hendrik Tews @ 2007-03-30 11:13 UTC (permalink / raw)
  To: caml-list

"Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:

   On 3/29/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
   > "Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:
   >
   >    > 2. There are various maps and folds on asts in Sig.Camlp4Ast. How
   >    >    can I use them?
   >
   >    2/ There is a lot of shortcuts to made the use of them easier when one
   >    just want to hook up only one thing (expressions for instance).
   >
   >    The same example.
   >
   >    open Camlp4.PreCast
   >    let f = Ast.map_expr begin function
   >        | <:expr< $x$ + 0 >> | <:expr< 0 + $x$ >> -> x
   >        | e -> e
   >    end in
   >    AstFilters.register_str_item f#str_item
   >
   > That should be  AstFilters.register_str_item f ?

   Nop, giving the up to date signature of Ast.map_exp:

   value map_expr : (expr -> expr) -> map;

Where does this come from? I only found

  value map_expr : (expr -> expr) -> expr -> expr;

in Sig.ml line 515.

   >    > 8. How can I process multiple files with the same camlp4 process?

   You're right only one file (implem or interf).
   So you can't by command line.

Well, I guessed that. But how about using the API? 

Hendrik


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] camlp4 3.10 questions
  2007-03-30 11:13       ` Hendrik Tews
@ 2007-03-30 16:13         ` Nicolas Pouillard
  0 siblings, 0 replies; 8+ messages in thread
From: Nicolas Pouillard @ 2007-03-30 16:13 UTC (permalink / raw)
  To: Hendrik Tews; +Cc: caml-list

On 3/30/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
> "Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:
>
>    On 3/29/07, Hendrik Tews <H.Tews@cs.ru.nl> wrote:
>    > "Nicolas Pouillard" <nicolas.pouillard@gmail.com> writes:
>    >
>    >    > 2. There are various maps and folds on asts in Sig.Camlp4Ast. How
>    >    >    can I use them?
>    >
>    >    2/ There is a lot of shortcuts to made the use of them easier when one
>    >    just want to hook up only one thing (expressions for instance).
>    >
>    >    The same example.
>    >
>    >    open Camlp4.PreCast
>    >    let f = Ast.map_expr begin function
>    >        | <:expr< $x$ + 0 >> | <:expr< 0 + $x$ >> -> x
>    >        | e -> e
>    >    end in
>    >    AstFilters.register_str_item f#str_item
>    >
>    > That should be  AstFilters.register_str_item f ?
>
>    Nop, giving the up to date signature of Ast.map_exp:
>
>    value map_expr : (expr -> expr) -> map;
>
> Where does this come from? I only found
>
>   value map_expr : (expr -> expr) -> expr -> expr;

Yes that's the old one.

>    >    > 8. How can I process multiple files with the same camlp4 process?
>
>    You're right only one file (implem or interf).
>    So you can't by command line.
>
> Well, I guessed that. But how about using the API?

I've put an example in camlp4/example/parse_files.ml
I've also improved a little the API by flattening the small modules
Syntax.{Parser,Printer,Warning} into Syntax itself.

The example make a new syntax module (the Caml one), then parse two
files make, then join both ASTs, and then print it.

(*******************************)
open Camlp4.PreCast;;

module CamlGram = MakeGram(Lexer);;

module Caml =
  Camlp4.Printers.OCaml.Make
    (Camlp4OCamlParser.Make
      (Camlp4OCamlRevisedParser.Make
        (Camlp4.OCamlInitSyntax.Make(Ast)(Gram)(Quotation))));;

let parse f =
  let ic = open_in f in
  let strm = Stream.of_channel ic in
  let res = Caml.parse_implem (Loc.mk f) strm in
  close_in ic; res;;

let ghost = Loc.ghost;;

let main () =
  let a = parse "apply_operator_test.ml" in
  let b = parse "global_handler_test.ml" in
  Caml.print_implem
    <:str_item@ghost<
      module Apply_operator_test = struct $a$ end;;
      module Global_handler_test = struct $b$ end >>
;;

try main ()
with e ->
  Format.eprintf "error: %a@." Camlp4.ErrorHandler.print e;
  exit 1;;
(*******************************)

Best regards,

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-03-30 16:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-28 22:36 camlp4 3.10 questions Hendrik Tews
2007-03-29  9:48 ` [Caml-list] " Nicolas Pouillard
2007-03-29 15:50   ` Hendrik Tews
2007-03-29 16:10     ` Nicolas Pouillard
2007-03-29 21:47   ` Hendrik Tews
2007-03-29 21:57     ` Nicolas Pouillard
2007-03-30 11:13       ` Hendrik Tews
2007-03-30 16:13         ` Nicolas Pouillard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).