caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Camlp4: varargs ie a la Printf
@ 2002-06-17 14:38 Basile STARYNKEVITCH
  2002-06-17 14:59 ` Alain Frisch
  2002-06-18  6:27 ` Daniel de Rauglaudre
  0 siblings, 2 replies; 4+ messages in thread
From: Basile STARYNKEVITCH @ 2002-06-17 14:38 UTC (permalink / raw)
  To: caml-list

Dear All Camlp4-ers,

I am using OCaml 3.04 (with the standard Camlp4 3.04) I do know that
the CVS Ocaml does have a Printf.kprintf function which would make my
problem easier to solve.

My problem is how to have a sort of variadic macro.



(**** file pa_trace.ml *****)

(******
    this file provide a tracing preprocessor facility; 
   at compile time, you can preprocess with the -Tfoo flag (there can be several such flags);
   in the preprocessed source, you can code 
      trace FOO "i=%d x=%g" i x end
   which is preprocessed to 
      if (Trace.Tracing.foo) then Trace.trace Trace.Level.Foo (Printf.sprintf "i=%d x=%g" i x) ;
   if preprocessed with -Tfoo or just to the unit value
      () 
   otherwise 
  ******)


module SetOfName = Set.Make(
  struct 
    type t= string;;
    let compare s1 s2 = Pervasives.compare s1 s2 
  end);;

let trset = ref SetOfName.empty;;

(* function to add tracing *)
let add_tracing trname = 
  trset := SetOfName.add trname !trset
;;

let is_traced trname =
  (SetOfName.is_empty !trset) || (SetOfName.mem trname !trset)
;;

EXTEND
  GLOBAL: Pcaml.expr ;
  Pcaml.expr: LEVEL "top" 
  [ [ "trace" ; trid = UIDENT ; fmt = STRING ; args = LIST0 Pcaml.expr ; "end" ->
    let lctrid = String.lowercase trid in 
    Printf.eprintf "trace %s does not work yet\n" lctrid;
    if is_traced lctrid then
      <:expr< 
      if Trace.Tracing.$lid:lctrid$ 
      then Trace.trace Trace.Level.$lid:lctrid$ 
	(Printf.sprintf $str:fmt$ 
	 (**** WHAT SHOULD BE ADDED HERE? *****)
	) 
      else () 
	  >>
    else 
      <:expr< () >>
    ] ]
  ;
END;


(****** end of file *****)

My problem is to produce a quotation of a list of expressions (and not
a quotation of a list expression). What should I put in place of the
comment WHAT SHOULD BE ADDED HERE?   trace FOO "i=%d x=%g" i x end

I am not sure to understand what the antiquotation  $list:l$ is for.

With the future Ocaml I would just expand
   trace FOO "i=%d x=%g" i x end
to
   if (Trace.Tracing.foo) then Printf.kprintf (Trace.ktrace Trace.Level.Foo) "i=%d x=%g" i x

which I believe should be simpler.



Perhaps the question is easy, but it is really too hot now
here..... so I am coding poorly.

If Ocaml3.05 is out soon I might use kprintf anyway....

Otherwise if some people have some tracing or logging macros (which
they accept to share into a GPL-ed code?) I would be grateful.

Thanks.
-- 

N.B. Any opinions expressed here are only mine, and not of my organization.
N.B. Les opinions exprimees ici me sont personnelles et n engagent pas le CEA.

---------------------------------------------------------------------
Basile STARYNKEVITCH   ----  Commissariat à l Energie Atomique * France
DRT/LIST/DTSI/SLA * CEA/Saclay b.528 (p111f) * 91191 GIF/YVETTE CEDEX 
phone:+33 1,6908.6055; fax: 1,6908.8395 home: 1,4665.4553; mobile: 6,8501.2359
work email: Basile point Starynkevitch at cea point fr 
home email: Basile at Starynkevitch point net

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: varargs ie a la Printf
  2002-06-17 14:38 [Caml-list] Camlp4: varargs ie a la Printf Basile STARYNKEVITCH
