Skip to content

Commit

Permalink
added some tests for Expr formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
xitology committed Mar 30, 2021
1 parent e3cb81e commit ec8874a
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 8 deletions.
204 changes: 204 additions & 0 deletions doc/src/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,207 @@ Parameter `tab` specifies the indentation level.
initialization,
intuition)
=#


## Formatting Julia Code

`pprint()` can format `Expr` objects. A fairly complete subset of Julia syntax
is supported.

ex = quote
module Test
export f
using Dates
import Base: show
abstract type A{T}
end
struct S{T} <: A{T}
x::T
end
const v1 = [1,2,3]
const v2 = Number[1,2,3]
const t1 = (1,)
const t2 = (1,2,3)
const p = 1 => 2
Base.show(Base.stdout)
Base.@show Base.stdout
println("x = $x")
"Compute nothing"
function f(::Number)
return
end
g(y) = y > 0 ? y : -y
h(args...; kw = 0) = (args, kw)
global G
if (x1 - (x2 - x3)) > ((x1 - x2) - x3)
if p1 && p2 || p3 && p4
nothing
elseif (p1 || p2) && (p3 || p4)
nothing
else
nothing
end
elseif (x1 ^ (x2 ^ x3)) <= ((x1 ^ x2) ^ x3) < x4 .+ x5
if !p
nothing
end
end
while x > 0
break
end
for t = 1:10
continue
end
begin
x = 1
y = 2
x + y
end
0 + (x = 1; y = 2; x + y)
let x = 1
y = 2
x + y
end
quote
$x + $y
end
try
error()
catch err
nothing
end
try
error()
finally
nothing
end
try
error()
catch err
nothing
finally
nothing
end
foreach(1:10) do k
println(k)
end
[k for k = 1:10 if isodd(k)]
$(Expr(:fallback, 1, 2, 3))
end
end

pprint(ex)
#=>
quote
module Test

export f

using Dates

import Base: show

abstract type A{T}
end

struct S{T} <: A{T}
x::T
end

const v1 = [1, 2, 3]

const v2 = Number[1, 2, 3]

const t1 = (1,)

const t2 = (1, 2, 3)

const p = 1 => 2

Base.show(Base.stdout)

Base.@show Base.stdout

println("x = $(x)")

"Compute nothing"
function f(::Number)
return nothing
end

g(y) = y > 0 ? y : -y

h(args...; kw = 0) = (args, kw)

global G

if x1 - (x2 - x3) > x1 - x2 - x3
if p1 && p2 || p3 && p4
nothing
elseif (p1 || p2) && (p3 || p4)
nothing
else
nothing
end
elseif x1 ^ x2 ^ x3 <= (x1 ^ x2) ^ x3 < x4 .+ x5
if !(p)
nothing
end
end

while x > 0
break
end

for t = 1:10
continue
end

begin
x = 1
y = 2
x + y
end

0 + (x = 1; y = 2; x + y)

let x = 1
y = 2
x + y
end

quote
$(x) + $(y)
end

try
error()
catch err
nothing
end

try
error()
finally
nothing
end

try
error()
catch err
nothing
finally
nothing
end

foreach(1:10) do k
println(k)
end

[k for k = 1:10 if isodd(k)]

$(Expr(:fallback, 1, 2, 3))

end
end
=#
14 changes: 6 additions & 8 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ function tile_expr(ex::Expr, pr = -1)
elseif @isexpr ex Expr(:module, notbare::Bool, name::Symbol, body)
return tile_expr_module(notbare ? :module : :baremodule, name, body)
elseif @isexpr ex Expr(:export, names...)
return list_layout(prefix = "export ", par = ("", ""),
Layout[tile_expr(name, 0) for name in names])
return tile_expr_export(names)
elseif @isexpr ex Expr(head := :using || :import, Expr(:(:), from, args...))
return tile_expr_import(head, from, args)
elseif @isexpr ex Expr(head := :using || :import, arg)
Expand Down Expand Up @@ -232,7 +231,6 @@ function tile_expr(ex::Expr, pr = -1)
elseif @isexpr ex Expr(:$, arg)
return literal("\$(") * tile_expr(arg) * literal(")")
end
dump(ex)
tile_expr_fallback(ex)
end

Expand Down Expand Up @@ -308,7 +306,7 @@ end

tile_expr_export(args) =
list_layout(prefix = "export ", par = ("", ""),
Layout[tile_expr(arg, 0) for arg in arg])
Layout[tile_expr(arg, 0) for arg in args])

tile_expr_import_path(ex) =
(@isexpr ex Expr(:., args...)) ?
Expand Down Expand Up @@ -416,10 +414,10 @@ function tile_expr_try(body, name, catch_body, finally_body)
end
if finally_body !== nothing
lt = lt / literal("finally")
end
finally_body_lt = tile_exprs(finally_body)
if finally_body_lt !== ZERO
lt = lt / (indent(4) * finally_body_lt)
finally_body_lt = tile_exprs(finally_body)
if finally_body_lt !== ZERO
lt = lt / (indent(4) * finally_body_lt)
end
end
lt = lt / literal("end")
lt
Expand Down

0 comments on commit ec8874a

Please sign in to comment.