有免费的 OCaml 到 C 转换器吗?

发布于 2024-08-29 01:34:06 字数 1536 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沙与沫 2024-09-05 01:34:06

这可能不是您想要的,但您可以让 OCaml 编译器将其运行时代码转储为 C:

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

您得到的基本上是字节码的静态转储。结果将类似于:

#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);
}

您可以使用以下命令编译它。

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

有关详细信息,请参阅 OCaml 手册

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.

不醒的梦 2024-09-05 01:34:06

有一个用于 C 源代码编译器的 OCaml 字节码可执行文件:
https://github.com/ocaml-bytes/ocamlcc

因此,首先将代码编译为字节码可执行文件然后使用
奥卡姆勒克。

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.

黯然#的苍凉 2024-09-05 01:34:06

OCamlJS 项目将是一个很好的起点。它将 OCaml 编译为 JavaScript;应该可以修改它以将 OCaml 编译为 ActionScript。编译为 C 可能会需要更多工作 - 没有任何类型的垃圾收集 - 但并非不可能,特别是如果 Adob​​e Alchemy 提供 API 来满足其中一些需求的话。

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.

合约呢 2024-09-05 01:34:06

如果我有一些 OCaml 代码,我想“在浏览器中”运行客户端(这似乎是您基于问题评论的意图),我不得不说我的第一个想法是执行以下操作之一:使用

  • 类似 < a href="http://ocamljava.x9c.fr/" rel="nofollow noreferrer">OcamlJava 到
    将 OCaml 编译为 java 字节码并
    使用 Java Web Start 或
    相似的。
  • 移植到 F#(Microsoft 版本
    OCaml)在.NET上运行并使用
    MS 提供的任何网络部署服务
    那。

也许如果我真的很疯狂:

  • 移植 OCaml 解释器(我
    相信是在“C”中实现的)
    使用 Alchemy 进行 Flash 并运行
    我的原始(未移植)代码的 OCaml 字节码。

不过,两阶段的 OCaml 到 C、C 到 Flash 并不真正有吸引力。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文