Category:Initlib: Difference between revisions

Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.
m (Use a lib template)
(Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.)
 
Line 1:
{{Library}}
Here is a library of functions tested in ghostscriptGhostScript. It is assumed to be loaded before startup with a command -line such as
<syntaxhighlight lang="shell" inline>
<lang>
ghostscript -q -dNODISPLAY -c '(initlib.ps) run'
</syntaxhighlight>
</lang>
 
== <code>initlib.ps</code> ==
<langsyntaxhighlight lang="postscript" lines>
%!PS
% conventions: parameter names with let begins with .
Line 23:
/empty? {dup length 0 eq?}.
/zero? {dup 0 eq?}.
 
% stack invariant, execute any predicate and leave stack untouched
% leave stack untouched except for answer.
/? {
4 dict begin
[/.pred] let*
count array astore /.stack exch def
/_restore {clear .stack aload pop}.
.stack aload pop .pred /.top exch def
_restore .top
end}.
 
 
% stacky functions
Line 64:
/uncons {getname getbody}.
/concat {exch [ rup aload pop counttomark -1 roll aload pop ] }.
 
% make a unit list.
/unit {1 array astore cvx}.
Line 81 ⟶ 82:
/find {
4 dict begin
/aif {0 /get /if}.
/atox { [ exch cvx {cvx} forall ] cvx}.
[ rup [ /dup rdown /exec /not [{pop}] aif ] atox forall ]
end}.
 
/transpose {
[ exch {
{ {empty? exch pop} map all?} {pop exit} ift
[ exch {} {uncons {exch cons} dip exch} fold counttomark 1 roll] uncons
} loop ] {reverse} map
}.
/zip {[rup] transpose}.
Line 96 ⟶ 97:
% [[1 2 3 4 ] [5 6 7 8] [9 10 11 12]] transpose
/all? {
{
{
{empty?} ? {pop true exit} if
uncons {?} dip exch not {pop false exit} if
} loop
}.
% 1 {{10 gt} {5 gt} {0 gt}} any?
/any? {
{
{
{empty?} ? {pop false exit} if
uncons {?} dip exch {pop true exit} if
} loop
}.
/pipe {
{
{
{empty?} ? {pop exit} if
uncons {i} dip
} loop
}.
% 1 {{2 *} {3 *} {5 *}} pipe
/collect {
{
{
{empty?} ? {pop exit} if
uncons {?} dip
} loop
}.
% 1 {{2 *} {3 *} {5 *}} collect
Line 126 ⟶ 127:
/treemap {
[/.tree /.rec] let
/.tree '
{leaf?} /.rec '
{{empty?} {}
{dup
{first /.rec ' treemap} dip
{rest /.rec ' treemap} i cons}
ifte}
ifte
end}.
 
Line 140 ⟶ 141:
/ift {
[/.if /.then] let
/.if ' ? /.then ' if
end}.
% ift - allow stack invariant ifelse condition
/ifte {
[/.if /.then /.else] let
/.if ' ? /.then ' /.else ' ifelse
end}.
% switch statement.
Line 154 ⟶ 155:
/dip {
[/.v /.q] let
.q /.v '
end}.
/apply {exec}.
Line 161 ⟶ 162:
/linrec {
[/.if /.then /.rec1 /.rec2] let
/.if ' /.then '
{.rec1
{/.if ' /.then ' /.rec1 ' /.rec2 ' linrec} i
.rec2}
ifte
end}.
 
/binrec {
[/.if /.then /.rec1 /.rec2] let
/.if ' /.then '
{ .rec1
{/.if ' /.then ' /.rec1 ' /.rec2 ' binrec} dip
{/.if ' /.then ' /.rec1 ' /.rec2 ' binrec} i
.rec2 }
ifte
end}.
 
/genrec {
[/.if /.then /.rec1 /.rec2] let
/.if ' /.then '
{.rec1
{/.if ' /.then ' /.rec1 ' /.rec2 ' genrec}
.rec2}
ifte
end}.
 
Line 191 ⟶ 192:
/primrec {
5 dict begin
/lzero? {
{list?} {empty?}
{zero?}
ifte}.
/lnext {
{list?} {rest}
{pred}
ifte}.
[/.param /.then /.rec] let*
{/.param ' lzero?} /.then '
{.param
{/.param ' lnext /.then ' /.rec ' primrec} i
.rec}
ifte
end}.
 
/cvstr {
4 dict begin
/elements exch def
/len elements length def
/str len string def
/i 0 def
{
{
i len ge { exit } if
str i
% %The element of the array, as a hexadecimal string.
% %If it exceeds 16#FF, this will fail with a rangecheck.
elements i get cvi
put
/i i 1 add def
} loop
str
end
} def
Line 230 ⟶ 231:
/puts {= (\n) print flush}.
/, {(=============\n)
print pstack
(=============\n) print}.
 
% set the prompt to something else so that we know initlib is loaded.
/prompt {(>| ) print flush} bind def</syntaxhighlight>
 
</lang>
6

edits