繁体   English   中英

带有Toploop / TopLevel的ocamlbuild

[英]ocamlbuild with Toploop/TopLevel

我希望在此处实现此答案中的评估功能: https//stackoverflow.com/a/33293116/

但是,当我去编译我的代码示例时:

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

具有以下内容:

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

我收到错误消息:

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

我已经检查了editor-libs目录,但没有ocamltoplevel.cmxa文件,但确实有ocamltoplevel.cma文件。

我想知道这是否是简单的解决方法? 我对ocaml有点陌生,所以我不确定该如何解决。 谢谢!

顶级库仅在字节码模式下可用:

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

还要注意,也许需要单独安装editor-libs软件包(至少在archlinux中是这种情况)。

但是,您的代码可能并没有达到您的期望:仅将用户输入提供给顶级解释器,而没有从顶级状态中读取任何内容。

如果您只想读取一个整数,则可以使用以下方法简单地做到这一点:

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

无需编译器库。

暂无
暂无

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

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