Pergunta

I'm trying to understand the ASTs of Julia methods. An example is the following function:

function inner(o, p)
  s = A1(o, p)
  s = s + A2(o, p)
end

Calling show(code_lowered(inner, (Int64, Int64))[1]) will display something like

:($(Expr(:lambda, {:o,:p}, {{:#s908,:s},{{:o,:Any,0},{:p,:Any,0},{:#s908,:Any,18},{:s,:Any,2}},{}}, quote  # /home/pool/projekt/julia/grouping.jl, line 7:
    s = A1(o,p) # line 8:
    #s908 = +(s,A2(o,p))
    s = #s908
    return #s908
end)))

My question is how to interpret the part {{:#s908,:s},{{:o,:Any,0},{:p,:Any,0},{:#s908,:Any,18},{:s,:Any,2}},{}} it seems to be two local variables {:#s908,:s}. After this there are a number of symbols listed, both function arguments and local variables again, each have type information and an Int, what is this for? I'm guessing it is listing variables in the local scope and the Int is some sort of attribute? What are the possible values and meanings of these? Are they documented somewhere?

Foi útil?

Solução

If you want the true AST, you don't need to use code_lowered, just quote:

julia> ex = quote function inner(o, p)
         s = A1(o, p)
         s = s + A2(o, p)
       end end
quote  # none, line 1:
    function inner(o,p) # none, line 2:
        s = A1(o,p) # line 3:
        s = +(s,A2(o,p))
    end
end

julia> 

julia> dump(ex)
Expr 
  head: Symbol block
  args: Array(Any,(2,))
    1: Expr 
      head: Symbol line
      args: Array(Any,(2,))
        1: Int64 1
        2: Symbol none
      typ: Any
    2: Expr 
      head: Symbol function
      args: Array(Any,(2,))
        1: Expr 
          head: Symbol call
          args: Array(Any,(3,))
          typ: Any
        2: Expr 
          head: Symbol block
          args: Array(Any,(4,))
          typ: Any
      typ: Any
  typ: Any

You're looking at the annotated AST after removing syntactic sugar.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top