Re: show subscripts in dump - help and ideas requested
- Posted by petelomax May 14, 2015
- 1881 views
Hi Pete,
Would this do the job?
symtab = . [1] 1 . [2] 0 . [3] 3 . [4] 4 . [5] 0 . [6] 0
Erm, I really wanted something a little more compact. Not to worry, my plan, of writing it down, publicly voicing it to force some kind of deadline, then letting my unconscious subconscious mull it over, worked a treat. Woke up and could see partial bits of text and start and current indexes being passed back and forth, and before finishing my first cuppa the second penny had dropped: don't fret about when you print things, instead chuck anything generated as and when you like into a big table along with the right numbers for a plain sort before a final print. Not too pretty, could suffer some cleanup, but here it is:
-- note: only tested on Phix constant MAXLINELEN = 77 sequence printstack -- each element contains 3 items: -- indicii - for sorting, eg {21,1} -- name - eg "symtab[21][1..11]" (matches that {21,1}) -- text - the formatted value procedure addtostack(sequence idii, string name, string text) -- if idii={11} then ?9/0 end if printstack = append(printstack,{idii,name,text}) end procedure function cdi(string name, string prev, integer prst, integer prdx, object o, sequence idii) -- ps: not totally sure I've got return "1,2,3" vs return "{1,2,3}" precisely right, -- but, touch wood, it seems fine in all the cases I have tested so far... string this, namedx integer newprst, lo, lp, wasstacklen wasstacklen = length(printstack) if atom(o) then this = sprintf("%.10g", o) if integer(o) then if o>=#20 and o<=#FF then this &= sprintf("'%s'",o) end if elsif not find('.',this) and not find('e',this) -- eg 1e308 and not find('n',this) then -- (inf/nan) -- -- (Ensures you can tell 5 and 5.00000000001 apart. -- Note that while you can infer from the presence -- of ".0" it is "not integer", in no way does the -- /absence/ mean anything at all about whether a -- variable was declared integer/atom/object/udt.) -- this &= ".0" end if elsif string(o) then this = '"'&o&'"' else if length(idii) then namedx = sprintf("%s[%d]",{name,prdx}) else namedx = name end if this = "" newprst = 1 for i=1 to length(o) do {newprst,this} = cdi(namedx,this,newprst,i,o[i],idii&i) end for if newprst>1 or (length(idii) and wasstacklen<length(printstack)) then if length(prev) then if prst=prdx-1 then namedx = sprintf("%s[%d]",{name,prst}) else namedx = sprintf("%s[%d..%d]",{name,prst,prdx-1}) prev = "{"&prev&"}" end if idii[$] = prst addtostack(idii,namedx,prev) idii[$] = prdx end if lo = length(o) if newprst<=lo then if prdx=-1 then if newprst=lo then name = sprintf("%s[%d]",{name,lo}) else this = "{"&this&"}" name = sprintf("%s[%d..%d]",{name,newprst,lo}) end if addtostack(idii&newprst,name,this) else if newprst=lo then name = sprintf("%s[%d][%d]",{name,prdx,lo}) else this = "{"&this&"}" name = sprintf("%s[%d][%d..%d]",{name,prdx,newprst,lo}) end if addtostack(idii&newprst,name,this) end if end if return {prdx+1,""} end if this = "{"&this&"}" end if lp = length(prev) if lp=0 then if not string(o) or length(this)+length(name)+4<MAXLINELEN then return {prst,this} end if elsif lp+length(this)+length(name)+4<MAXLINELEN then return {prst,prev&','&this} else if prdx=prst+1 then namedx = sprintf("%s[%d]",{name,prst}) else namedx = sprintf("%s[%d..%d]",{name,prst,prdx-1}) prev = "{"&prev&"}" end if idii[$] = prst addtostack(idii,namedx,prev) end if if string(o) then if prdx!=-1 then name = sprintf("%s[%d]",{name,prdx}) end if newprst = 1 idii &= 1 lo = length(o) while lo-newprst+length(name)+13>MAXLINELEN do lp = newprst+MAXLINELEN-14-length(name) this = '"'&o[newprst..lp]&'"' idii[$] = newprst namedx = sprintf("%s[%d..%d]",{name,newprst,lp}) addtostack(idii,namedx,this) newprst = lp+1 end while if newprst!=1 then name = sprintf("%s[%d..%d]",{name,newprst,lo}) this = '"'&o[newprst..lo]&'"' end if idii[$] = newprst addtostack(idii,name,this) return {prdx+1,""} end if return {prdx,this} end function procedure clever_dump(string name, object o) integer prst string s printstack = {} {prst,s} = cdi(name,"",1,-1,o,{}) if length(s) then addtostack({prst},name,s) end if printstack = sort(printstack) for i=1 to length(printstack) do printf(1,"%s = %s\n",printstack[i][2..3]) end for end procedure constant symtab = {1,0,3,4,0,0,0,8,0,0,0,12,0,0,15, {"C:\\Program Files (x86)\\Phix\\builtins\\", "C:\\Program Files (x86)\\Phix\\", "C:\\Program Files (x86)\\Phix\\demo\\misc\\eubins-mingw32\\"}, {{3, "someotherfile.exw"}, {3, "somefile.e"}, {1, "prtnid2.e"}, {1, "psprint.e"}, {1, "pprntf.e"}}, 26,2, {-1073741819,0,32463637,32017528,253,32049420,0,0}, {-1,8,1,2304,0,253, "P", 0,0,0,32452560, {0,-77,28,35,38,-1,41,46,-1,49,52, 55,-1,58,-1,124,155,163,210,256,259,280, 291,307,345,352,396,434,-1,436,478,483,-2,501,550, -1,561,-3,577,625,662,700,712,727,738,-1,744,782, 790,808,852,890,905,950,-2,957,962,970,972,983,-2, 991,1033,1038,1048}, 1,0}, 0} clever_dump("symtab",symtab) clever_dump("simple",1) clever_dump("text","text") clever_dump("shortsq",{-1,0,1}) --sequence s = repeat(0,10) -- for i=1 to 9 do -- s[10] = s -- end for -- clever_dump("s",s) -- for i=1 to 9 do -- s[1] = s -- end for -- clever_dump("s",s) sequence t = repeat(0,10) t[5] = repeat('t',200) clever_dump("t",t) t = repeat('t',200) clever_dump("longtext",t) if getc(0) then end if abort(0)
which generates the following output:
symtab[1..15] = {1,0,3,4,0,0,0,8,0,0,0,12,0,0,15} symtab[16][1] = "C:\Program Files (x86)\Phix\builtins\" symtab[16][2] = "C:\Program Files (x86)\Phix\" symtab[16][3] = "C:\Program Files (x86)\Phix\demo\misc\eubins-mingw32\" symtab[17][1..3] = {{3,"someotherfile.exw"},{3,"somefile.e"},{1,"prtnid2.e"}} symtab[17][4..5] = {{1,"psprint.e"},{1,"pprntf.e"}} symtab[18..20] = {26,2,{-1073741819,0,32463637,32017528,253'²',32049420,0,0}} symtab[21][1..11] = {-1,8,1,2304,0,253'²',"P",0,0,0,32452560} symtab[21][12][1..13] = {0,-77,28,35'#',38'&',-1,41')',46'.',-1,49'1',52'4',55'7',-1} symtab[21][12][14..24] = {58':',-1,124'|',155'ø',163'ú',210'Ê',256,259,280,291,307} symtab[21][12][25..40] = {345,352,396,434,-1,436,478,483,-2,501,550,-1,561,-3,577,625} symtab[21][12][41..55] = {662,700,712,727,738,-1,744,782,790,808,852,890,905,950,-2} symtab[21][12][56..65] = {957,962,970,972,983,-2,991,1033,1038,1048} symtab[21][13..14] = {1,0} symtab[22] = 0 simple = 1 text = "text" shortsq = {-1,0,1} t[1..4] = {0,0,0,0} t[5][1..60] = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" t[5][61..120] = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" t[5][121..180] = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" t[5][181..200] = "tttttttttttttttttttt" t[6..10] = {0,0,0,0,0} longtext[1..56] = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttt" longtext[57..112] = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttt" longtext[113..168] = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttt" longtext[169..200] = "tttttttttttttttttttttttttttttttt"
Anyway, thanks for playing!
Pete
Edit: oops, missed out constant MAXLINELEN, corrected a bug on line 79