Skip to content

Commit

Permalink
avoid bxBX after 0 when renaming (fixes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismypassport committed Sep 23, 2023
1 parent 014c9ca commit dc9d213
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
52 changes: 40 additions & 12 deletions pico_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def check_safety(node):
member_excludes = member_strings.copy()
local_excludes = defaultdict(list)

globals_after_zero = set()
members_after_zero = set()
locals_after_zero = set()

def compute_effective_kind(node, kind, explicit):
"""get the identifier kind (global/member/etc) of a node, taking into account hints in the code"""
if kind == VarKind.member:
Expand Down Expand Up @@ -251,16 +255,32 @@ def compute_effective_kind(node, kind, explicit):

return kind

def is_after_zero(node):
if not node.is_extra_child():
prev = node.prev_token()
# TODO: would be good to rename after minify?
if prev.type == TokenType.number and prev.value.endswith("0"):
return True

def collect_idents_pre(node):
if node.type == NodeType.var:
node.effective_kind = compute_effective_kind(node, default(node.var_kind, node.kind), explicit=e(node.var_kind))

if node.effective_kind == VarKind.member:
member_uses[node.name] += 1
if is_after_zero(node):
members_after_zero.add(node.name)

elif node.effective_kind == VarKind.global_:
global_uses[node.name] += 1
if is_after_zero(node):
globals_after_zero.add(node.name)

elif node.effective_kind == VarKind.local:
local_uses[node.var] += 1
if is_after_zero(node):
locals_after_zero.add(node.var)

elif node.effective_kind == VarKind.label:
label_uses[node.var] += 1

Expand Down Expand Up @@ -356,8 +376,11 @@ def are_vars_compatible(var1, var2):

local_renames, global_renames, member_renames, label_renames = {}, {}, {}, {}

def try_select_var(sel, excluded, renames, ident, var_map=None):
def try_select_var(sel, excluded, renames, avoids, ident, var_map=None):
sel_var = var_map[sel] if var_map else sel

if sel in avoids:
return False

for exclude in excluded:
if not are_vars_compatible(sel_var, exclude):
Expand All @@ -367,22 +390,27 @@ def try_select_var(sel, excluded, renames, ident, var_map=None):
renames[sel] = ident
return True

def select_var(remaining, excluded, renames, ident, var_map=None, i=0):
def select_var(remaining, excluded, renames, avoids, ident, var_map=None, i=0):
while i < len(remaining):
if try_select_var(remaining[i], excluded, renames, ident, var_map):
if try_select_var(remaining[i], excluded, renames, avoids, ident, var_map):
del remaining[i]
return i
i += 1

def select_vars(remaining, excluded, renames, ident):
def select_vars(remaining, excluded, renames, avoids, ident):
i = 0
while i != None:
i = select_var(remaining, excluded, renames, ident, i=i)
i = select_var(remaining, excluded, renames, avoids, ident, i=i)

for ident in get_idents():
if not remaining_locals and not remaining_globals and not remaining_members and not remaining_labels:
break

avoid_locals = avoid_globals = avoid_members = ()
for ch in "bxBX": # these chars cause extra space if placed after 0
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])
Expand All @@ -393,15 +421,15 @@ def select_vars(remaining, excluded, renames, ident):

if ident != "_ENV":
if not focus.chars: # going over locals first seems to usually increase compression (TODO...)
select_vars(remaining_locals, excluded, local_renames, ident)
select_var(remaining_globals, excluded, global_renames, ident, root.globals)
select_var(remaining_members, excluded, member_renames, ident, root.members)
select_vars(remaining_locals, excluded, local_renames, avoid_locals, ident)
select_var(remaining_globals, excluded, global_renames, avoid_globals, ident, root.globals)
select_var(remaining_members, excluded, member_renames, avoid_members, ident, root.members)
else:
select_var(remaining_globals, excluded, global_renames, ident, root.globals)
select_var(remaining_members, excluded, member_renames, ident, root.members)
select_vars(remaining_locals, excluded, local_renames, ident)
select_var(remaining_globals, excluded, global_renames, avoid_globals, ident, root.globals)
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)
select_vars(remaining_labels, excluded, label_renames, (), ident)

# output the identifier mapping, if needed

Expand Down
3 changes: 3 additions & 0 deletions pico_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def add_extra_child(m, child):
child.parent = m
child.extra_i = len(m.extra_children)
m.extra_children.append(child)

def is_extra_child(m):
return hasattr(m, "extra_i")

class TokenType(Enum):
number = string = ident = keyword = punct = ...
Expand Down
2 changes: 1 addition & 1 deletion shrinko8.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pico_tokenize import k_hint_split_re
import argparse

k_version = 'v1.1.1'
k_version = 'v1.1.2'

def SplitBySeps(val):
return k_hint_split_re.split(val)
Expand Down
7 changes: 6 additions & 1 deletion site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
textarea {
resize: none;
}

a { /* even without href */
cursor: pointer;
}
</style>
</head>
<body style="height: 100%; margin: 0;" data-droptarget="#input-code"
Expand Down Expand Up @@ -216,7 +220,8 @@
<button type="button" onclick="saveOutputFile()">Download</button>
</div>
<div style="flex: 1" class="flex-column margin">
<div>Preview:<span style="color: gray"> (To download, click the 'Download' button to the left. Copy/pasting this preview to Pico-8 will not compress as well)</span></div>
<div>Preview:<span style="color: gray"> (Shrinko-8 can compress better than Pico-8 itself, so you're recommended to
<a onclick="saveOutputFile()">download</a> the file instead of copy/pasting)</span></div>
<div id="minify-preview" class="ace-textarea" style="flex: 1; margin-top: 10px" readonly></div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions test_compare/mini.p8
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
function A(n,f,i,o,d,e,u,c,t,r,a,b,g,h,j,k,l,m,p,q,s,v,w,x)
n=0f=0i=0o=0d=0e=0u=0c=0t=0r=0a=0 b=0g=0h=0j=0k=0l=0m=0p=0q=0s=0v=0w=0 x=0
function A(n,f,i,o,d,e,u,c,t,r,a,g,h,j,k,l,m,p,q,s,v,w,y,z)
n=0f=0i=0o=0d=0e=0u=0c=0t=0r=0a=0g=0h=0j=0k=0l=0m=0p=0q=0s=0v=0w=0y=0z=0
end
B=0C=0D=0E=0F=0G=0H=0I=0J=0K=0L=0M=0N=0O=0P=0
a()b()c()d()e()f()g()h()i()j()k()l()m()n()o()p()q()r()s()t()u()v()w()x()y()z()
for A in Q do
if(A)a()b()c()d()e()f()g()h()i()j()k()l()m()n()o()p()q()r()s()t()u()v()w()x()y()z()
end
__meta:title__
ideally, the minifier shouldn't add spaces below (as of writing this it does...)
ideally, the minifier shouldn't add spaces below
2 changes: 1 addition & 1 deletion test_input/mini.p8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__lua__
-- ideally, the minifier shouldn't add spaces below (as of writing this it does...)
-- ideally, the minifier shouldn't add spaces below
function test1(a,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,y,z)
a=0c=0d=0e=0f=0g=0h=0i=0j=0k=0l=0m=0n=0o=0p=0q=0r=0s=0t=0u=0v=0w=0y=0z=0
end
Expand Down

0 comments on commit dc9d213

Please sign in to comment.