Sal*_*apa 1 metaprogramming abstract-syntax-tree expression-trees julia
我正在编写一个基于Python的列表推导的宏@vcomp(矢量理解)和一个条件子句,以简洁的方式过滤元素.
macro vcomp(comprehension::Expr, when::Symbol, condition)
comp_head, comp_args = comprehension.head, comprehension.args
comp_head ? [:comprehension, :typed_comprehension] && error("@vcomp not a comprehension")
when ? :when && error("@vcomp expected `when`, got: `$when`")
T = comp_head == :typed_comprehension ? comp_args[1] : nothing
if VERSION < v"0.5-"
element = comp_head == :comprehension ? comp_args[1] : comp_args[2]
sequence = comp_head == :comprehension ? comp_args[2] : comp_args[3]
else
element = comp_head == :comprehension ? comp_args[1].args[1] : comp_args[2].args[1]
sequence = comp_head == :comprehension ? comp_args[1].args[2] : comp_args[2].args[2]
end
result = T ? nothing ? :($T[]) : :([])
block = Expr(:let, Expr(:block,
Expr(:(=), :res, result),
Expr(:for, sequence,
Expr(:if, condition,
Expr(:call, :push!, :res, element))),
:res))
return esc(block)
end
Run Code Online (Sandbox Code Playgroud)
像这样使用:
julia> @vcomp Int[i^3 for i in 1:10] when i % 2 == 0
5-element Array{Int64,1}:
8
64
216
512
1000
Run Code Online (Sandbox Code Playgroud)
其中扩展到:
julia> macroexpand(:(@vcomp Int[i^3 for i in 1:15] when i % 2 == 0))
:(let
res = Int[]
for i = 1:15
if i % 2 == 0
push!(res,i ^ 3)
end
end
res
end)
Run Code Online (Sandbox Code Playgroud)
我原以为能够block像这样写:
block = quote
let
res = $result
for $sequence
if $condition
push!(res, $element)
end
end
res
end
end
Run Code Online (Sandbox Code Playgroud)
这给出了以下错误:
ERROR: syntax: invalid iteration specification而不是我提出的方式:
block = Expr(:let, Expr(:block,
Expr(:(=), :res, result),
Expr(:for, sequence,
Expr(:if, condition,
Expr(:call, :push!, :res, element))),
:res))
Run Code Online (Sandbox Code Playgroud)
但是我能够Expr(:for, ...)直接使用如上所示,据我所知,这是一个解析器错误(这是一个错误?).我也无法找到这种插值的例子,这是我尝试过的:
julia> ex? = :(i in 1:10)
:($(Expr(:in, :i, :(1:10))))
julia> ex? = :(i = 1:10)
:(i = 1:10)
julia> quote
for $ex?
ERROR: syntax: invalid iteration specification
julia> quote
for $ex?
ERROR: syntax: invalid iteration specification
Run Code Online (Sandbox Code Playgroud)
构造整个表达式并检查它:
julia> ex? = quote
for i in 1:10
print(i)
end
end
quote # none, line 2:
for i = 1:10 # none, line 3:
print(i)
end
end
julia> ex?.args
2-element Array{Any,1}:
:( # none, line 2:)
:(for i = 1:10 # none, line 3:
print(i)
end)
julia> ex?.args[2].args
2-element Array{Any,1}:
:(i = 1:10)
quote # none, line 3:
print(i)
end
julia> ex?.args[2].args[1]
:(i = 1:10)
julia> ex?.args[2].args[1] == ex? # what's the difference then?
true
Run Code Online (Sandbox Code Playgroud)
这有效,但不太可读:
julia> ex? = Expr(:for, ex?, :(print(i)))
:(for $(Expr(:in, :i, :(1:10)))
print(i)
end)
julia> ex? = Expr(:for, ex?, :(print(i)))
:(for i = 1:10
print(i)
end)
julia> eval(ex?)
12345678910
julia> eval(ex?)
12345678910
julia> eval(ex?)
12345678910
Run Code Online (Sandbox Code Playgroud)
有没有办法可以使用更简洁的语法?我发现当前的实现难以阅读和理由与我期望写的相比.
首先,我相信对守卫的理解是来到朱莉娅(在v0.5?).
回答你的问题:解析器希望能够验证其输入在语法上是否正确,而无需查看插值的实际值.试试例如
x, y = :i, :(1:10)
quote
for $x = $y
end
end
Run Code Online (Sandbox Code Playgroud)
现在,解析器可以识别语法的相关部分.(如果你使用的话,你应该得到相同的AST for $x in $y.)