Is there any free OCaml to C translator? [closed]

北城以北 提交于 2019-11-29 17:56:45

问题


So I have nice OCaml code (50000 lines). I want to port it to C. So Is there any free OCaml to C translator?


回答1:


This probably isn't what you want, but you can get the OCaml compiler to dump its runtime code in C:

ocamlc -output-obj -o foo.c foo.ml

What you get is basically a static dump of the bytecode. The result will look something like:

#include <caml/mlvalues.h>
CAMLextern void caml_startup_code(
           code_t code, asize_t code_size,
           char *data, asize_t data_size,
           char *section_table, asize_t section_table_size,
           char **argv);
static int caml_code[] = {
0x00000054, 0x000003df, 0x00000029, 0x0000002a, 0x00000001, 0x00000000, 
/* ... */
}

static char caml_data[] = {
132, 149, 166, 190, 0, 0, 3, 153, 0, 0, 0, 118, 
/* ... */
};

static char caml_sections[] = {
132, 149, 166, 190, 0, 0, 21, 203, 0, 0, 0, 117, 
/* ... */
};

/* ... */

void caml_startup(char ** argv)
{
    caml_startup_code(caml_code, sizeof(caml_code),
                      caml_data, sizeof(caml_data),
                      caml_sections, sizeof(caml_sections),
                      argv);
}

You can compile it with

gcc -L/usr/lib/ocaml foo.c -lcamlrun -lm -lncurses

For more information, see the OCaml manual.




回答2:


There is an OCaml bytecode executable file to C source code compiler: https://github.com/ocaml-bytes/ocamlcc

So, first compile your code to a bytecode executable then use ocamlcc.




回答3:


The OCamlJS project would be a good starting point. It compiles OCaml to JavaScript; it should be possible to modify it to compile OCaml to ActionScript. Compiling to C would probably be more work - no garbage collection of any kind - but not impossible, particularly if Adobe Alchemy provides APIs to meet some of those needs.




回答4:


If I had some OCaml code I wanted to run client-side "in the browser" (which seems to be your intent based on comments with the question), I have to say my first thought would be to do one of

  • Use something like OcamlJava to compile OCaml to java bytecode and deploy that using Java web start or similar.
  • Port to F# (Microsoft version of OCaml) running on .NET and use whatever MS provides to web-deploy that.

And maybe if I was really crazy:

  • Port the OCaml interpreter (which I believe is implemented in 'C') to Flash using Alchemy and have it run the OCaml bytecode of my original (unported) code.

A two-stage OCaml-to-C, C-to-Flash doesn't really appeal though.



来源:https://stackoverflow.com/questions/2638664/is-there-any-free-ocaml-to-c-translator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!