ocaml-community / utop

Universal toplevel for OCaml
Other
846 stars 113 forks source link

Dumping AST facilities #267

Open XVilka opened 5 years ago

XVilka commented 5 years ago

When you write the PPX extension using dumpast from ppx_tools is often not enough or inconvenient to use. It might be a killer feature of utop - to dump the AST of some expression. Something like "#dumpast" command in utop might be solution. Moreover it makes sense to support also metaquot syntax in it, to make life of PPX developer even easier.

pmetzger commented 5 years ago

Make a pull request?

XVilka commented 5 years ago

@pmetzger If I only knew how to implement this...

pmetzger commented 5 years ago

I don't know either. :)

ghost commented 5 years ago

What about:

$ utop-full
# #require "compiler-libs";;
# #require "ppx_tools";;
# Format.printf "%a@." (Printast.expression 0) [%expr 1 + 1];;
expression (_none_[1,0+-1]..[1,0+-1]) ghost
  Pexp_apply
  expression (_none_[1,0+-1]..[1,0+-1]) ghost
    Pexp_ident "+" (_none_[1,0+-1]..[1,0+-1]) ghost
  [
    <arg>
    Nolabel
      expression (_none_[1,0+-1]..[1,0+-1]) ghost
        Pexp_constant PConst_int (1,None)
    <arg>
    Nolabel
      expression (_none_[1,0+-1]..[1,0+-1]) ghost
        Pexp_constant PConst_int (1,None)
  ]

?

XVilka commented 5 years ago

Strange, didn't work in my utop - it doesn't understand [%expr...]. even though #require "ppx_tools";; worked without any error.

XVilka commented 5 years ago

Never mind, found out it should be #require "ppx_tools.metaquot";; obviously. Anyway, having a shortcut for this would be useful for utop, what do you think? Should I send a PR for this?

ghost commented 5 years ago

I suppose we could add a function in the UTop module (src/lib/uTop.mli), a PR is welcome. Note that it cannot be a directive as directives cannot take arbitrary expressions as arguments.

pmetzger commented 5 years ago

@XVilka Also add documentation for the new facility. :)

Lupus commented 1 year ago

More modern recipe using ppxlib will be something like this:

#require "ppxlib";;
#require "ppxlib.metaquot";;
# open Ppxlib;;
# let loc = Location.none;;
# [%expr 1+1];;

An alias in utop would still be nice though :)