Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Review: Data structure printing code

54 views
Skip to first unread message

luser droog

unread,
Feb 11, 2018, 10:22:39 PM2/11/18
to
I've reorganized this code to be top-down and functional.
What improvements can be made to this program?

One obvious sore spot to me is that it doesn't measure
the printed length in order to determine when to break
lines but instead just counts characters. This leads to
a weird value for /X which is the upper x bound by my
convention. I think the resulting behavior is okay for
the purposes of this program, but I'm not 100% thrilled
with using /X in a weird way although it simplifies the
rest of the code that deals with it. Counting characters
is just simpler than counting lengths.


$ cat printdata.ps
%!

<<
/x 30
/X 100 /max-line-length 1 index
/Y 750 /max-page-height 1 index
/y 30
/at_start { new-page }
/at_end { marks-on-page { showpage } if }
/marks-on-page false
/line-position 0

/show-it {
show-and-update-position
line-position max-line-length gt { next-line } if
}
/show-and-update-position {
dup length /line-position +=
show
/marks-on-page true store
}
/next-line {
lead /Y +=
x Y moveto
/line-position 0 store
Y y lt { next-page } if
}
/next-page {
showpage new-page
}
/new-page {
/marks-on-page false store
/Y max-page-height store
/Palatino-Roman 10 selectfont /lead -12 store
x Y moveto
}

/show-simple { 100 string cvs show-it ( ) show-it }
/show-object { object-procs type-switch }
/object-procs <<
/default { show-simple }

/nulltype { pop (null ) show-it }
/nametype {
dup xcheck not { (/) show-and-update-position } if % ie. <nobr>
show-simple
}
/stringtype {
(\() show-and-update-position % nobr
show-and-update-position % nobr
(\)) show-it % br
}
/arraytype {
dup all-simple {
([ ) show-it { show-object } forall (] ) show-it
}{
([ ) show-it next-line
+20 /x += x Y moveto
{ show-object next-line } forall
-20 /x += x Y moveto
(] ) show-it next-line
} ifelse
}
/packedarraytype 1 index
/dicttype {
dup {}map all-simple {
(<< ) show-it { exch show-object show-object } forall (>> ) show-it
}{
(<< ) show-it next-line
+20 /x += x Y moveto
{ exch show-object show-object next-line } forall
-20 /x += x Y moveto
(>> ) show-it next-line
} ifelse
}
>>

/all-simple { dup length 0 gt { {is-simple} map {and} reduce }{ pop true } ifelse }
/is-simple { is-compound not }
/is-compound { compound-types exch type known }
/compound-types << [
/arraytype %/packedarraytype % In gs the packed arrays are all small
/dicttype
]{null}forall >>
/type-switch { 1 index type switch }
/switch { 2 copy known not { pop /default } if get exec }
/map { 1 index xcheck 3 1 roll [ 3 1 roll forall ] exch { cvx } if }
/reduce { exch dup first exch rest 3 -1 roll forall }
/first { 0 get }
/rest { 1 1 index length 1 sub getinterval }
/+= { dup load 3 2 roll add store }
>> {def} forall
at_start

<<
/currentpagedevice currentpagedevice
/astring (this string data)
>> show-object

at_end
0 new messages