Re: a simple example of compiler-libs.toplevel?
Florian Angeletti <[email protected]> Fri, 5 Jun 2026 14:18:41 +0200
| Newsgroups | gmane.comp.lang.caml.inria |
|---|---|
| Message-ID | <[email protected]> |
To share the answer discussed during the compiler office hours, I have a very minimal example of a shallow wrapper around compiler-libs.toplevel (with at least its own toplevel loop) which replaces the prompt with a "λ" at https://github.com/Octachron/minitop Note also that the toplevel API is quite awkward and it tends to reflect what is used by the vanilla toplevel rather than what would be useful in general for a REPL. There are been remarks on this point from time to time and I rather agree with them, but I have yet to found the time to solve those paper cuts. Le 15/04/2026 à 14:45, Kenichi Asai a écrit : > Five years ago, I asked a question to this mailing list on how to > create a copy of the OCaml toplevel using compiler-libs (so that I can > customize it). I am trying it again. The thread is here: > > https://inbox.vuxu.org/caml-list/CANhEzE5tOqq7JXA47tmkQcnF6Bz70eU8jPBT+=+g2EwQ7cNf6Q@mail.gmail.com/T/ > > What I did is to copy the necessary files from the OCaml source (I am > working on OCaml 4.14.2.): > > topstart.ml, topmain.ml, toploop.ml, topeval.ml, topdirs.ml > topcommon.ml, trace.ml, genprintval.ml > > rename them to my_topstart.ml, my_topmain.ml, etc, rename all the > module references to the new ones (My_toploop instead of Toploop, etc.) > and compiled them with compiler-libs. See the toplevel1 directory of: > > https://github.com/kenichi-asai/toplevel-experiment > > However, when I compile them (by "make"), and execute the binary, I get: > > $ ./toplevel > OCaml version 4.14.2 > Enter #help;; for help. > > # let a = 3;; > Error: Reference to undefined global `Toploop' > # > > Why does this happen? > > When I search for "Toploop" in the OCaml source outside the toplevel > directory, I found lambda/translmod.ml mentions it: > > let toploop_ident = Ident.create_persistent "Toploop" > > Does this mean the toplevel must be given by the Toploop module > instead of My_toploop? > > As a second attempt, I created the toplevel2 directory of the above > repository, where I did not rename files to "my_*" but used the > original names. When I compiled them and execute the binary, I get > segmentation fault (!): > > $ ./toplevel > OCaml version 4.14.2 > Enter #help;; for help. > > # let a = 3;; > Segmentation fault > $ > > What's wrong? How can I create a copy of the OCaml toplevel using > compiler-libs? Any help appreciated. > > Another question. I also want to use dune to compile, instead of > OCamlMakefile. I provided dune and dune_project files in the > repository, but when I execute dune build, I obtain the following. > What am I doing wrong? > > $ dune build > File "dune", line 2, characters 7-18: > 2 | (name my_topstart) > ^^^^^^^^^^^ > Undefined symbols for architecture x86_64: > "_caml_get_current_environment", referenced from: > _camlDune__exe__My_topmain__fun_2483 in dune__exe__My_topmain.o > _camlDune__exe__My_topmain__1 in dune__exe__My_topmain.o > ld: symbol(s) not found for architecture x86_64 > clang: error: linker command failed with exit code 1 (use -v to see invocation) > File "caml_startup", line 1: > Error: Error during linking (exit code 1) > > Thank you in advance! >