Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] adding an ocaml interpreter to my C program
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-01-08 (10:37)
From: Adolf Mathias <dolfi@z...>
Subject: Re: [Caml-list] adding an ocaml interpreter to my C program
Dear list,

Kip Macy wrote:

>>Also, it only works for bytecode.  (And it only works for compiled
>>code - linking in the toplevel so you can parse ML directly is a
>>different thing entirely).
>I didn't read the message closely enough initially. Linking in the
>toplevel is my stated intention. I'd like to be able to use ocaml
>as an application scripting language. This is doable, but not a 2
>minute copy-paste operation as with perl.
As Rich said, the first one seems simple, and the second one is an 
entirely different story. I tried to copy some of the toplevel stuff, 
and got the following function working:

let myeval str =
  let lb  = Lexing.from_string str in
  let phr = !Toploop.parse_toplevel_phrase lb in
  ignore(Toploop.execute_phrase true Format.std_formatter phr);;

With it, I can interactively eval strings, like in
#myeval "sin 45.0;;";;
- : float = 0.850903524534118438
- : unit = ()

Hooray, it works. It becomes obvious though that the scope of O'Caml is 
slightly different than that of embeddable scripting languages like Lua, 
where lua_dostring (the equivalent of the above) is a fundamental 
library function, and written in C.

There is one thing that puzzles me though: I can't get access to symbols 
like Parse.toplevel_phrase like it is done in, but have to 
access it via !Toploop.parse_toplevel_phrase.
Is this a question for the Ocaml beginner's list?


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: