julia lang - How to alter an expression in generic function? -
e.g.
function sq(x) x ^ 2 end function sq2(x) (x+1) ^ 2 end function fun(x) sq(x) end
i replace sq
call sq2
call redefine fun
generic function. attempt below changes call wasn't able redefine function. appreciated.
change(:fun, (int,)) function analyze_expr(exp::expr) = 1:length(exp.args) arg = exp.args[i] if(typeof(arg) == expr) analyze_expr(arg) elseif(arg==symbol("sq")) exp.args[i] = symbol("sq2") end end
end
function change(sym::symbol, params) func = eval(sym) func_code = code_lowered(func, params) func_body = func_code[1].args[3] analyze_expr(func_body) println("printing function body:",func_body) end
i suspect you'll find easier kind of work using macros: http://docs.julialang.org/en/latest/manual/metaprogramming/
given existing function definition, relevant thing in julia isn't syntax generated compiled machine code resulted. knowledge, modifying syntax won't (without deep hacking in julia's internals) have effect on compiled machine code.
Comments
Post a Comment