# Way to get string representation of an Expression?

**URL:** <https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 15, 2017, 5:10am UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778 "2017-12-15T05:10:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 15, 2017, 5:10am UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/1 "2017-12-15T05:10:04Z")

</div>

Let’s say you have a deeply nested Expr node.

How do you turn that into a string that could (manually) be copied and pasted into the REPL?

* * *

**edit:** related github [issue#25121](https://github.com/JuliaLang/julia/issues/25121)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 15, 2017, 5:21am UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/2 "2017-12-15T05:21:05Z")

</div>

`string`?

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 15, 2017, 5:35am UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/3 "2017-12-15T05:35:35Z")

</div>

The output from `string()` sometimes looks like:

```julia
$(Expr(:stagedfunction, :(dcontract(S1::FourthOrderTensor{dim}, S2::FourthOrderTensor{dim}) where dim), quote # none, line 3:
    TensorType = getreturntype(dcontract, get_base(S1), get_base(S2)) # none, line 4:
    ...
    exps = Expr(:tuple) # none, line 7:
    for l = 1:dim, k = 1:dim, j = 1:dim, i = 1:dim # none, line 8:
        ex1 = (Expr[(Core._expr)(:ref, $(Expr(:copyast, :($(QuoteNode(:(get_data(S1))))))), idxS1(i, j, m, n)) for m = 1:dim, n = 1:dim])[:] # none, line 9:
        ex2 = (Expr[(Core._expr)(:ref, $(Expr(:copyast, :($(QuoteNode(:(get_data(S2))))))), idxS2(m, n, k, l)) for m = 1:dim, n = 1:dim])[:] # none, line 10:
        push!(exps.args, reducer(ex1, ex2, true))
    end # none, line 12:
    expr = remove_duplicates(TensorType, exps) # none, line 13:
    (Core._expr)(:block, $(Expr(:copyast, :($(QuoteNode(:( # none, line 14:)))))), Expr(:meta, :inline), $(Expr(:copyast, :($(QuoteNode(:( # none, line 15:)))))), (Core._expr)(:macrocall, Symbol("@inbounds"), (Core._expr)(:return, (Core._expr)(:call, TensorType, expr))))
end))

```

Isn’t there enough information there to make a string without just being an `Expr` with quotes around it?

* * *

// **edit:** a simpler example would be:

```julia
julia> parse("import Woof; import Bowow")

:($(Expr(:toplevel, :(import Woof), :(import Bowow))))

```

* * *

similar posts:

- [deparse()](https://github.com/JuliaLang/julia/issues/2017)
- [Pretty print expressions](https://github.com/JuliaLang/julia/pull/5713)
- [Any function to generate code in String from Expr?](https://groups.google.com/forum/#!topic/julia-users/LGIApGcLWoE)
- [Pretty-printing of AST:s](https://groups.google.com/forum/#!topic/julia-dev/jrTao4VYhJ4)

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 15, 2017, 7:34pm UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/4 "2017-12-15T19:34:21Z")

</div>

I guess the real questions now are:

- Is this even possible? (Turn the above expression back into `"import Woof; import Bowow"`)
- Is this functionality present in some existing package?
  - [https://github.com/SimonDanisch/Transpiler.jl](https://github.com/SimonDanisch/Transpiler.jl)
  - [GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for working with Julia code and expressions.](https://github.com/MikeInnes/MacroTools.jl)
  - [https://github.com/JuliaLang/JuliaParser.jl](https://github.com/JuliaLang/JuliaParser.jl)

- And maybe, why has this not been implemented yet?

// just wanted to do the research before I try to code something up

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [December 17, 2017, 9:36am UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/5 "2017-12-17T09:36:06Z")

</div>

If you just want to paste it into the repl and you have the expression available you can do that without turning it into a string first - just use `eval`.

Other than that, `string` is probably what you’re looking for. Sometimes the result looks a bit arcane, but you should be able to `eval` it. For example,

```julia
julia> eval(:($(Expr(:toplevel, :(import Woof), :(import Bowow)))))
ERROR: ArgumentError: Module Woof not found in current path.
Run `Pkg.add("Woof")` to install the Woof package.
Stacktrace:
 ...

```

If you wanted to do source to source translation, I believe CSTParser is currently the most capable package, but that’s probably not what you’re looking for.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [December 17, 2017, 8:26pm UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/6 "2017-12-17T20:26:43Z")

</div>

> [@c42f](#):
>
> If you wanted to do source to source translation, I believe CSTParser is currently the most capable package, but that’s probably not what you’re looking for.

That is what I’m after. I was just wondering if anyone has made some headway on it.

see: [Difference b/w include("foo.jl") and eval(parse(readstring(open( "foo.jl" ))))](https://discourse.julialang.org/t/difference-b-w-include-foo-jl-and-eval-parse-readstring-open-foo-jl/7807)

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [September 4, 2021, 2:04pm UTC](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/7 "2021-09-04T14:04:55Z")

</div>

As I found somewhere on them internets, you can use `Base.show_unquoted` to convert any `Expr` to a string of valid Julia code:

```julia
julia> Base.show_unquoted(stdout, :(import Woof; import Bowow))
begin
    import Woof
    #= REPL[5]:1 =#
    import Bowow
end

```

To convert to a string, use `IOBuffer`, or write to a file:

```julia
julia> buf = IOBuffer();

julia> Base.show_unquoted(buf, :(import Woof; import Bowow))

julia> String(take!(buf))
"begin\n import Woof\n #= REPL[11]:1 =#\n import Bowow\nend"

```
