• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

JuliaStaging/JuliaVariables.jl: The implementation of NameResolution.jl for Juli ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

JuliaStaging/JuliaVariables.jl

开源软件地址:

https://github.com/JuliaStaging/JuliaVariables.jl

开源编程语言:

Julia 100.0%

开源软件介绍:

JuliaVariables

Stable Dev Build Status Codecov

About

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  :: Bool
    end
  • 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 begin
               Expr(: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 = 1
           function (a)
               x = 1
           end
       end |>  solve_from_local |> rmlines |> unwrap_scoped
quote
    mut @shared x = 1
    function (a,)
        mut @shared x = 1
    end
end


julia> quote
           x = 1
           function ()
               x = 1
           end
       end |>  solve |> rmlines
:($(Expr(:scoped, (bounds = Var[], freevars = Var[], bound_inits = Symbol[]), quote
    @global x = 1
    function ()
        $(Expr(:scoped, (bounds = Var[@local x], freevars = Var[], bound_inits = Symbol[]), quote
    @local x = 1
end))
    end
end)))


julia> quote
           x = 1
           function ()
               x = 1
           end
       end |>  solve_from_local |> rmlines
:($(Expr(:scoped, (bounds = Var[mut @shared x], freevars = Var[], bound_inits = Symbol[]), quote
    mut @shared x = 1
    function ()
        $(Expr(:scoped, (bounds = Var[], freevars = Var[mut @shared x], bound_inits = Symbol[]), quote
    mut @shared x = 1
end))
    end
end)))

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> quote
          function f(x)
               for i in I, j in J
                   let x = 1, y
                       () -> 2
                   end
               end
               f(x) = 2
          end
       end |> rmlines |> simplify_ex
quote
    function f(x)
        for i = I
            for j = J
                let x = 1
                    let y
                        function ()
                            2
                        end
                    end
                end
            end
        end
        function f(x)
            2
        end
    end
end

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(:for,
    Expr(:block,
        :(i = I),
        :(j = J),
    ),
    Expr(:block, body)
)

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:

mysolve! = solve!  simplify_ex
mysolve_from_local! = solve_from_local!  simplify_ex



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap