caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Chris GauthierDickey" <chrisg@cs.uoregon.edu>
To: <caml-list@inria.fr>
Subject: [Caml-list] scripting ocaml from inside of ocaml
Date: Fri, 27 Jun 2003 09:51:35 -0700	[thread overview]
Message-ID: <001001c33ccc$5e878300$a806df80@chrisgd> (raw)

Hi all,

  Just a disclaimer--this is a long message.

  I've searched the FAQs, manuals, and mailing lists to no avail. I have an
application that I'm writing in ocaml, and I need to do some scripting for
the program. I'd like to use ocaml as the scripting language. I've seen
references to using ocaml to interpret scripts from files, etc, but these
all assume that your environment is closed to the scripting. In particular,
I would like to be able to use ocaml to interpret scripts, have scripts
refer to objects that I import into their environment, and be able to take
values from their bindings in the scripts environment and use them in my
ocaml code.

  In some ways, the toplevel program does some of what I want, so using the
actual ocaml source, I created a library with the following functions:


exception ScriptError

let eval_script doprint ppf script =
  (* first, create a lexical buffer *)
  let lbuf = Lexing.from_string script in
    try
      let phrase = try !parse_toplevel_phrase lbuf with Exit -> raise
ScriptError in
         if !Clflags.dump_parsetree then Printast.top_phrase ppf phrase;
         ignore(execute_phrase true ppf phrase)
    with
      | End_of_file -> exit 0
      | Sys.Break -> fprintf ppf "Interrupted.@."
      | ScriptError -> ()
      | x -> Errors.report_error ppf x

let init_scripting () =
  Sys.interactive := false;
  Compile.init_path();
  initialize_toplevel_env ()

In this way, I can use Toploop.toplevel_env to store the environment that is
created from evaluating the script. Toploop also has a few other functions
to check the environment, such as:

Toploop.getvalue : str -> Obj.t
and
Toploop.setvalue : str -> Obj.t -> unit

so, all of these allow me to do something like:

let main () =
  (* set up the environment for scripting *)
  init_scripting();
  run_script true Format.std_formatter "let add x y = x + y;;";
  run_script true Format.std_formatter "let x = add 2 3;;"
in
  main () ;;

and this adds a binding to add for the function, and x for the result in
Toploop.toplevel_env.

However, my dilema is this: How can I add my own bindings to Obj.t in
Toploop.toplevel_env
and how can I retrieve values from Obj.t?  Taking a look under the bytecomp
directory under
the source distribution seems to indicate that Obj.t is an abstract type
that represents all types
in bytecode. Does anyone know of a way I can manipulate Obj.t and create my
own bindings
and extract the results?

Thanks for listening (=,
Chris


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


             reply	other threads:[~2003-06-27 16:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-27 16:51 Chris GauthierDickey [this message]
2003-06-27 18:00 ` Yamagata Yoriyuki
2003-06-28  0:47   ` [Caml-list] Operation complexity Zed Sereg
2003-06-28  1:17     ` Karl Zilles
2003-07-01  9:47 ` [Caml-list] scripting ocaml from inside of ocaml Jean-Christophe Filliatre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='001001c33ccc$5e878300$a806df80@chrisgd' \
    --to=chrisg@cs.uoregon.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).