Skip to content

Commit

Permalink
whoops - fixed rename hint
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismypassport committed Oct 6, 2023
1 parent 727b383 commit 3b6ea85
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 31 deletions.
29 changes: 20 additions & 9 deletions pico_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def check_safety(node):
global_excludes = global_strings_cpy
member_excludes = member_strings.copy()
local_excludes = defaultdict(set)
label_excludes = defaultdict(set)

globals_after_zero = set()
members_after_zero = set()
Expand All @@ -212,6 +213,14 @@ def compute_effective_kind(node, kind, explicit):
if node.var.rename:
node.name = node.var.rename
renamed_vars.add(node.var)
if kind == VarKind.member:
member_excludes.add(node.name)
elif kind == VarKind.global_:
global_excludes.add(node.name)
elif kind == VarKind.local:
local_excludes[node.name].add(node.var)
elif kind == VarKind.label:
label_excludes[node.name].add(node.var)
return None

if kind == VarKind.member:
Expand Down Expand Up @@ -420,15 +429,15 @@ def select_vars(remaining, excluded, renames, avoids, ident):
if ident.startswith(ch):
avoid_locals, avoid_globals, avoid_members = locals_after_zero, globals_after_zero, members_after_zero

excluded = []
if ident in global_excludes:
excluded.append(root.globals[ident])
if ident in member_excludes:
excluded.append(root.members[ident])
if ident in local_excludes:
excluded.extend(local_excludes[ident])

if ident != "_ENV":
excluded = []
if ident in global_excludes:
excluded.append(root.globals[ident])
if ident in member_excludes:
excluded.append(root.members[ident])
if ident in local_excludes:
excluded.extend(local_excludes[ident])

if not focus.chars: # going over locals first seems to usually increase compression (TODO...)
select_vars(remaining_locals, excluded, local_renames, avoid_locals, ident)
select_var(remaining_globals, excluded, global_renames, avoid_globals, ident, root.globals)
Expand All @@ -438,7 +447,9 @@ def select_vars(remaining, excluded, renames, avoids, ident):
select_var(remaining_members, excluded, member_renames, avoid_members, ident, root.members)
select_vars(remaining_locals, excluded, local_renames, avoid_locals, ident)

select_vars(remaining_labels, excluded, label_renames, (), ident)
if remaining_labels:
excluded = list(label_excludes[ident])
select_vars(remaining_labels, excluded, label_renames, (), ident)

