caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] adding an ocaml interpreter to my C program
@ 2004-01-07  9:01 Kip Macy
  2004-01-07 13:00 ` Richard Jones
  0 siblings, 1 reply; 7+ messages in thread
From: Kip Macy @ 2004-01-07  9:01 UTC (permalink / raw)
  To: caml-list

It is possible to add many scripting languages as configuration
languages for an arbitrary C program. One can transfer control
to the interpreter, let it use the C bindings to modify program
state, and then get control back. I'll give perl as an example:

void sourceperl(int argc, char **argv) {
        PerlInterpreter gdb_perl = perl_alloc();
        perl_construct(gdb_perl);
        PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
        perl_parse(gdb_perl, NULL, argc, argv, (char **)NULL);
        perl_run(gdb_perl);
        perl_destruct(gdb_perl);

}

I don't see any obvious way of doing this with ocaml. I could link in
GDB to the toplevel and use the toplevel as a frontend, but that isn't
what I'm interested in doing.

I'm using perl as a powerful macro language for gdb by just doing:
(gdb) sourceperl mycoreanalyser.pl
<OUTPUT>

I'd like to also be able to support:
(gdb) sourceocaml mycoranalyser.ml

Is there any way for me to do this?


Thanks.


				-Kip





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


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

* Re: [Caml-list] adding an ocaml interpreter to my C program
  2004-01-07  9:01 [Caml-list] adding an ocaml interpreter to my C program Kip Macy
@ 2004-01-07 13:00 ` Richard Jones
  2004-01-07 15:42   ` Kip Macy
  2004-01-07 21:09   ` Kip Macy
  0 siblings, 2 replies; 7+ messages in thread
From: Richard Jones @ 2004-01-07 13:00 UTC (permalink / raw)
  To: Kip Macy; +Cc: caml-list

On Wed, Jan 07, 2004 at 01:01:00AM -0800, Kip Macy wrote:
> It is possible to add many scripting languages as configuration
> languages for an arbitrary C program. One can transfer control
> to the interpreter, let it use the C bindings to modify program
> state, and then get control back. I'll give perl as an example:
[...]
> I don't see any obvious way of doing this with ocaml.

It's documented here (right at the bottom, in the "Advanced" section):

http://caml.inria.fr/ocaml/htmlman/manual032.html

Basically, you call caml_main, which runs your initialization code
(written in OCaml), which registers C callbacks, which your C can then
execute by calling callbackN ().

What the manual doesn't tell you is that none of this stuff works at
all if your C code is contained in a dynamically linked library (.so
file), unless you undertake some pretty horrific hacks.  Particularly
if you plan to use the OCaml Dynlink module at the same time.  See the
code in my mod_caml project[1] for details.

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'm using perl as a powerful macro language for gdb by just doing:
> (gdb) sourceperl mycoreanalyser.pl
> <OUTPUT>

You might also be interested in [2].

Rich.

[1] http://www.merjis.com/developers/mod_caml/
[2] http://www.merjis.com/developers/perl4caml/

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
If I have not seen as far as others, it is because I have been
standing in the footprints of giants.  -- from Usenet

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


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

* Re: [Caml-list] adding an ocaml interpreter to my C program
  2004-01-07 13:00 ` Richard Jones
@ 2004-01-07 15:42   ` Kip Macy
  2004-01-07 16:04     ` Richard Jones
  2004-01-07 21:09   ` Kip Macy
  1 sibling, 1 reply; 7+ messages in thread
From: Kip Macy @ 2004-01-07 15:42 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

Thanks. Not quite as trivial as it is with perl, but not a major
undertaking.

					-Kip


On Wed, 7 Jan 2004, Richard Jones wrote:

> On Wed, Jan 07, 2004 at 01:01:00AM -0800, Kip Macy wrote:
> > It is possible to add many scripting languages as configuration
> > languages for an arbitrary C program. One can transfer control
> > to the interpreter, let it use the C bindings to modify program
> > state, and then get control back. I'll give perl as an example:
> [...]
> > I don't see any obvious way of doing this with ocaml.
>
> It's documented here (right at the bottom, in the "Advanced" section):
>
> http://caml.inria.fr/ocaml/htmlman/manual032.html
>
> Basically, you call caml_main, which runs your initialization code
> (written in OCaml), which registers C callbacks, which your C can then
> execute by calling callbackN ().
>
> What the manual doesn't tell you is that none of this stuff works at
> all if your C code is contained in a dynamically linked library (.so
> file), unless you undertake some pretty horrific hacks.  Particularly
> if you plan to use the OCaml Dynlink module at the same time.  See the
> code in my mod_caml project[1] for details.
>
> 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'm using perl as a powerful macro language for gdb by just doing:
> > (gdb) sourceperl mycoreanalyser.pl
> > <OUTPUT>
>
> You might also be interested in [2].
>
> Rich.
>
> [1] http://www.merjis.com/developers/mod_caml/
> [2] http://www.merjis.com/developers/perl4caml/
>
> --
> Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
> Merjis Ltd. http://www.merjis.com/ - improving website return on investment
> If I have not seen as far as others, it is because I have been
> standing in the footprints of giants.  -- from Usenet
>

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


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

