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 XAA07311; Tue, 13 Apr 2004 23:28:25 +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 XAA07530 for ; Tue, 13 Apr 2004 23:28:24 +0200 (MET DST) Received: from feline (63-228-144-98.phnx.qwest.net [63.228.144.98]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3DLSMYM029154 for ; Tue, 13 Apr 2004 23:28:23 +0200 Received: from awwaiid by feline with local (Exim 3.36 #1 (Debian)) id 1BDVP6-0005YH-00 for ; Tue, 13 Apr 2004 14:25:12 -0700 Date: Tue, 13 Apr 2004 14:25:12 -0700 From: Brock To: caml-list@inria.fr Subject: [Caml-list] eval for OCaml Message-ID: <20040413212512.GA21319@thelackthereof.org> References: <200404072306.15109.clement.capel@free.fr> <200404072325.30198.clement.capel@free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200404072325.30198.clement.capel@free.fr> User-Agent: Mutt/1.5.5.1+cvs20040105i X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; 3.07:01 toploop:01 toploop:01 formatter:01 ihd:99 ocamlc:01 toplevellib:01 ihd:99 avoided:01 --brock:01 capel:99 formatter:01 toplevellib:01 3.07:01 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 284 Based on Clement's example, I created a usable eval. I am using The Objective Caml compiler, version 3.07+2 Here is the code: testeval.ml --------------------------------------------------------- exception EvalError (* Reset the eval environment *) let eval_reset() = Toploop.initialize_toplevel_env() (* Eval some text. We get no result from this *) let evalp txt = try let lb = (Lexing.from_string txt) in let phr = !Toploop.parse_toplevel_phrase lb in ignore(Toploop.execute_phrase false Format.std_formatter phr) with _ -> raise EvalError (* Get the content of some defined variable *) let evalv v = Obj.obj (Toploop.getvalue v) (* Eval some text and then return some defined variable *) let eval v txt = evalp txt; evalv v let _ = eval_reset(); evalp "let add1 x = x +1;;"; let n : int = eval "n" "let n = add1 2;;" in print_string "n = "; print_int n; print_newline() ------------------------------------------------- brock@ihd103:~/tmp$ ocamlc -o evaltest toplevellib.cma evaltest.ml brock@ihd103:~/tmp$ ./evaltest n = 3 ------------------------------------------------- Just as a reminder, THIS IS NOT TYPESAFE and is quite dangerous and should be avoided at nearly all costs. :) Improvements might include more shortcuts - for example one like (* Evaluate an expression, return the result *) let evalexp txt = eval "v" ("let v = " ^ txt ^ ";;") --Brock On 2004.04.07.23.25, clement capel wrote: | | Try this simple example: | | Toploop.initialize_toplevel_env();; | | let eval txt = let lb = (Lexing.from_string txt) in | let phr = !Toploop.parse_toplevel_phrase lb in | Toploop.execute_phrase true Format.std_formatter phr;; | | eval "let add1 x = x +1;;";; | eval "add1 2;;";; | | (compile with toplevellib.cma) | | But be careful, it can break the typing system. | if you use the Toploop module in the | "string parameter" of the function eval or if you | evaluate it in the toplevel. | But it seems there's a guard with the new version (3.07+2). | | | | Hope it will help you. | | Regards, | | Cl?ment ------------------- 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