caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* First-class evaluation
@ 2000-11-21 14:28 John Matthews
  2000-11-21 19:11 ` Alain Frisch
  0 siblings, 1 reply; 2+ messages in thread
From: John Matthews @ 2000-11-21 14:28 UTC (permalink / raw)
  To: caml-list

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"?

Thanks,

-john



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: First-class evaluation
  2000-11-21 14:28 First-class evaluation John Matthews
@ 2000-11-21 19:11 ` Alain Frisch
  0 siblings, 0 replies; 2+ messages in thread
From: Alain Frisch @ 2000-11-21 19:11 UTC (permalink / raw)
  To: John Matthews; +Cc: caml-list

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-11-22  8:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-21 14:28 First-class evaluation John Matthews
2000-11-21 19:11 ` Alain Frisch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).