* Re: [Caml-list] adding an ocaml interpreter to my C program
  2004-01-07 15:42   ` Kip Macy
@ 2004-01-07 16:04     ` Richard Jones
  2004-01-07 16:51       ` Damien Doligez
  0 siblings, 1 reply; 7+ messages in thread
From: Richard Jones @ 2004-01-07 16:04 UTC (permalink / raw)
  Cc: caml-list

On Wed, Jan 07, 2004 at 07:42:38AM -0800, Kip Macy wrote:
> Thanks. Not quite as trivial as it is with perl, but not a major
> undertaking.

Actually, perhaps I didn't describe it too well, but the OCaml case is
probably quite a bit simpler than Perl.  What would be really nice for
OCaml would be some way to have all the identifiers properly prefixed,
eg. with caml_ and CAML_.  Even if was an optional
-DCAML_CLEAN_SYMBOLS or something, that would be a huge help.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment

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


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

* Re: [Caml-list] adding an ocaml interpreter to my C program
  2004-01-07 16:04     ` Richard Jones
@ 2004-01-07 16:51       ` Damien Doligez
  0 siblings, 0 replies; 7+ messages in thread
From: Damien Doligez @ 2004-01-07 16:51 UTC (permalink / raw)
  To: caml-list

> Actually, perhaps I didn't describe it too well, but the OCaml case is
> probably quite a bit simpler than Perl.  What would be really nice for
> OCaml would be some way to have all the identifiers properly prefixed,
> eg. with caml_ and CAML_.  Even if was an optional
> -DCAML_CLEAN_SYMBOLS or something, that would be a huge help.

Just check out the CVS version.  I spent some of my Christmas holidays
implementing exactly that.

Every global symbol exported by the runtime system now starts with
"caml_", with the only exception of "main", and every global symbol
produced by ocamlopt now starts with "caml".

The change is transparent: any C source that interfaces with OCaml
still works as before, thanks to a bunch of #defines in a new
header file (compatibility.h), which is included from the other
caml header files.  In your code that uses the new names directly,
you can #define CAML_NAME_SPACE before including the header files,
and then compatibility.h is not included.

I may have broken some of the native-code ports in the process,
so if you have one of the more exotic architectures, please try
the CVS version and report any problems you find.  It was tested
on PPC/MacOSX, i386/Linux, alpha/Tru64Unix, and IA64 (I don't know
which OS).

We still need to do it to the pieces of C code in otherlibs.

We haven't done it for the macros, because name space pollution
is much less problematic at that level.

-- Damien

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


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

* Re: [Caml-list] adding an ocaml interpreter to my C program
  2004-01-07 13:00 ` Richard Jones
  2004-01-07 15:42   ` Kip Macy
@ 2004-01-07 21:09   ` Kip Macy
  2004-01-08 16:40     ` Adolf Mathias
  1 sibling, 1 reply; 7+ messages in thread
From: Kip Macy @ 2004-01-07 21:09 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

> 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.

				-Kip

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


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

* Re: [Caml-list] adding an ocaml interpreter to my C program
  2004-01-07 21:09   ` Kip Macy
@ 2004-01-08 16:40     ` Adolf Mathias
  0 siblings, 0 replies; 7+ messages in thread
From: Adolf Mathias @ 2004-01-08 16:40 UTC (permalink / raw)
  To: caml-list

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 toplevel.ml, but have to 
access it via !Toploop.parse_toplevel_phrase.
Is this a question for the Ocaml beginner's list?

Dolfi

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


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

end of thread, other threads:[~2004-01-08 10:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-07  9:01 [Caml-list] adding an ocaml interpreter to my C program Kip Macy
2004-01-07 13:00 ` Richard Jones
2004-01-07 15:42   ` Kip Macy
2004-01-07 16:04     ` Richard Jones
2004-01-07 16:51       ` Damien Doligez
2004-01-07 21:09   ` Kip Macy
2004-01-08 16:40     ` Adolf Mathias

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).