@ 2002-06-17 14:59 ` Alain Frisch
  2002-06-17 16:20   ` John Malecki
  2002-06-18  6:27 ` Daniel de Rauglaudre
  1 sibling, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2002-06-17 14:59 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list

On Mon, 17 Jun 2002, Basile STARYNKEVITCH wrote:

> My problem is to produce a quotation of a list of expressions (and not
> a quotation of a list expression). What should I put in place of the
> comment WHAT SHOULD BE ADDED HERE?   trace FOO "i=%d x=%g" i x end

I'm not sure to understand how this is more difficult than the solution
with Printf.kprintf; in both case, you want to generate a 'varargs'
application, right ?  Or what is a "list of expressions" ?

Try something like:

List.fold_left (fun e_1 e_2 -> <:expr< $e_1$ $e_2$ >>)


-- Alain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: varargs ie a la Printf
  2002-06-17 14:59 ` Alain Frisch
@ 2002-06-17 16:20   ` John Malecki
  0 siblings, 0 replies; 4+ messages in thread
From: John Malecki @ 2002-06-17 16:20 UTC (permalink / raw)
  To: Alain Frisch; +Cc: Basile STARYNKEVITCH, caml-list

Alain Frisch wrote (2002-06-17T16:59:05+0200):
 > On Mon, 17 Jun 2002, Basile STARYNKEVITCH wrote:
 > 
 > > My problem is to produce a quotation of a list of expressions (and not
 > > a quotation of a list expression). What should I put in place of the
 > > comment WHAT SHOULD BE ADDED HERE?   trace FOO "i=%d x=%g" i x end
 > 
 > I'm not sure to understand how this is more difficult than the solution
 > with Printf.kprintf; in both case, you want to generate a 'varargs'
 > application, right ?  Or what is a "list of expressions" ?
 > 
 > Try something like:
 > 
 > List.fold_left (fun e_1 e_2 -> <:expr< $e_1$ $e_2$ >>)

Here is what i normally use in my parsers

  let expr_of_list loc l =
    let f e l = <:expr< [$e$ :: $l$] >> in
    List.fold_right f l <:expr< [] >>

as in

  cnds: [ [ cs = LIST1 cnd SEP "and" -> expr_of_list loc cs ] ];
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: varargs ie a la Printf
  2002-06-17 14:38 [Caml-list] Camlp4: varargs ie a la Printf Basile STARYNKEVITCH
  2002-06-17 14:59 ` Alain Frisch
@ 2002-06-18  6:27 ` Daniel de Rauglaudre
  1 sibling, 0 replies; 4+ messages in thread
From: Daniel de Rauglaudre @ 2002-06-18  6:27 UTC (permalink / raw)
  To: caml-list

Hi,

On Mon, Jun 17, 2002 at 04:38:45PM +0200, Basile STARYNKEVITCH wrote:

>   Pcaml.expr: LEVEL "top" 
>   [ [ "trace" ; trid = UIDENT ; fmt = STRING ; args = LIST0 Pcaml.expr ; "end" ->
>     let lctrid = String.lowercase trid in 
> [...]
> 	(Printf.sprintf $str:fmt$ 
> 	 (**** WHAT SHOULD BE ADDED HERE? *****)

John Malecki answered correcly (how to create an AST of a list), but
I would just add that "args = LIST0 Pcaml.expr" does not work without
separator, because if you have "a b c" on input, it does not return
the list of a, b and c, but the application of the function a to b and
c.

A solution is to call Pcaml.expr at the level just after the application,
i.e. for the normal and revised syntaxes: ".":
        args = LIST0 (Pcaml.expr LEVEL ".)

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-06-18  6:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-17 14:38 [Caml-list] Camlp4: varargs ie a la Printf Basile STARYNKEVITCH
2002-06-17 14:59 ` Alain Frisch
2002-06-17 16:20   ` John Malecki
2002-06-18  6:27 ` Daniel de Rauglaudre

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).