Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Dynamically evaluating OCaml code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: clement capel <clement.capel@f...>
Subject: Re: [Caml-list] Dynamically evaluating OCaml code

 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

> Le Mercredi 7 Avril 2004 18:47, John Goerzen a écrit :
> > 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 FAQ:
> > 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