caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Opening a module from toploop startup
@ 2013-07-01 21:30 Michael Le Barbier Grünewald
  2013-07-09  9:26 ` [Caml-list] " Michael
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Le Barbier Grünewald @ 2013-07-01 21:30 UTC (permalink / raw)
  To: caml-list

Dear Camlers,

I am learning to produce customised toplevel loops and, while I was able
to automatically add custom printers, could not open modules.

Obviously, there is something I should know, and don't… could someone
help me?

My code (working example, see below) is a bit long, so let me outline it:

The _startup_ procedure issues a few toploop directives, adding search
directories, installing printers for the given modules (currently one)
and opening them.

_open_module_ and _install_printer_ produce and interpret toplevel
phrases corresponding to the directives with the same name.

Tank you for your help,
Michael


----8<----
Compile:

    |ocamlfind ocamlc -c -package "compiler-libs" -o demonstrate.cmo
    demonstrate.ml|||
    |||ocamlfind ocamlmktop -package "compiler-libs" -custom -linkall
    -dllpath-all -linkpkg -package "compiler-libs" demonstrate.cmo -o
    mytoplevel|||
    ||

Demonstrate that the Demonstrate.Tag module is not opened:

    |> ./mytoplevel|||
    |||        OCaml version 4.00.1|||
    ||||
    |||# test_open_module;;|||
    |||Error: Unbound value test_open_module|||
    |||# ^D|||
    ||

Demonstrate that the custom printer is installed:

    |> ./mytoplevel|||
    |||        OCaml version 4.00.1|||
    ||||
    |||# open Demonstrate;;|||
    |||# (5 : Tag.t);;|||
    |||- : Demonstrate.Tag.t = Tag#5|||
    |||# ^D|

The whole code:
~~~~
|open Format||
||
||module Tag =||
||struct||
||  type t = int||
||  let format fft tag =||
||    fprintf fft "Tag#%d" tag||
||  let test_open_module () =||
||    true||
||end||
||
||let libdir = [||
||  "/usr/local/lib/ocaml/compiler-libs";||
||]||
||
||let execute_phrase ident phrase =||
||  if Toploop.execute_phrase false Format.std_formatter phrase||
||  then||
||    ()||
||  else||
||    failwith ident||
||
||let rec make_path_rev p =||
||  match p with||
||  | [] -> invalid_arg "make_path_rev"||
||  | h :: [] -> Longident.Lident h||
||  | h :: t -> Longident.Ldot (make_path_rev t, h)||
||
||let make_path p =||
||  make_path_rev (List.rev p)||
||    ||
||let install_printer path =||
||  let longident = make_path_rev ("format" :: List.rev path) in||
||  let phrase = Parsetree.Ptop_dir(||
||    "install_printer", Parsetree.Pdir_ident longident||
||  )||
||  in||
||  execute_phrase "install_printer" phrase||
||
||
||let open_module p =||
||  let open Parsetree in||
||  let open Location in||
||  let open Asttypes in||
||  let loc = {||
||    loc_start = Lexing.dummy_pos;||
||    loc_end = Lexing.dummy_pos;||
||    loc_ghost = false||
||  }||
||  in||
||  let phrase = Ptop_def [||
||    {||
||      pstr_desc =||
||    Pstr_open {||
||      txt = make_path p;||
||      loc;||
||    };||
||      pstr_loc = loc||
||    };||
||  ]||
||  in||
||  execute_phrase "open_module" phrase||
||
||let startup () =||
||  begin||
||    List.iter Topdirs.dir_directory libdir;||
||    List.iter install_printer [["Demonstrate"; "Tag"]];||
||    List.iter open_module [["Demonstrate"]; ["Demonstrate"; "Tag" ]];||
||  end||
||
||let _ =||
||  Toploop.toplevel_startup_hook := startup|
---->8----

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-07-14 18:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-01 21:30 [Caml-list] Opening a module from toploop startup Michael Le Barbier Grünewald
2013-07-09  9:26 ` [Caml-list] " Michael
2013-07-09  9:33   ` Daniel Bünzli
2013-07-14 18:23     ` Michael
2013-07-09  9:36   ` Anil Madhavapeddy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).