caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* scripting the toplevel
@ 2007-05-30 15:27 Yitzhak Mandelbaum
  2007-05-30 15:56 ` [Caml-list] " Markus E.L.
  0 siblings, 1 reply; 3+ messages in thread
From: Yitzhak Mandelbaum @ 2007-05-30 15:27 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1013 bytes --]

Hi,

I'm interested in writing scripts that can execute code in the ocaml  
toplevel as if the user had added the input by hand. In particular,  
I'd like to let the user pass parameters to my script and then use  
those. For example,

let user_module = Sys.argv.(1);;
#load user_module;;

Or, more ambitiously,

let user_expr = Sys.argv.(1);;
user_expr;;

The first example doesn't work, as #load needs a string literal and  
the second example works, but does not produce the desired effect -  
that of interpreting and executing an ocaml expression passed on the  
command line.  Currently, to get around these limitations I'm  
collecting the user's parameters, using them to fill in a script  
template, dumping the filled-in template into a temp file, and then  
executing "ocaml" on the temp file. I'd prefer to avoid this level of  
complexity.

Thanks,
Yitzhak Mandelbaum

--------------------------------------------------
Yitzhak Mandelbaum
AT&T Labs - Research

http://www.research.att.com/~yitzhak



[-- Attachment #2: Type: text/html, Size: 3535 bytes --]

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

* Re: [Caml-list] scripting the toplevel
  2007-05-30 15:27 scripting the toplevel Yitzhak Mandelbaum
@ 2007-05-30 15:56 ` Markus E.L.
  2007-05-30 16:46   ` Markus E.L.
  0 siblings, 1 reply; 3+ messages in thread
From: Markus E.L. @ 2007-05-30 15:56 UTC (permalink / raw)
  To: caml-list



I picked the following trick from Gerd Stolpmann's findlib;

   exception Toplevel_error of string;;

   let toplevel_exec s =
     let l = Lexing.from_string s in
     let ph = !Toploop.parse_toplevel_phrase l in
     let fmt = Format.make_formatter (fun _ _ _ -> ()) (fun _ -> ()) in
       if not (Toploop.execute_phrase false fmt ph)
       then raise (Toplevel_error s)
   ;;


You can now do

   toplevel_exec "#load " ^ (String.escaped user_module) ^ ";;"

Regards -- Markus


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

* Re: [Caml-list] scripting the toplevel
  2007-05-30 15:56 ` [Caml-list] " Markus E.L.
@ 2007-05-30 16:46   ` Markus E.L.
  0 siblings, 0 replies; 3+ messages in thread
From: Markus E.L. @ 2007-05-30 16:46 UTC (permalink / raw)
  To: caml-list


> I picked the following trick from Gerd Stolpmann's findlib;
>
>    exception Toplevel_error of string;;
>
>    let toplevel_exec s =
>      let l = Lexing.from_string s in
>      let ph = !Toploop.parse_toplevel_phrase l in
>      let fmt = Format.make_formatter (fun _ _ _ -> ()) (fun _ -> ()) in
>        if not (Toploop.execute_phrase false fmt ph)
>        then raise (Toplevel_error s)
>    ;;

You'd probably want to have a different formatter at 'fmt = ...': This
formatter just suppresses arror messages which was good enough for my
purposes (extending the toplevel by some new directives :-) ).

Regards -- Markus


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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-30 15:27 scripting the toplevel Yitzhak Mandelbaum
2007-05-30 15:56 ` [Caml-list] " Markus E.L.
2007-05-30 16:46   ` Markus E.L.

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