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
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
string
?
The output from string()
sometimes looks like:
$(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> parse("import Woof; import Bowow")
:($(Expr(:toplevel, :(import Woof), :(import Bowow))))
similar posts:
I guess the real questions now are:
"import Woof; import Bowow"
)// just wanted to do the research before I try to code something up
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> 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.
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" ))))
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> 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> 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"