在 OCaml 中嵌入 Lua

发布于 2024-07-10 16:53:06 字数 1003 浏览 6 评论 0原文

您能否提供一段代码片段来展示如何使用 OCaml 中嵌入的 Lua?

一个简单的例子就是“Hello, World”的变体。 让 OCaml 提示用户输入名称。 然后将该名称传递给 Lua 函数。 让 Lua 打印问候语并返回姓名的长度。 然后让 OCaml 打印一条有关名称长度的消息。

例子:

user@desktop:~$ ./hello.opt

姓名? 用户

您好,用户。

您的名字有 4 个字母长。

用户@桌面:~$

[Edit]

作为一个非 C 程序员,我是否可以实现这个而无需编写中间 C 程序来在 Lua 和 OCaml 之间传递数据?

以下是我想尝试的理论想法。 不幸的是,ocaml_hello.ml 的第 3 行需要知道如何调用 lua_hello.lua 中定义的函数才能使代码有效。

lua_hello.lua 定义 lua_hello,它打印一个参数并返回其长度。

1  function lua_hello (name)
2    print ("Hello, "..name..".")
3    return (string.len (name))
4  end

ocaml_hello.ml OCaml 提示输入名称,调用 Lua 函数,并打印返回值。

1  let () = print_string "Name? "; flush stdout in
2  let name = input_line stdin in
3  let len  = Lua_hello.lua_hello name in
4    Printf.printf "Your name is %d letters long." len; flush stdout;;

Could you, please, give a code snippet showing how to use Lua embedded in OCaml?

A simple example could be a "Hello, World" variant. Have OCaml prompt the user for a name. Then pass that name to a Lua function. Have Lua print a greeting and return the length of the name. Then have OCaml print a message about the length of the name.

Example:

user@desktop:~$ ./hello.opt

Name? User

Hello, User.

Your name is 4 letters long.

user@desktop:~$

[Edit]

As a non-C programmer, could I implement this without having to write an intermediary C program to pass the data between Lua and OCaml?

Following is a theoretical idea of what I would like to try. Unfortunately, line 3 of ocaml_hello.ml would need to know how to call the function defined in lua_hello.lua in order for the code to be valid.

lua_hello.lua
Defines lua_hello, which prints an argument and returns its length.

1  function lua_hello (name)
2    print ("Hello, "..name..".")
3    return (string.len (name))
4  end

ocaml_hello.ml
OCaml prompts for a name, calls the Lua function, and prints the return value.

1  let () = print_string "Name? "; flush stdout in
2  let name = input_line stdin in
3  let len  = Lua_hello.lua_hello name in
4    Printf.printf "Your name is %d letters long." len; flush stdout;;

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

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

发布评论

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

评论(2

白龙吟 2024-07-17 16:53:06

我不知道有一套成熟的绑定可以将 Lua 的 C 实现嵌入到 OCaml 中。 2004 年在 Caml 邮件列表上发布了一组不成熟的绑定

如果您想使用 ML 实现,您可以在名为 机器学习模块狂热。 与 C 实现不同,ML 实现保证类型安全,但为此它在 ML 模块系统中使用了一些非常卑鄙的技巧。 如果您问的是基本问题,您可能想避免这种情况。

在您的示例中,很难猜测您想要该函数来自哪里。 我建议你要么索要一个 C 示例,要么给人们一个 C 示例并询问如何在 OCaml 中实现它(尽管我认为绑定将是一个问题)。


编辑

针对修改后的问题,它非常复杂。 通常的模型是让 Lua 负责,然后从 Lua 调用 Objective Caml 代码。 你让 Caml 负责,这让事情变得更加复杂。 下面是大概的情况:

let lua = Lua.new()  (* create Lua interpreter *)
let chunk = LuaL.loadfile lua "hello.lua" (* load and compile the file hello.lua *)
let _ = Lua.call lua 0 0 (* run the code to create the hello function *)
let lua_len s =
  (* push the function; push the arg; call; grab the result; pop it; return *)
  let _ = Lua.getglobal lua "lua_hello" in
  let _ = Lua.pushstring lua s in
  let _ = Lua.call lua 1 1 in
  let len = Lua.tointeger lua (-1) in
  let _ = Lua.pop lua 1 in
  len

let () = print_string "Name? "; flush stdout
let name = input_line stdin
let len  = lua_len name
Printf.printf "Your name is %d letters long." len; flush stdout;;

同样,我不知道从哪里可以获得 LuaLuaL 模块的绑定。

I'm not aware of a mature set of bindings for embedding the C implementation of Lua into OCaml. An immature set of bindings was posted on the Caml mailing list in 2004.

If you want to use the ML implementation you can find some examples in a paper called ML Module Mania. The ML implementation, unlike the C implementation, guarantees type safety, but to do so it uses some very scurvy tricks in the ML module system. If you are asking basic questions, you probably want to avoid this.

In your example it's a little hard to guess where you want the function to come from. I suggest you either ask for a C example or give people a C example and ask how it could be realized in OCaml (though I think bindings are going to be a problem).


Edit

In response to the revised question, it's pretty complicated. The usual model is that you would put Lua in charge, and you would call Objective Caml code from Lua. You're putting Caml in charge, which makes things more complicated. Here's a rough sketch of what things might look like:

let lua = Lua.new()  (* create Lua interpreter *)
let chunk = LuaL.loadfile lua "hello.lua" (* load and compile the file hello.lua *)
let _ = Lua.call lua 0 0 (* run the code to create the hello function *)
let lua_len s =
  (* push the function; push the arg; call; grab the result; pop it; return *)
  let _ = Lua.getglobal lua "lua_hello" in
  let _ = Lua.pushstring lua s in
  let _ = Lua.call lua 1 1 in
  let len = Lua.tointeger lua (-1) in
  let _ = Lua.pop lua 1 in
  len

let () = print_string "Name? "; flush stdout
let name = input_line stdin
let len  = lua_len name
Printf.printf "Your name is %d letters long." len; flush stdout;;

Again, I don't know where you'll get the bindings for the Lua and LuaL modules.

糖果控 2024-07-17 16:53:06

经过进一步思考,我不确定是否可以使用 Lua 的官方 C 实现来做到这一点,因为我认为 OCaml 相信它拥有 main()。 您必须确定 OCaml 是否可以从 C 主程序打包为库。

有关让 Lua-ML 负责的示例,您可以获取 Lua-ML 独立版来自 Cminusminus.org,您还可以查看 中的示例关于 Lua-ML 的论文以及 QC-- 编译器本身的源代码。

On further reflection, I'm not sure if you can do this with the official C implementation of Lua, because I think OCaml believes it owns main(). You'd have to find out if OCaml could be packaged as a library from a C main program.

For an example of putting Lua-ML in charge, you can get Lua-ML standalone from Cminusminus.org, and you can also check out the examples in the paper on Lua-ML as well as the source code to the QC-- compiler itself.

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