From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA18802; Fri, 27 Jun 2003 18:51:41 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA26230 for ; Fri, 27 Jun 2003 18:51:39 +0200 (MET DST) Received: from cs.uoregon.edu (vitalstatistix.cs.uoregon.edu [128.223.4.19]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h5RGpbj15019 for ; Fri, 27 Jun 2003 18:51:38 +0200 (MET DST) Received: from chrisgd (dyna6-168.cs.uoregon.edu [128.223.6.168]) by cs.uoregon.edu (8.12.9/8.12.8) with SMTP id h5RGpZBJ014917 for ; Fri, 27 Jun 2003 09:51:36 -0700 (PDT) Message-ID: <001001c33ccc$5e878300$a806df80@chrisgd> Reply-To: "Chris GauthierDickey" From: "Chris GauthierDickey" To: Subject: [Caml-list] scripting ocaml from inside of ocaml Date: Fri, 27 Jun 2003 09:51:35 -0700 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Spam: no; 0.00; buffer:01 clflags:01 parsetree:01 printast:01 fprintf:01 toploop:01 formatter:01 retrieve:99 bytecomp:01 chris:01 bindings:01 ocaml:01 bytecode:01 toplevel:01 dump:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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