From 85fdc92007223ebe6b55c27ea5c0e113ed3d7567 Mon Sep 17 00:00:00 2001 From: Carlo Baldassi Date: Sat, 9 Feb 2013 05:37:37 +0100 Subject: [PATCH] Add `@which` macro To be used directly in front of a function call, e.g.: @which rand(1:10) @which 1 + im @which a[1,2] @which a[1:end] = 2 --- base/exports.jl | 1 + base/util.jl | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/base/exports.jl b/base/exports.jl index 6f240f36e3903..6ad312e107078 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1124,6 +1124,7 @@ export @time, @timed, @elapsed, + @which, @windows_only, @unix_only, @osx_only, diff --git a/base/util.jl b/base/util.jl index b373896ff9f01..a5c04166dfb28 100644 --- a/base/util.jl +++ b/base/util.jl @@ -106,6 +106,28 @@ end which(f, args...) = whicht(f, map(a->(isa(a,Type) ? Type{a} : typeof(a)), args)) +macro which(ex) + ex = expand(ex) + exret = expr(:call, :error, "expression is not a function call") + if !isa(ex, Expr) + # do nothing -> error + elseif ex.head == :call + exret = expr(:call, :which, map(esc, ex.args)...) + elseif ex.head == :body + a1 = ex.args[1] + if isa(a1, Expr) && a1.head == :call + a11 = a1.args[1] + if isa(a11, TopNode) && a11.name == :assign + exret = expr(:call, :which, eval(expr(:toplevel, :assign)), map(esc, a1.args[2:end])...) + end + end + elseif ex.head == :thunk + exret = expr(:call, :error, "expression is not a function call, or is too complex for @which to analyze; " + * "break it down to simpler parts if possible") + end + exret +end + edit(file::String) = edit(file, 1) function edit(file::String, line::Integer) editor = get(ENV, "JULIA_EDITOR", "emacs")