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

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