Last active
September 15, 2016 22:24
-
-
Save gamax92/e2b02dbddae23b156c8a168681ed44d8 to your computer and use it in GitHub Desktop.
Monitor style debugger for PICO-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do | |
local oenv=_ENV | |
if oenv==nil then | |
print("do not open this code in the\npico-8 editor") | |
return | |
end | |
local indent=0 | |
local type, printh, coresume, cocreate=type, printh, coresume, cocreate | |
local _nvar | |
local function _count(_, ...) | |
if coresume(cocreate(type), ...) then | |
_nvar+=1 | |
return _count(...) | |
end | |
end | |
local function select_n(...) | |
_nvar=0 | |
_count(nil, ...) | |
return _nvar | |
end | |
local _pos, _tbl | |
local function _unpack(...) | |
if _pos<=0 then | |
return ... | |
end | |
_pos-=1 | |
return _unpack(_tbl[_pos+1], ...) | |
end | |
local function unpack(tbl) | |
_pos=tbl.n | |
_tbl=tbl | |
return _unpack() | |
end | |
local function pack(...) | |
return {n=select_n(...), ...} | |
end | |
local function serialize(args) | |
local str="" | |
for i=1,args.n do | |
local arg=args[i] | |
local targ=type(arg) | |
if targ=="string" then | |
str=str.."\""..arg.."\"" | |
elseif targ=="number" then | |
str=str..arg | |
elseif targ=="boolean" then | |
str=str..(arg and "true" or "false") | |
else | |
str=str..targ | |
end | |
if i<args.n then | |
str=str..", " | |
end | |
end | |
return str | |
end | |
local function rep(str, n) | |
if (n==1) return str | |
local nstr="" | |
for i=1,n do | |
nstr=nstr..str | |
end | |
return nstr | |
end | |
_ENV=setmetatable({}, { | |
__index=function(_, k) | |
if type(oenv[k])=="function" then | |
return function(...) | |
printh(rep("\t",indent).."enter "..k.."("..serialize(pack(...))..")") | |
indent+=1 | |
local out=pack(oenv[k](...)) | |
indent-=1 | |
local outs="" | |
if out.n>0 then | |
outs="=("..serialize(out)..")" | |
end | |
printh(rep("\t",indent).."exit "..k..outs) | |
return unpack(out) | |
end | |
else | |
return oenv[k] | |
end | |
end, | |
__newindex=function(_, k, v) | |
oenv[k]=v | |
end}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment