Version française
Home     About     Download     Resources     Contact us    
Browse thread
First-class evaluation
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alain Frisch <frisch@c...>
Subject: Re: First-class evaluation
On Tue, 21 Nov 2000, John Matthews wrote:

> Does the Ocaml interpreter provide a mechanism for interactively evaluating
> strings or abstract syntax tree representations of Ocaml code? I'm looking
> for something along the lines of Common Lisp's "eval" mechanism. If so, does
> it work in the presence of the source-level debugger? That is, can I step
> into a call to "eval"?

The undocumented toplevellib.cma library provides some entry points to the
toplevel evaluator. To communicate with the main program, the "script"
may access the modules loaded before the call to the evaluator.

For instance:

File top_test.ml:
====
open Toploop;;

let exec s = 
  let ast = !parse_toplevel_phrase (Lexing.from_string s) in
  ignore (execute_phrase false Format.std_formatter ast);;

exec "open Pervasives;;";;
exec "Share.result := 10 + 2*3;;";;

Printf.printf "Result = %i\n" !Share.result;;
====

File share.ml:
====
let result = ref 0;;
====


You compile and link with:
ocamlc -o top_test toplevellib.cma share.ml top_test.ml


The source and interfaces for the library is in the toplevel/ subdirectory
of the ocaml distribution.


I think you can't use the debugger the way you would like: if you trace
the call to execute_phrase, you debug the evaluator, not the code you
evaluate.

Hope this helps.


-- 
  Alain Frisch