c42f / JuliaLowering.jl

Julia code lowering with precise provenance
MIT License
52 stars 2 forks source link

Renamig by color? #2

Closed Krastanov closed 2 months ago

Krastanov commented 2 months ago

This is a very exciting package!

What would be the proper way to take code like this (with potentially even more nested scopes)

a = 1
b = 2
for a in [b]
    println(a)
end
let b = a
    println(b)
end

and use JuliaSyntax/JuliaLowering to transform it into something like

a = 1
b = 2
for a_scope1 in [b]
    println(a_scope1)
end
let b_scope2 = a
    println(b_scope2)
end

This is a usability question, please feel free to close/redirect if this is the wrong venue to ask.

Krastanov commented 2 months ago

This was just mentioned on slack. For now the functions too look up are parsestmt, annotate_scopes, and formatsrc

image

c42f commented 2 months ago

Yea, I just posted that over there. Glad you find this work exciting :)

JuliaLowering adds a var_id to each identifier in the resolve_scopes pass - a unique ID for each identifier. Currently that ID goes with the var_info map which is attached to the ScopeResolutionContext where you can find out more information about the variable.

annotate_scopes and formatsrc are currently just hacky tools in test/demo.jl. As for renaming, the extremely new JuliaSyntaxFormatter package could do this if you create a custom format_token_str function and pass it to format().

Do feel free to play around with these things, just be aware that none of this is near "ready for production use" - JuliaLowering doesn't even support most Julia syntax yet!