我想在这里实现一个eval函数: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.
我检查了compiler-libs目录,但我没有ocamltoplevel.cmxa文件,但我有一个ocamltoplevel.cma文件。
我想知道这是否是一个简单的解决方案?我对ocaml有点新意,所以我不确定如何解决这个问题。谢谢!
顶层库仅在字节码模式下可用:
ocamlbuild UserInputEval.byte -pkgs compiler-libs,compiler-libs.toplevel
另请注意,可能需要单独安装compiler-libs软件包(这至少是archlinux的情况)。
然而,你的代码可能没有做你期望的事情:你只是将用户输入提供给顶层解释器而不从顶层状态读取任何东西。
如果你只想读取一个整数,你可以简单地完成:
let a = print_string "Enter sum_until x where x = an int: \n"; read_int ();;
print_int (sum_until a);;
不需要编译器-libs。