# AST example in julia

**URL:** https://discourse.julialang.org/t/ast-example-in-julia/58625
**Category:** New to Julia
**Created:** [April 5, 2021, 3:26pm UTC](https://discourse.julialang.org/t/ast-example-in-julia/58625 "2021-04-05T15:26:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![csango](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csango/32/23654_2.png) [@csango](https://discourse.julialang.org/u/csango)
#### Post date: [April 5, 2021, 3:26pm UTC](https://discourse.julialang.org/t/ast-example-in-julia/58625/1 "2021-04-05T15:26:27Z")

</div>

Hi,

I was trying to generate the AST for the following code snippet:

```julia
function alma(c)
    f(x,y) = 3x + 4y
    A = Int8[1, 2, 3] * c
    B = [4.0, 5.0, 6.0] .+ c
    return f(A,B)/c
end

```

```julia
str = open(f->read(f, String), "expr.jl")
ex = Meta.parse(str)
dump(ex)

```

I got the following:

```julia
Expr
  head: Symbol function
  args: Array{Any}((2,))
    1: Expr
      head: Symbol call
      args: Array{Any}((2,))
        1: Symbol alma
        2: Symbol c
    2: Expr
      head: Symbol block
      args: Array{Any}((8,))
        1: LineNumberNode
          line: Int64 2
          file: Symbol none
        2: Expr
          head: Symbol =
          args: Array{Any}((2,))
            1: Expr
              head: Symbol call
              args: Array{Any}((3,))
                1: Symbol f
                2: Symbol x
                3: Symbol y
            2: Expr
              head: Symbol block
              args: Array{Any}((2,))
                1: LineNumberNode
                2: Expr
        3: LineNumberNode
          line: Int64 3
          file: Symbol none
        4: Expr
          head: Symbol =
          args: Array{Any}((2,))
            1: Symbol A
            2: Expr
              head: Symbol call
              args: Array{Any}((3,))
                1: Symbol *
                2: Expr
                3: Symbol c
        5: LineNumberNode
          line: Int64 4
          file: Symbol none
        6: Expr
          head: Symbol =
          args: Array{Any}((2,))
            1: Symbol B
            2: Expr
              head: Symbol call
              args: Array{Any}((3,))
                1: Symbol .+
                2: Expr
                3: Symbol c
        7: LineNumberNode
          line: Int64 5
          file: Symbol none
        8: Expr
          head: Symbol return
          args: Array{Any}((1,))
            1: Expr
              head: Symbol call
              args: Array{Any}((3,))
                1: Symbol /
                2: Expr
                3: Symbol c

```

The return expression is wrong. Is this a bug or it should put this out?

Regards,  
RUn

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [April 5, 2021, 3:51pm UTC](https://discourse.julialang.org/t/ast-example-in-julia/58625/2 "2021-04-05T15:51:55Z")

</div>

Please, read [dump documentation](https://docs.julialang.org/en/v1/base/io-network/#Base.dump) that says:

> dump(x; maxdepth=8)
> 
> Show every part of the representation of a value. The depth of the output is  
> truncated at maxdepth.

You are seeing empty `Expr`s because dump has truncated the output.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 5, 2021, 4:05pm UTC](https://discourse.julialang.org/t/ast-example-in-julia/58625/3 "2021-04-05T16:05:54Z")

</div>

You can obtain the AST in an easy way by constructing a quoted expression

```julia
julia> ex = quote
           function alma(c)
               f(x,y) = 3x + 4y
               A = Int8[1, 2, 3] * c
               B = [4.0, 5.0, 6.0] .+ c
               return f(A,B)/c
           end
       end
quote
    #= REPL[6]:2 =#
    function alma(c)
        #= REPL[6]:2 =#
        #= REPL[6]:3 =#
        f(x, y) = begin
                #= REPL[6]:3 =#
                3x + 4y
            end
        #= REPL[6]:4 =#
        A = Int8[1, 2, 3] * c
        #= REPL[6]:5 =#
        B = [4.0, 5.0, 6.0] .+ c
        #= REPL[6]:6 =#
        return f(A, B) / c
    end
end

julia> dump(ex)
Expr
  head: Symbol block
  args: Array{Any}((2,))
    1: LineNumberNode
      line: Int64 2
      file: Symbol REPL[6]
    2: Expr
.
.
.

```
