MetaUtils

MetaUtils.MetaUtilsModule
MetaUtils

contains utilities for metaprogramming in Julia.

export @show_sexpr, 
    show_tree, @show_tree, 
    print_subtypes, 
    show_expr, @show_expr, 
    show_texpr, @show_texpr, 
    texpr2expr, teval, @teval
source
MetaUtils.print_subtypesMethod
print_subtypes(T::Type; kwargs...)
print_subtypes(io::IO, T::Type; kwargs...)
print_subtypes(f, io::IO, T::Type; kwargs...)

prints the subtypes of T by AbstractTrees.print_tree.

Example

julia> print_subtypes(AbstractRange)
AbstractRange
├─ LinRange
├─ OrdinalRange
│  ├─ AbstractUnitRange
│  │  ├─ Base.IdentityUnitRange
│  │  ├─ Base.OneTo
│  │  ├─ Base.Slice
│  │  └─ UnitRange
│  └─ StepRange
└─ StepRangeLen
source
MetaUtils.show_exprMethod
show_expr([io::IO,], ex)

shows expression ex as a Julia style expression.

Examples

julia> show_expr(:(f(x, g(y, z))))
Expr(:call, :f, :x, 
    Expr(:call, :g, :y, :z))
source
MetaUtils.show_texprMethod
show_texpr([io::IO,], ex)

Yet another Meta.show_sexpr. It shows expression ex as a lisp style expression.

Remark: The indentation is different from Meta.show_sexpr.

Examples

julia> show_texpr(:(f(x, g(y, z))))
Expr(:call, :f, :x, 
    Expr(:call, :g, :y, :z))
source
MetaUtils.show_treeMethod
show_tree(expr) = AbstractTrees.print_tree(expr)

can be regarded as the function version of @show_tree.

source
MetaUtils.tevalFunction
teval(texpr, m::Module=Main)

evaluates the lisp-like tuple expression texpr.

Example: Calculation of sin(π/6)

julia> (:call, :sin, (:call, :/, π, 6)) |> teval
0.49999999999999994

In some cases, you can omit :call.

julia> (:sin, (:/, π, 6)) |> teval
0.49999999999999994
source
MetaUtils.texpr2exprFunction
texpr2expr(x, m::Module=Main)

converts a lisp-like tuple expression x to the executable expression of Julia.

Example: The expression of sin(π/6)

julia> texpr2expr((:call, :sin, (:call, :/, π, 6)))
:(sin(π / 6))
source
MetaUtils.@TMacro
MetaUtils.@T texpr

shows show(texpr), show_texpr(texpr), the Julia expression, and the value of the lisp-like tuple expression texpr.

Example:

julia> MetaUtils.@T (:call, :sin, (:call, :/, π, 6))
(:call, :sin, (:call, :/, π, 6))
→ (:call, :sin, 
    (:call, :/, π, 6))
→ :(sin(π / 6))
→ 0.49999999999999994
source
MetaUtils.@show_exprMacro
@show_expr(expr, linenums=false)

shows the Juia style expression of expr and prints the line number nodes if linenums is true. This is the macro version of show_expr.

Example

julia> @show_expr 2x+1
Expr(:call, :+, 
    Expr(:call, :*, 2, :x), 1)

eval function can evaluate the output of @show_expr.

julia> x = 10; Expr(:call, :+, 
    Expr(:call, :*, 2, :x), 1) |> eval
21
source
MetaUtils.@show_sexprMacro
@show_sexpr(expr, linenums=false)

shows the lisp style S-expression of expr and prints the line number nodes if linenums is true. This is the macro version of Meta.show_sexpr.

Example

julia> @show_sexpr 2x+1
(:call, :+, (:call, :*, 2, :x), 1)

teval function can evaluate the output of @show_sexpr.

julia> x = 10; (:call, :+, (:call, :*, 2, :x), 1) |> teval
21
source
MetaUtils.@show_texprMacro
@show_texpr(expr, linenums=false)

Yet another @show_sexpr. It shows the lisp style S-expression of expr and prints the line number nodes if linenums is true.

Remark: The indentation is different from @show_sexpr.

Example

julia> @show_texpr 2x+1
(:call, :+, 
    (:call, :*, 2, :x), 1)

teval function can evaluate the output of @show_texpr.

julia> x = 10; (:call, :+, 
    (:call, :*, 2, :x), 1) |> teval
21
source
MetaUtils.@show_treeMacro
@show_tree(expr, maxdepth=10, linenums=false)

shows the tree form of the expression expr with maxdepth and prints the line number nodes if linenums is true.

Example

julia> @show_tree 2x+1
Expr(:call)
├─ :+
├─ Expr(:call)
│  ├─ :*
│  ├─ 2
│  └─ :x
└─ 1
source
MetaUtils.@tMacro
MetaUtils.@t texpr

shows the Julia expression and the value of the lisp-like tuple expression texpr.

Example:

julia> MetaUtils.@t (:call, :sin, (:call, :/, π, 6))
:(sin(π / 6))
→ 0.49999999999999994
source
MetaUtils.@tevalMacro
@teval texpr

evaluates the lisp-like tuple expression texpr.

Example: Calculation of sin(π/6)

julia> @teval (:call, :sin, (:call, :/, π, 6))
0.49999999999999994

In some cases, you can omit :call.

julia> @teval (:sin, (:/, π, 6))
0.49999999999999994
source