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 XAA08306; Wed, 7 Apr 2004 23:25:11 +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 XAA08701 for ; Wed, 7 Apr 2004 23:25:10 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from postfix3-2.free.fr (postfix3-2.free.fr [213.228.0.169]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i37LP9YM023588 for ; Wed, 7 Apr 2004 23:25:09 +0200 Received: from 192.168.0.2 (massena-7-81-56-56-104.fbx.proxad.net [81.56.56.104]) by postfix3-2.free.fr (Postfix) with ESMTP id 1D785DF9F for ; Wed, 7 Apr 2004 23:25:09 +0200 (CEST) From: clement capel To: caml-list@inria.fr Subject: Re: [Caml-list] Dynamically evaluating OCaml code Date: Wed, 7 Apr 2004 23:25:30 +0000 User-Agent: KMail/1.5 References: <200404072306.15109.clement.capel@free.fr> In-Reply-To: <200404072306.15109.clement.capel@free.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200404072325.30198.clement.capel@free.fr> 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; capel:99 capel:99 caml-list:01 dynamically:01 toploop:01 toploop:01 formatter:01 toplevellib:01 3.07:01 2004:99 python:01 python's:01 python:01 usr:01 bug:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 115 Try this simple example: =20 Toploop.initialize_toplevel_env();; let eval txt =3D let lb =3D (Lexing.from_string txt) in let phr =3D !Toploop.parse_toplevel_phrase lb in Toploop.execute_phrase true Format.std_formatter phr;; eval "let add1 x =3D 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=20 "string parameter" of the function eval or if you evaluate it in the toplevel.=20 But it seems there's a guard with the new version (3.07+2). Hope it will help you. Regards, Cl=E9ment > Le Mercredi 7 Avril 2004 18:47, John Goerzen a =E9crit : > > Hello, > > > > I am moving from Python to OCaml and one of the things I miss is > > Python's eval() call. It takes a string representing a bit of Python > > source code, evaluates it, and returns the result. I would like to be > > able to do similar things with OCaml. > > > > I have observed that /usr/bin/ocaml, the interactive top-level, is > > itself written in OCaml, which suggests that this should be possible. > > Although I have tried to study the source for this, it seems extremely > > complex and I can't figure out a way to do the simple evaluation > > described above. > > > > Can anyone help me out here? > > > > Thanks, > > John > > > > > > ------------------- > > To unsubscribe, mail caml-list-request@inria.fr Archives: > > http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FA= Q: > > http://caml.inria.fr/FAQ/ Beginner's list: > > http://groups.yahoo.com/group/ocaml_beginners ------------------- 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