if renamed_vars:
for var1, var2 in itertools.product(renamed_vars, renamed_vars):
Expand Down
5 changes: 5 additions & 0 deletions test_compare/input-reformat.p8
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,9 @@ function old_name(--[[rename::new_name2]]old_param, do_rename_this, do_rename_th
return old_param.--[[rename::new_member]]old_member
end

function ggg(--[[rename::l]]p1, --[[rename::e]]p2, --[[rename::f]]p3, p4, p5, p6)
return p1 + p2 + p3 + p4 + p5 + p6
end

?ggg(1, 2, 4, 8, 16, 32)
done = ?"END!"
15 changes: 10 additions & 5 deletions test_compare/input-un.p8
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __lua__

print = printh
?"hello ᶜ7there♥ら"
🐱, i, d, r, h, s, e, e = 11, 12, 13, 14, 15, 16, 17, 17
🐱, i, r, h, u, s, e, e = 11, 12, 13, 14, 15, 16, 17, 17
t(stat(band()))
-- this one comment, i do want!
t()
Expand Down Expand Up @@ -40,7 +40,7 @@ local e, l = split "e,l,i,123", {e = 123, l = 234, i = 345}
local e = "o"
o = 123
?_ENV[e]
local e = "e:l#~~i,", "!t$h+123-s\nif\nu"
local e = "e:l#~~i,", "!t$h+123-u\nif\ns"
do
local _ENV = {assert = assert}
assert(true)
Expand Down Expand Up @@ -120,7 +120,7 @@ e += 1
l, n = sin(1, 2), cos((cos()))
o, f = (cos((cos())))

function u()
function x()
return 1, 2, ord "1", (ord "1")
end

Expand Down Expand Up @@ -156,7 +156,7 @@ end
j = "renaming bug"

function a()
local e, l, n, o, f, i, c, a, d, r, t, h, s, u, x, k, y, v, p, b, w, g, _, m, E, N, D
local e, l, n, o, f, i, c, a, d, r, t, h, u, s, x, k, y, v, p, b, w, g, _, m, E, N, D
return j
end

Expand All @@ -173,4 +173,9 @@ function new_name(new_name2, e, l)
return new_name2.new_member
end

x = ?"END!"
function d(l, e, f, n, o, i)
return l + e + f + n + o + i
end

?d(1, 2, 4, 8, 16, 32)
k = ?"END!"
11 changes: 6 additions & 5 deletions test_compare/output.p8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
print=printh?"hello ᶜ7there♥ら"
🐱,i,d,r,h,s,e,e=11,12,13,14,15,16,17,17t(stat(band()))-- this one comment, i do want!
🐱,i,r,h,u,s,e,e=11,12,13,14,15,16,17,17t(stat(band()))-- this one comment, i do want!
t()c=0l=0l=0print"this is included"?"#[disable[[this for now/ever]]]"
local e={1,2,3}print(#e)print(#[[#include notaninclude
]])local e,l="preserved_key",{preserved_key=123}?l[e]
Expand All @@ -12,7 +12,7 @@ function e.subfunc()end function e:subfunc()end?e:subfunc()
local e,l="c",{c=123}?l[e]
local e,l=split"e,l,i,123",{e=123,l=234,i=345}?l[e[2]]
local e="o"o=123?_ENV[e]
local e="e:l#~~i,","!t$h+123-s\nif\nu"do local _ENV={assert=assert}assert(true)end for _ENV in all{{o=1},{o=2}}do o+=1end function some_future_pico8_api()end some_future_pico8_api(1,2,3)local e={preserved1=1,preserved2=2}e.preserved1+=1?e["preserved1"]
local e="e:l#~~i,","!t$h+123-u\nif\ns"do local _ENV={assert=assert}assert(true)end for _ENV in all{{o=1},{o=2}}do o+=1end function some_future_pico8_api()end some_future_pico8_api(1,2,3)local e={preserved1=1,preserved2=2}e.preserved1+=1?e["preserved1"]
e=setmetatable({preserved3=3},f)?e["preserved3"]
n={preserved1=1,preserved2=2}n.preserved1+=1?n["preserved1"]
n=setmetatable({preserved3=3},f)?n["preserved3"]
Expand All @@ -27,8 +27,9 @@ local e={1},{1,2,3,4}local e,l=1~=2,1,1.2345,4660,4660.33777,-1,-1.2345,-4660.33
]]]=]]===]]==]local l=-256,-256*4,65280^4,-65280,~65280if(not e)e=-1
?1or 1or 2and 3==4>=4|5~6<<1>><1 ..2 ..3- -1^4^1/1&7
?((~(((((((tonum(((3or 4)and 5)~=2)|1)~2)&3)>>1)..1)-(1+3))*3))^2)^1
local e=({})[1],(function()end)()local l,n,o,e,f=sin(1,2),cos((cos())),(cos((cos()))),{d=ord,r=pal}local e=ord"123",pal{1,2},e:d("ord"),e:r({1,2}),sin(1)local i={ord"1",[2]=3,o=4,(ord"1")}e+=1l,n=sin(1,2),cos((cos()))o,f=(cos((cos())))function u()return 1,2,ord"1",(ord"1")end if 1==2then elseif 1==2then else end while 1==2do end repeat until 1==1for e in(all{})do end print("test"..@16 .."str")if(true)?"sh1"
local e=({})[1],(function()end)()local l,n,o,e,f=sin(1,2),cos((cos())),(cos((cos()))),{d=ord,r=pal}local e=ord"123",pal{1,2},e:d("ord"),e:r({1,2}),sin(1)local i={ord"1",[2]=3,o=4,(ord"1")}e+=1l,n=sin(1,2),cos((cos()))o,f=(cos((cos())))function x()return 1,2,ord"1",(ord"1")end if 1==2then elseif 1==2then else end while 1==2do end repeat until 1==1for e in(all{})do end print("test"..@16 .."str")if(true)?"sh1"
if true then?"sh2"
end if(true)if false then else print"sh3"end
if true then if false then else print"sh4"end end j="renaming bug"function a()local e,l,n,o,f,i,c,a,d,r,t,h,s,u,x,k,y,v,p,b,w,g,_,m,E,N,D return j end?a()
c=0c=1function new_name(new_name,e)return new_name.new_member,e.new_member end function new_name(new_name2,e,l)local e,l return new_name2.new_member end x=?"END!"
if true then if false then else print"sh4"end end j="renaming bug"function a()local e,l,n,o,f,i,c,a,d,r,t,h,u,s,x,k,y,v,p,b,w,g,_,m,E,N,D return j end?a()
c=0c=1function new_name(new_name,e)return new_name.new_member,e.new_member end function new_name(new_name2,e,l)local e,l return new_name2.new_member end function d(l,e,f,n,o,i)return l+e+f+n+o+i end?d(1,2,4,8,16,32)
k=?"END!"
1 change: 1 addition & 0 deletions test_compare/output.p8.printh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ sh2
sh3
sh4
renaming bug
63
END!
4 changes: 4 additions & 0 deletions test_compare/output_min.p8
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ function old_name(old_param,do_rename_this,do_rename_that)
local more_things_to_rename,and_so_on
return old_param.old_member
end
function ggg(p1,p2,p3,p4,p5,p6)
return p1+p2+p3+p4+p5+p6
end
?ggg(1,2,4,8,16,32)
done=?"END!"
5 changes: 3 additions & 2 deletions test_compare/output_minrename-ih.p8
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ local e={1},{1,2,3,4}local e,n=1~=2,1,1.2345,4660,4660.33777,-1,-1.2345,-4660.33
local e=({})[1],(function()end)()local n,o,f,e,i=sin(1,2),cos((cos())),(cos((cos()))),{ord=ord,pal=pal}local e=ord"123",pal{1,2},e:ord("ord"),e:pal({1,2}),sin(1)local c={ord"1",[2]=3,x=4,(ord"1")}e+=1n,o=sin(1,2),cos((cos()))f,i=(cos((cos())))function xxx()return 1,2,ord"1",(ord"1")end if 1==2then elseif 1==2then else end while 1==2do end repeat until 1==1for e in(all{})do end print("test"..@16 .."str")if(true)?"sh1"
if true then?"sh2"
end if(true)if false then else print"sh3"end
if true then if false then else print"sh4"end end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,s,u,x,k,y,v,p,b,w,g,_,m,E,N,D,j return l end?fff()
x=0 x=1function old_name(e,l)return e.old_member,l.old_member end function old_name(e,l,n)local l,n return e.old_member end done=?"END!"
if true then if false then else print"sh4"end end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,u,s,x,k,y,v,p,b,w,g,_,m,E,N,D,j return l end?fff()
x=0 x=1function old_name(e,l)return e.old_member,l.old_member end function old_name(e,l,n)local l,n return e.old_member end function ggg(e,l,n,o,f,i)return e+l+n+o+f+i end?ggg(1,2,4,8,16,32)
done=?"END!"
5 changes: 3 additions & 2 deletions test_compare/output_minrename-ob.p8
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ local e={assert=assert,add=add}do local _ENV=e assert(add({},1)==1)end do local
?((~(((((((tonum(((3or 4)and 5)~=2)|1)~2)&3)>>1)..1)-(1+3))*3))^2)^1
local e=({})[1],(function()end)()local n,o,f,e,i=sin(1,2),cos((cos())),(cos((cos()))),{ord=ord,pal=pal}local e=ord"123",pal{1,2},e:ord("ord"),e:pal({1,2}),sin(1)local c={ord"1",[2]=3,x=4,(ord"1")}e+=1n,o=sin(1,2),cos((cos()))f,i=(cos((cos())))function xxx()return 1,2,ord"1",(ord"1")end if 1==2then elseif 1==2then else end while 1==2do end repeat until 1==1for e in(all{})do end print("test"..@16 .."str")if true then?"sh1"
end if true then?"sh2"
end if true then if false then else print"sh3"end end if true then if false then else print"sh4"end end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,s,u,x,k,y,v,p,b,w,g,_,m,j,q,z,e1 return l end?fff()
x=0 x=1function old_name(e,l)return e.old_member,l.old_member end function old_name(e,l,n)local l,n return e.old_member end done=?"END!"
end if true then if false then else print"sh3"end end if true then if false then else print"sh4"end end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,u,s,x,k,y,v,p,b,w,g,_,m,j,q,z,e1 return l end?fff()
x=0 x=1function old_name(e,l)return e.old_member,l.old_member end function old_name(e,l,n)local l,n return e.old_member end function ggg(e,l,n,o,f,i)return e+l+n+o+f+i end?ggg(1,2,4,8,16,32)
done=?"END!"
5 changes: 3 additions & 2 deletions test_compare/output_minrename-oc.p8
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ repeat until 1==1for e in(all{})do end print("test"..@16 .."str")if(true)?"sh1"
if true then?"sh2"
end if(true)if false then else print"sh3"end
if true then if(false);else print"sh4"
end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,s,u,x,k,y,v,p,♥,b,w,g,_,ら,m,E,N return l end?fff()
x=0 x=1function old_name(e,l)return e.old_member,l.old_member end function old_name(e,l,n)local l,n return e.old_member end done=?"END!"
end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,u,s,x,k,y,v,p,♥,b,w,g,_,ら,m,E,N return l end?fff()
x=0 x=1function old_name(e,l)return e.old_member,l.old_member end function old_name(e,l,n)local l,n return e.old_member end function ggg(e,l,n,o,f,i)return e+l+n+o+f+i end?ggg(1,2,4,8,16,32)
done=?"END!"
5 changes: 3 additions & 2 deletions test_compare/output_minrename.p8
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ local e={1},{1,2,3,4}local e,n=1~=2,1,1.2345,4660,4660.33777,-1,-1.2345,-4660.33
local e=({})[1],(function()end)()local n,o,f,e,i=sin(1,2),cos((cos())),(cos((cos()))),{ord=ord,pal=pal}local e=ord"123",pal{1,2},e:ord("ord"),e:pal({1,2}),sin(1)local c={ord"1",[2]=3,x=4,(ord"1")}e+=1n,o=sin(1,2),cos((cos()))f,i=(cos((cos())))function xxx()return 1,2,ord"1",(ord"1")end if 1==2then elseif 1==2then else end while 1==2do end repeat until 1==1for e in(all{})do end print("test"..@16 .."str")if(true)?"sh1"
if true then?"sh2"
end if(true)if false then else print"sh3"end
if true then if false then else print"sh4"end end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,s,u,x,k,y,v,p,b,w,g,_,m,E,N,D,j return l end?fff()
x=0 x=1function new_name(new_name,e)return new_name.new_member,e.new_member end function new_name(new_name2,e,l)local e,l return new_name2.new_member end done=?"END!"
if true then if false then else print"sh4"end end l="renaming bug"function fff()local e,n,o,f,i,c,a,d,r,t,h,u,s,x,k,y,v,p,b,w,g,_,m,E,N,D,j return l end?fff()
x=0 x=1function new_name(new_name,e)return new_name.new_member,e.new_member end function new_name(new_name2,e,l)local e,l return new_name2.new_member end function ggg(l,e,f,n,o,i)return l+e+f+n+o+i end?ggg(1,2,4,8,16,32)
done=?"END!"
12 changes: 8 additions & 4 deletions test_compare/output_semiob.p8
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version 41
__lua__
print=printh
?"hello ᶜ7there♥ら"
🐱,i,d,r,h,s,e,e=11,12,13,14,15,16,17,17
🐱,i,r,h,u,s,e,e=11,12,13,14,15,16,17,17

t(stat(band()))

Expand Down Expand Up @@ -120,7 +120,7 @@ local i = {ord"1",[2]=3,x=4,(ord"1")}
e += 1
l, n = sin(1,2), cos((cos()))
o, f = (cos((cos())))
function u() return 1, 2, ord"1", (ord"1") end
function x() return 1, 2, ord"1", (ord"1") end
if 1 == 2 then elseif 1 == 2 then else end
while 1 == 2 do end
repeat until 1 == 1
Expand All @@ -135,7 +135,7 @@ if true then if false then else print"sh4" end end

j="renaming bug"
function a()
local e,l,n,o,f,i,c,a,d,r,t,h,s,u,x,k,y,v,p,b,w,g,_,m,E,N,D
local e,l,n,o,f,i,c,a,d,r,t,h,u,s,x,k,y,v,p,b,w,g,_,m,E,N,D
return j
end
?a()
Expand All @@ -148,5 +148,9 @@ function new_name( new_name2, e, l)
local e, l
return new_name2. new_member
end
function d( l, e, f, n, o, i)
return l+e+f+n+o+i
end
?d(1,2,4,8,16,32)

x=?"END!"
k=?"END!"
4 changes: 4 additions & 0 deletions test_compare/output_tokens.p8
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,9 @@ function old_name(--[[rename::new_name2]]old_param, do_rename_this, do_rename_th
local more_things_to_rename, and_so_on
return old_param.--[[rename::new_member]]old_member
end
function ggg(--[[rename::l]]p1, --[[rename::e]]p2, --[[rename::f]]p3, p4, p5, p6)
return p1+p2+p3+p4+p5+p6
end
?ggg(1,2,4,8,16,32)

done=?"END!"
4 changes: 4 additions & 0 deletions test_input/input.p8
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,9 @@ function old_name(--[[rename::new_name2]]old_param, do_rename_this, do_rename_th
local more_things_to_rename, and_so_on
return old_param.--[[rename::new_member]]old_member
end
function ggg(--[[rename::l]]p1, --[[rename::e]]p2, --[[rename::f]]p3, p4, p5, p6)
return p1+p2+p3+p4+p5+p6
end
?ggg(1,2,4,8,16,32)

done=?"END!"

0 comments on commit 3b6ea85

Please sign in to comment.