Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Dynamic usage of Ocaml evaluator?
@ 1998-06-18  8:48 Jan Skibinski
  1998-06-22  9:00 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Jan Skibinski @ 1998-06-18  8:48 UTC (permalink / raw)
  To: caml-list


I'm sorry for this unilingual message - I do not speak French.

I am in a process of writing a portable dynamic writer of Adobe PDF
documents. The package contains series of scripts for three functional
languages: Q, Haskell and Ocaml, which all interface to the same C
driver. The driver does the low level work of talking to PDFlib
- a freely available third party library.

The scripts make use of ascii files with data for plots, tables,
plain text and HTML-like tag annotated information. The data is
portable in a sense that any of those languages use the same
sources and produce the same output - the PDF documents. One special
pair of tags <eval> and </eval> admits sequences of a universal code,
which look alike in all three languages, and if this is not possible
because of the syntax differences, then the language specific tags can
be used, such as <ocaml> and </ocaml>.

The obvious way of parsing the code from the external files is to
take an advantage of the evaluators residing in the specific
interpreters (toplevels). With Q there is no problem at all:
it accepts strings containing Q expressions, compiles them on the
fly and evaluates them within the current environment.

Ocaml requires that the code should reside in a *.ml file, therefore
I create such a file on the fly, say "phrases.ml", write required "open"
instructions, copy code from tagged file to "phrases.ml", close the
file and finally invoke 'Toploop.run_script "phrases.ml"' to evaluate
the expressions from this file. They usually plot something, draw
tables, etc.

Is there any better way to do it? Is the code below correct? It works,
but I am afraid that it might leave the toplevel in some unstable state.
Normally I do exit from the toplevel after I finished producing the
document (70 or so pages), but I wonder what might go wrong on a long
run?
 
Jan Skibinski
Huntsville, Ontario, Canada



val eval_until : string -> out_channel -> number list 
		-> string -> unit
let eval_until t f lv s =
	(* --------------------------------- *)
	(* Read lines from file "f", ignore  *)
	(* empty lines and comments, and     *)
	(* evaluate lines of Ocaml code      *)
	(* until tag "t" is found.           *)
	(* --------------------------------- *)
	let temp = open_out "phrases.ml" in
	output_string temp "open Pdf;;\n";
	output_string temp "open Html;;\n";
	eval_until2 t f temp lv "";    (* copy code from f to temp *) 
	close_out temp;
	Toploop.run_script "phrases.ml"







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

* Re: Dynamic usage of Ocaml evaluator?
  1998-06-18  8:48 Dynamic usage of Ocaml evaluator? Jan Skibinski
@ 1998-06-22  9:00 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 1998-06-22  9:00 UTC (permalink / raw)
  To: Jan Skibinski, caml-list

For several reasons, "eval" is hard to support well in Caml.  There
are typing issues, of course.  Also, the Caml Light and Objective Caml
implementations are more targeted towards batch compilation than
towards run-time code generation.  (Remember, those two
implementations are not interpreters, but full compilers.)

This said, your solution with Toploop.run_script is reasonable, and
probably the simplest way to get what you want.  I'm assuming you're
not concerned about security, as it allows embedded scripts to do
pretty much anything the user could do (e.g. by calling Sys.command).

An alternate solution would be to compile the ML source by calling the
ocamlc compiler, then load the compiled bytecode using the Dynlink
library.  This allows more precise control on what the user code has
access to.  However, in your particular case it doesn't make much
sense to generate a .cmo file that you're going to use only once.

> Is there any better way to do it? Is the code below correct? It works,
> but I am afraid that it might leave the toplevel in some unstable state.
> Normally I do exit from the toplevel after I finished producing the
> document (70 or so pages), but I wonder what might go wrong on a long
> run?

I think it should work fine.  Some of the bytecode will accumulate in
memory (i.e. the bytecode for the functions defined in the embedded ML
text), but you'd really need large quantities of embedded ML before
that becomes noticeable.

Regards,

- Xavier Leroy





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

end of thread, other threads:[~1998-06-23  0:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-06-18  8:48 Dynamic usage of Ocaml evaluator? Jan Skibinski
1998-06-22  9:00 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox