ocaml - 带有 Toploop/TopLevel 的 ocamlbuild
问题描述
我希望在此答案中实现 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 有点陌生,所以我不确定如何解决这个问题。谢谢!
解决方案
顶级库仅在字节码模式下可用:
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);;
无需编译器库。
推荐阅读
- javascript - 通过组件的 displayName 应用 CSS?
- php - Laravel Virgin:通过 `artisan tinker` 获取所有配置
- php - lib/classes/Swift/Transport/EsmtpTransport.php 中的 Swift Mailer 错误日志
- java - 如何在java中只返回一个实例?
- stored-procedures - 是否可以将可变数量的参数传递给redshift中的存储过程?
- android - 包含 TableLayout 和 3 个 Textview 的 ExpandableListView 子项未触发单击侦听器
- spark-streaming - 在 Spark-Streaming 中使用 InputDStream 时导致垃圾收集器开销的原因
- mysql - 指定为定义者的用户不存在 - GRANT 无法修复
- c++ - 用 C++ 构建一个完全独立的wireshark解析器模块
- typescript - 打字稿:无法从模块导入类