Browse thread
[Caml-list] Dynamically evaluating OCaml code
-
John Goerzen
- Vitaly Lugovsky
- Samuel Mimram
-
Basile Starynkevitch
-
Issac Trotts
- Dustin Sallings
-
Brian Hurt
- Oleg Trott
- Ville-Pertti Keinonen
-
John Goerzen
-
Markus Mottl
-
Richard Jones
-
Markus Mottl
- Jon Harrop
-
John Goerzen
- Jean-Marc EBER
-
Trevor Andrade
-
Gerd Stolpmann
- skaller
-
John Goerzen
-
Gerd Stolpmann
-
Christophe TROESTLER
-
Gerd Stolpmann
-
Christophe TROESTLER
- Brandon J. Van Every
- John Goerzen
- Jacques GARRIGUE
-
Christophe TROESTLER
-
Gerd Stolpmann
-
Christophe TROESTLER
- Matt Gushee
-
Gerd Stolpmann
- Benjamin Geer
-
Gerd Stolpmann
- skaller
-
Markus Mottl
- John Goerzen
- Jon Harrop
-
Richard Jones
- Fernando Alegre
- Jean-Marc EBER
- Kenneth Knowles
- Brian Hurt
- skaller
-
Markus Mottl
- Issac Trotts
- Basile Starynkevitch
-
Issac Trotts
-
clement capel
- Brock
- Jon Harrop
- Walid Taha
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Brock <rbw3@c...> |
| Subject: | [Caml-list] eval for OCaml |
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