10 Commits

Author SHA1 Message Date
e028209adf assert on unrecognized directive
saves four tokens and is more diagnostic.
2023-01-01 14:11:11 -08:00
ce3fc83221 fmt in debugmouse:draw3.
This is literally the reason I implemented fmt in the first place
2023-01-01 13:54:28 -08:00
3e2229be65 use fmt in a commented-out debug logging block 2023-01-01 13:54:28 -08:00
46f1339e19 golf recollide_reanchor
Uses fmt in recollide_reanchor. This has a performance impact in code that is already slow so this might need to be reverted; I think that should be part of a comprehensive optimization pass, however, and making it worse for now as part of a general code cleanup is probably better.
2023-01-01 13:54:28 -08:00
f86e52d3bd tostring golf
this could be golfed further by rewriting the str..= line in terms of fmt, but the performance impact of inserting the extra `fmt` calls and parsing into the tostring recursion seems likely to be a problem.
2023-01-01 13:54:28 -08:00
e6c35dbeda Use fmt on title screen. 2023-01-01 13:54:28 -08:00
3516d2855e fix section before first "%"
oops, need to special-case the first part of the split list.

amusingly, I don't need to special-case zero-length format strings because that will skip the entire loop and output "", which seems correct. (I am also not special-casing zero-length segments because `s[1] == nil` will go into the error handler and that seems fine.
2023-01-01 13:54:28 -08:00
2264349e72 fix double-consume, add %!
I discovered your tostring function for debugging, might as well make it reachable from fmt.  I also realized I forgot to convert to using "p" when I introduced it so I fixed that
2023-01-01 13:54:27 -08:00
93c154f876 fix sprintf and rename to fmt
the "split on %" strategy makes parsing "%%" complicated, so the "actually literally %" placeholder is now "%~" rather than "%%". since thsi resembles a real sprintf even less now I renamed it to "fmt".

also actually closes the `if m == "~"` block. (which was `if m == "%"` before this patch)
2023-01-01 13:54:27 -08:00
3494c48e74 implement fake sprintf
this spends extra tokens to handle "invalid format character" to catch using a % where %% is intended. this is designed to grow with further format chars if needed; I have ideas for not-exactly-POSIX %d, %x, %h, %!, %#, %c, and possibly others but there is absolutely no reason to spend tokens on these things until we need them. (that said, existing debugging output might benefit from some of these other formats, but that debug code is commented out, so maybe nevert.)
2023-01-01 13:54:27 -08:00

View File

@ -38,6 +38,39 @@ function cycle(tbl,period)
return tbl[t()%period*#tbl\period+1] return tbl[t()%period*#tbl\period+1]
end end
-- fake sprintf function
-- %~ for literal "%"
-- %v for param
-- %! for tostring(param)
-- which dumps tables
function fmt(f, ...)
local out, i = "", 0
for s in all(split(f,"%")) do
if i == 0 then
-- before first format directive
out ..= s
i = 1
else
local m = s[1]
if m == "~" then
out ..= "%"
else
local p = select(i,...)
i+=1
if m == "v" then
out ..= p
elseif m == "!" then
out ..= tostring(p)
else
assert(false, tostr(m).."is not a formatting directive")
end
end
out ..=sub(s,2)
end
end
return out
end
mnames={} mnames={}
function names(root) function names(root)
local n=mnames[root] local n=mnames[root]
@ -246,14 +279,12 @@ function kbd:release(i)
end end
function tostring(any) function tostring(any)
if type(any)=="table" then if (type(any)!="table") return tostr(any)
local str = "{ " local str = "{ "
for k,v in pairs(any) do for k,v in pairs(any) do
str=str..tostring(k).."->"..tostring(v).." " str..=tostring(k).."->"..tostring(v).." "
end
return str.."}"
end end
return tostr(any) return str.."}"
end end
-->8 -->8
@ -269,7 +300,7 @@ function title:draw()
print("pyrex",32,73,7) print("pyrex",32,73,7)
print("[nyeogmi]",62,73,7) print("[nyeogmi]",62,73,7)
print("kistaro",32,79,7) print("kistaro",32,79,7)
local lvlstr = "⬅️ "..start_level.." ➡️" local lvlstr = fmt("⬅️ %v ➡️",start_level)
print(lvlstr,50,91,1) print(lvlstr,50,91,1)
print(lvlstr,51,90,blinkcol) print(lvlstr,51,90,blinkcol)
end end
@ -439,8 +470,7 @@ function level:recollide_reanchor()
not self:mcoll(mx1,my0) and not self:mcoll(mx1,my0) and
not self:mcoll(mx1,my1) not self:mcoll(mx1,my1)
) then ) then
local key="GEOM"..mx0..","..my0..","..dx..","..dy anch_new[fmt("GEOM%v,%v,%v,%v",mx0,my0,dx,dy)]= {
anch_new[key]= {
max(mx0,mx1),max(my0,my1),adx=-dx,ady=-dy max(mx0,mx1),max(my0,my1),adx=-dx,ady=-dy
} }
end end
@ -448,10 +478,9 @@ function level:recollide_reanchor()
end end
for _,cr in pairs(self._crates) do for _,cr in pairs(self._crates) do
local key="CRATE"..cr.id..","..dx..","..dy
local mx0,my0=cr.mx,cr.my local mx0,my0=cr.mx,cr.my
local mx1,my1=mx0+dx,my0+dy local mx1,my1=mx0+dx,my0+dy
anch_new[key]={ anch_new[fmt("CRATE%v,%v,%v",cr.id,dx,dy)]={
max(mx0,mx1),max(my0,my1),adx=-dx,ady=-dy max(mx0,mx1),max(my0,my1),adx=-dx,ady=-dy
} }
end end
@ -1059,7 +1088,7 @@ function rope:draw(artificial_px,artificial_py)
if (anch.ady>0) y-=1 if (anch.ady>0) y-=1
end end
rectfill(x-1,y-1,x+1,y+1,12) rectfill(x-1,y-1,x+1,y+1,12)
print("ax="..n1.ax..",ay="..n1.ay,72,sy) print(fmt("ax=%v,ay=%v",n1.ax,n1.ay),72,sy)
sy+=7 sy+=7
local n0=n1.prev local n0=n1.prev
@ -1834,7 +1863,7 @@ function debugmouse:draw3()
if (c == " ") spr(50,x,y) if (c == " ") spr(50,x,y)
print(c,x,y,15) print(c,x,y,15)
local px, py = mid(0,x,89), mid(0, y > 111 and y - 12 or y + 6, 117) local px, py = mid(0,x,89), mid(0, y > 111 and y - 12 or y + 6, 117)
print("("..x..", "..y..")\n["..(x\8)..", "..(y\8).."]",px,py,15) print(fmt("(%v, %v)\n[%v, %v]",x,y,x\8,y\8),px,py,15)
pal() pal()
end end