简体   繁体   English

带有Toploop / TopLevel的ocamlbuild

[英]ocamlbuild with Toploop/TopLevel

I'm looking to implement an eval function like in this answer here: https://stackoverflow.com/a/33293116/ 我希望在此处实现此答案中的评估功能: https//stackoverflow.com/a/33293116/

However, when I go to compile my code sample: 但是,当我去编译我的代码示例时:

let eval code =
  let as_buf = Lexing.from_string code in
  let parsed = !Toploop.parse_toplevel_phrase as_buf in
  ignore (Toploop.execute_phrase true Format.std_formatter parsed)

let rec sum_until n =
  if n = 0
  then 0
  else n + sum_until (n - 1);;

let a = print_string "Enter sum_until x where x = an int: "; read_line ();;
print_int eval a;;

with the following: 具有以下内容:

ocamlbuild UserInputEval.native -pkgs compiler-libs,compiler-libs.toplevel

I am getting the error: 我收到错误消息:

File "_none_", line 1: Error: Cannot find file
/usr/lib/ocaml/compiler-libs/ocamltoplevel.cmxa Command exited with
code 2.

I have checked the compiler-libs directory and I don't have an ocamltoplevel.cmxa file but I do have an ocamltoplevel.cma file. 我已经检查了editor-libs目录,但没有ocamltoplevel.cmxa文件,但确实有ocamltoplevel.cma文件。

I'm wondering if this is a simple fix? 我想知道这是否是简单的解决方法? I'm a bit new to ocaml so I'm not sure how to go about fixing this. 我对ocaml有点陌生,所以我不确定该如何解决。 Thanks! 谢谢!

The toplevel library is only available in bytecode mode: 顶级库仅在字节码模式下可用:

ocamlbuild UserInputEval.byte -pkgs compiler-libs,compiler-libs.toplevel

Note also that the compiler-libs package may need to be installed separately (this is at least the case for archlinux). 还要注意,也许需要单独安装editor-libs软件包(至少在archlinux中是这种情况)。

Nevertheless, your code is probably not doing what are you expecting: you are only feeding the user input to the toplevel interpreter without reading anything from the toplevel state. 但是,您的代码可能并没有达到您的期望:仅将用户输入提供给顶级解释器,而没有从顶级状态中读取任何内容。

If you just want to read an integer, you can do it with simply: 如果您只想读取一个整数,则可以使用以下方法简单地做到这一点:

let a = print_string "Enter sum_until x where x = an int: \n"; read_int ();;
print_int (sum_until a);;

without any need for compiler-libs. 无需编译器库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM