Skip to content

Instantly share code, notes, and snippets.

@toivoh
Created November 20, 2012 09:46
Show Gist options
  • Save toivoh/4117023 to your computer and use it in GitHub Desktop.
Save toivoh/4117023 to your computer and use it in GitHub Desktop.
simple lispy AST printer for julia
show_sexpr(ex) = show_sexpr(OUTPUT_STREAM, ex)
show_sexpr(io::IO, ex) = show_sexpr(io, ex, 0)
show_sexpr(io::IO, ex, indent::Int) = show(io, ex)
const sexpr_indent_width = 2
function show_sexpr(io::IO, ex::Expr, indent::Int)
inner = indent + sexpr_indent_width
if (ex.head === :block) inter, post = ("\n"*" "^inner, "\n"*" "^indent)
else inter, post = (" ", "")
end
print(io, '(')
show(io, ex.head)
for arg in ex.args
print(io, inter)
show_sexpr(io, arg, inner)
end
print(io, post, ')')
end
@toivoh
Copy link
Author

toivoh commented Nov 20, 2012

This is meant for viewing the internal structure of an AST.
Example: show_sexpr applied to its own code

julia> show_sexpr(quote
           show_sexpr(ex) = show_sexpr(OUTPUT_STREAM, ex)
           show_sexpr(io::IO, ex) = show_sexpr(io, ex, 0)
           show_sexpr(io::IO, ex, indent::Int) = show(io, ex)

           const sexpr_indent_width = 2

           function show_sexpr(io::IO, ex::Expr, indent::Int)
               inner = indent + sexpr_indent_width
               if (ex.head === :block) inter, post = ("\n"*" "^inner, "\n"*" "^indent)
               else                    inter, post = (" ", "")
               end

               print(io, '(')
               show(io, ex.head)
               for arg in ex.args
                   print(io, inter)
                   show_sexpr(io, arg, inner)
               end
               print(io, post, ')')
           end
       end)
(:block
  (:line 2)
  (:= (:call :show_sexpr :ex) (:block
      (:line 2 :none)
      (:call :show_sexpr :OUTPUT_STREAM :ex)
    ))
  (:line 3)
  (:= (:call :show_sexpr (::: :io :IO) :ex) (:block
      (:line 3 :none)
      (:call :show_sexpr :io :ex 0)
    ))
  (:line 4)
  (:= (:call :show_sexpr (::: :io :IO) :ex (::: :indent :Int)) (:block
      (:line 4 :none)
      (:call :show :io :ex)
    ))
  (:line 6)
  (:const (:= :sexpr_indent_width 2))
  (:line 8)
  (:function (:call :show_sexpr (::: :io :IO) (::: :ex :Expr) (::: :indent :Int)) (:block
      (:line 9 :none)
      (:= :inner (:call :+ :indent :sexpr_indent_width))
      (:line 10)
      (:if (:comparison (:. :ex (:quote :head)) :=== (:quote :block)) (:block
          (:line 10)
          (:= (:tuple :inter :post) (:tuple (:call :* "\n" (:call :^ " " :inner)) (:call :* "\n" (:call :^ " " :indent))))
        ) (:block
          (:line 11)
          (:= (:tuple :inter :post) (:tuple " " ""))
        ))
      (:line 14)
      (:call :print :io '(')
      (:line 15)
      (:call :show :io (:. :ex (:quote :head)))
      (:line 16)
      (:for (:= :arg (:. :ex (:quote :args))) (:block
          (:line 17)
          (:call :print :io :inter)
          (:line 18)
          (:call :show_sexpr :io :arg :inner)
        ))
      (:line 20)
      (:call :print :io :post ')')
    ))
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment