[julia-users] Re: Replace symbols in the AST

2014-09-22 Thread David P. Sanders
Thanks to all for your ideas. Independently of Gray, I arrived at something pretty similar, as follows. It then turned out that this wasn't in the end what I needed for my package, but I am working on something very similar. transform(a::BigFloat) = a transform(a::Number) = :(

[julia-users] Re: Replace symbols in the AST

2014-09-19 Thread David P. Sanders
El viernes, 19 de septiembre de 2014 08:34:05 UTC-5, David P. Sanders escribió: Hi, for a package I'm writing (or, more precisely, trying to write), I need to do the following: Change `:(3 + pi)` into `:(3 + big(pi))` or `:(BigFloat(3) + BigFloat(pi))` I forgot to say that I need

[julia-users] Re: Replace symbols in the AST

2014-09-19 Thread David P. Sanders
In fact, I will also need to wrap floating-point constants like 2.1 as BigFloat(2.1), I believe. (Apologies for the self-replies.)

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Isaiah Norton
To do what you want, very briefly: x.args[3] == :pi x.args[3] = Expr(:call, :BigFloat, :pi) This will make more sense if you look at `xdump` output to see how Exprs are structured (and `xdump` is quite handy to know about in general). Try: xdump(:(big(3) + big(pi))) ... On Fri, Sep 19,

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Isaiah Norton
BigFloat(pi) is undefined, so that should be `Expr(:call, :big, :pi)`, but hopefully the rest makes sense. On Fri, Sep 19, 2014 at 9:58 AM, Isaiah Norton isaiah.nor...@gmail.com wrote: To do what you want, very briefly: x.args[3] == :pi x.args[3] = Expr(:call, :BigFloat, :pi) This will

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread David P. Sanders
El viernes, 19 de septiembre de 2014 08:58:56 UTC-5, Isaiah escribió: To do what you want, very briefly: x.args[3] == :pi x.args[3] = Expr(:call, :BigFloat, :pi) This will make more sense if you look at `xdump` output to see how Exprs are structured (and `xdump` is quite handy to know

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread David P. Sanders
El viernes, 19 de septiembre de 2014 09:26:09 UTC-5, David P. Sanders escribió: El viernes, 19 de septiembre de 2014 08:58:56 UTC-5, Isaiah escribió: To do what you want, very briefly: x.args[3] == :pi x.args[3] = Expr(:call, :BigFloat, :pi) This will make more sense if you look at

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread John Myles White
Depth-first search and replace? — John On Sep 19, 2014, at 7:32 AM, David P. Sanders dpsand...@gmail.com wrote: El viernes, 19 de septiembre de 2014 09:26:09 UTC-5, David P. Sanders escribió: El viernes, 19 de septiembre de 2014 08:58:56 UTC-5, Isaiah escribió: To do what you

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Isaiah Norton
Yes, that's what I need, thanks. Now I need to iterate over the (in principle arbitrarily complex, i.e. nested) syntax tree and do this everywhere. Is there a standard method for this kind of iteration? I guess some kind of recursion. I'll give it a go... Beyond that, you might find some

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Paweł Biernat
Hi, you might try something like this: function mapleaves(f,expr) if typeof(expr) == Expr expr.args[2:end]=map(e-mapleaves(f,e),expr.args[2:end]) return expr else return :($f($expr)) end end you can apply it like this: eval(mapleaves(BigFloat,:(1+sin(1 I

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Gray Calhoun
On Friday, September 19, 2014 9:32:09 AM UTC-5, David P. Sanders wrote: El viernes, 19 de septiembre de 2014 09:26:09 UTC-5, David P. Sanders escribió: El viernes, 19 de septiembre de 2014 08:58:56 UTC-5, Isaiah escribió: To do what you want, very briefly: x.args[3] == :pi x.args[3]

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Gray Calhoun
Oops, the second method was a mutating biggen! It should have been function biggen(x::Expr) Meta.isexpr(x, :call) || error(Only works for calls right now) x = copy(x) ## Otherwise we'll rewrite the arguments of the original expression for i in 2:length(x.args) x.args[i] =

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Gray Calhoun
The Expr method had an error and would overwrite the expressions in its argument. Here's a better version. function biggen(x::Expr) Meta.isexpr(x, :call) || error(Only works for calls right now) y = copy(x); for i in 2:length(y.args) y.args[i] = biggen(x.args[i]) end

Re: [julia-users] Re: Replace symbols in the AST

2014-09-19 Thread Tim Holy
Additional examples in base/cartesian On Friday, September 19, 2014 07:26:09 AM David P. Sanders wrote: El viernes, 19 de septiembre de 2014 08:58:56 UTC-5, Isaiah escribió: To do what you want, very briefly: x.args[3] == :pi x.args[3] = Expr(:call, :BigFloat, :pi) This will make more