我希望在此答案中实现 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.
我检查了编译器库目录,我没有 ocamltoplevel.cmxa 文件,但我有一个 ocamltoplevel.cma 文件。
我想知道这是否是一个简单的修复?我对 ocaml 有点陌生,所以我不确定如何解决这个问题。谢谢!