MetaUtils
MetaUtils.MetaUtils
MetaUtils.expr_indent
MetaUtils.print_subtypes
MetaUtils.show_expr
MetaUtils.show_texpr
MetaUtils.show_tree
MetaUtils.teval
MetaUtils.texpr2expr
MetaUtils.@T
MetaUtils.@show_expr
MetaUtils.@show_sexpr
MetaUtils.@show_texpr
MetaUtils.@show_tree
MetaUtils.@t
MetaUtils.@teval
MetaUtils.MetaUtils
— ModuleMetaUtils
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
MetaUtils.expr_indent
— Constantconst expr_indent = 4
is the default indent of showing expressions.
MetaUtils.print_subtypes
— Methodprint_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
MetaUtils.show_expr
— Methodshow_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))
MetaUtils.show_texpr
— Methodshow_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))
MetaUtils.show_tree
— Methodshow_tree(expr) = AbstractTrees.print_tree(expr)
can be regarded as the function version of @show_tree
.
MetaUtils.teval
— Functionteval(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
MetaUtils.texpr2expr
— Functiontexpr2expr(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))
MetaUtils.@T
— MacroMetaUtils.@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
MetaUtils.@show_expr
— Macro@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
MetaUtils.@show_sexpr
— Macro@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
MetaUtils.@show_texpr
— Macro@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
MetaUtils.@show_tree
— Macro@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
MetaUtils.@t
— MacroMetaUtils.@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
MetaUtils.@teval
— Macro@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