使用Toploop/TopLevel进行ocamlbuild
我希望实现一个eval函数,如下所示: 但是,在编译代码示例时:使用Toploop/TopLevel进行ocamlbuild,ocaml,ml,ocamlbuild,ocamlfind,ocaml-toplevel,Ocaml,Ml,Ocamlbuild,Ocamlfind,Ocaml Toplevel,我希望实现一个eval函数,如下所示: 但是,在编译代码示例时: 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
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.
我已经检查了编译器libs目录,没有ocamltoplevel.cmxa文件,但有一个ocamltoplevel.cma文件
我想知道这是否是一个简单的解决方案?我对ocaml有点陌生,所以我不确定如何着手解决这个问题。谢谢 顶级库仅在字节码模式下可用:
ocamlbuild UserInputEval.byte -pkgs compiler-libs,compiler-libs.toplevel
还要注意,编译器libs包可能需要单独安装(至少对于archlinux是这样)
然而,您的代码可能并没有达到预期的效果:您只是将用户输入提供给顶级解释器,而没有从顶级状态读取任何内容
如果您只想读取一个整数,只需执行以下操作:
let a = print_string "Enter sum_until x where x = an int: \n"; read_int ();;
print_int (sum_until a);;
不需要任何编译器库