[英]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.