The solve function will solve the scopes of a simplified Julia expression.
The variables(Symbol) are transformed to Var:
struct Var
name ::Symbol
is_mutable ::Bool
is_shared ::Bool
is_global ::Boolend
Some expressions will be wrapped within Expr(:scoped, (bounds=..., freevars=..., bound_inits=...), inner_expression).
Example
solve & solve_from_local
julia>using MLStyle
julia>unwrap_scoped(ex) =@match ex beginExpr(:scoped, _, a) =>unwrap_scoped(a)
Expr(head, args...) =>Expr(head, map(unwrap_scoped, args)...)
a => a
end
unwrap_scoped (generic function with 1 method)
julia>quote
x =1function (a)
x =1endend|> solve_from_local |> rmlines |> unwrap_scoped
quote
mut @shared x =1function (a,)
mut @shared x =1endend
julia>quote
x =1function ()
x =1endend|> solve |> rmlines
:($(Expr(:scoped, (bounds = Var[], freevars = Var[], bound_inits = Symbol[]), quote@global x =1function ()
$(Expr(:scoped, (bounds = Var[@local x], freevars = Var[], bound_inits = Symbol[]), quote@local x =1end))
endend)))
julia>quote
x =1function ()
x =1endend|> solve_from_local |> rmlines
:($(Expr(:scoped, (bounds = Var[mut @shared x], freevars = Var[], bound_inits = Symbol[]), quote
mut @shared x =1function ()
$(Expr(:scoped, (bounds = Var[], freevars = Var[mut @shared x], bound_inits = Symbol[]), quote
mut @shared x =1end))
endend)))
simplify_ex
Not all expressions can be accepted as the input of solve or solve_from_local, thus we provide such a
handy API to apply conversions from almost arbitrary
expressions to the simplified expressions.
julia>quotefunctionf(x)
for i in I, j in J
let x =1, y
() ->2endendf(x) =2endend|> rmlines |> simplify_ex
quotefunctionf(x)
for i = I
for j = J
let x =1let y
function ()
2endendendendendfunctionf(x)
2endendend
The reason why we don't couple this API with solve is, we need to let user aware that there exists destructive operations for expressing the scope information, for instance, it's impossible to inject
scope information to for i in I, j in J; body end, because
the AST shape of it is
Expr(:block, body) is actually in the sub-scope of
that of :(j = J), and :(j=J)'s scope in inherited from that of :(i=I), which ruins the handy use(especially the top-down tree visiting) of scoped expressions.
Not only due to the uselessness of scoping the messy ASTs like for i in I, j in J; body end, the analyses for them are also much more ugly to implement than those of the simplified expressions. Finally, I give up doing this.
If you have understood the above concerns and made
sure it's safe to return a restructured expression after injecting scope information, you can compose
simplify_ex and solve to gain a more handy API:
请发表评论