utop icon indicating copy to clipboard operation
utop copied to clipboard

Dumping AST facilities

Open XVilka opened this issue 7 years ago • 9 comments

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.

XVilka avatar Dec 27 '18 13:12 XVilka

Make a pull request?

pmetzger avatar Dec 30 '18 19:12 pmetzger

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

XVilka avatar Jan 02 '19 05:01 XVilka

I don't know either. :)

pmetzger avatar Jan 02 '19 16:01 pmetzger

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)
  ]

?

ghost avatar Jan 07 '19 12:01 ghost

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

XVilka avatar Jan 07 '19 12:01 XVilka

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?

XVilka avatar Jan 07 '19 12:01 XVilka

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.

ghost avatar Jan 07 '19 13:01 ghost

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

pmetzger avatar Jan 07 '19 17:01 pmetzger

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 :)

Lupus avatar Mar 20 '23 06:03 Lupus