11

Is there any way to run name resolution on an arbitrary expression without running it? e.g. I would like to take an expression such as

quote
    x = 1
    y = 2*x + 1
    z = x^2 - 1
    f(x) = 2*x + 1
end

and be told that the names defined in the scope of this block are x, y, z, f and the names *, +, ^, - are pulled in from outside the scope of this block. Bonus points if it can tell me that there's a sub-scope defined in the body of f which creates it's own name x and pulls in + from an enclosing scope.

This question appeared in the Julia Zulip community

1
  • You could deconstruct the whole syntax tree. If you find expressions which use names not assigned or defined in the current scope, they were pulled from the outer scope. Hope you get the idea :) Commented Oct 28, 2020 at 21:48

1 Answer 1

9

Thanks to Takafumi for showing me how to solve this on Zulip

We can get a list of locally defined names in the outermost scope of a julia expression like so:

ex = quote
    x = 1
    y = 2*x + 1
    z = x^2 - 1
    f(x) = 2*x + 1
end
using JuliaVariables, MLStyle

function get_locals(ex::Expr)
    vars = (solve_from_local ∘ simplify_ex)(ex).args[1].bounds
    map(x -> x.name, vars)
end

julia> get_locals(ex)
 4-element Array{Symbol,1}:
  :f
  :y
  :z
  :x

and we can get the symbols pulled in from outside the scope like this:

_get_outers(_) = Symbol[]
_get_outers(x::Var) = x.is_global ? [x.name] : Symbol[]
function _get_outers(ex::Expr)
    @match ex begin
        Expr(:(=), _, rhs) => _get_outers(rhs)
        Expr(:tuple, _..., Expr(:(=), _, rhs)) => _get_outers(rhs)
        Expr(_, args...) => mapreduce(_get_outers, vcat, args)
    end
end

get_outers(ex) = (unique! ∘ _get_outers ∘ solve_from_local ∘ simplify_ex)(ex)

julia> get_outers(ex)
 6-element Array{Symbol,1}:
  :+
  :*
  :-
  :^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.