Emit an AST as Julia code?

Hi there! Does anyone know if there a function or library to emit an AST as Julia code (i.e. convert an AST back into normal human-readable Julia)?

1 Like

I recall MacroTools.jl having something like that. Worth checking out.

2 Likes

By “AST”, do you mean an Expr?

If so, simply printing an Expr displays it the form of Julia code:

julia> a = quote
         println("hello world!")
       end
quote
    #= REPL[1]:2 =#
    println("hello world!")
end

julia> println(a)
begin
    #= REPL[1]:2 =#
    println("hello world!")
end

For prettier printing, you can get rid of the line annotations using MacroTools.striplines:

julia> using MacroTools


julia> MacroTools.striplines(a) |> println
begin
    println("hello world!")
end
4 Likes

Thank you so much! That’s perfect!