Category:Initlib: Difference between revisions

Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.
(→‎Library: restructuring)
(Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.)
 
(7 intermediate revisions by 2 users not shown)
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 18:
 
% some predicates.
/eq? {eq]}.
/list? {dup type /arraytype eq?}.
/leaf? {list? not}.
/empty? {dup length 0 eq?}.
/zero? {dup 0 eq?}.
 
% stack invariant, execute any predicate and
% leave stack untouched except for answer.
/zip? {
4 dict begin
[/.pred] let*
count array astore /.stack exch def
/_restore {clear .stack aload pop}.
.stack aload pop .ifpred /.top exch {def
_restore .thentop
end}.
 
% stacky functions
Line 53 ⟶ 64:
/uncons {getname getbody}.
/concat {exch [ rup aload pop counttomark -1 roll aload pop ] }.
 
% make a unit list.
/unit {1 array astore cvx}.
Line 70 ⟶ 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}.
 
/zip {
/transpose {
[rup{
[ exch {
dup length 0 eq rdown dup length 0 eq rdown or {exit} if
{ {empty? exch pop} map all?} {pop exit} ift
uncons rdown uncons rdown 4 2 roll unit cons rup exch
[ exch {} {uncons {exch cons} dip exch} fold counttomark 1 roll] uncons
} loop pop pop]
} loop ] {reverse} map
}.
/zip {[rup] transpose}.
 
% [[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
 
% do on all elements of a tree.
/treemap {
[/.tree /.rec] let
/.tree '
{leaf?} /.rec '
{{empty?} {}
{dup
{first /.rec ' treemap} dip
{rest /.rec ' treemap} i cons}
ifte}
ifte
end}.
 
Line 97 ⟶ 140:
% ift - allow stack invariant if condition
/ift {
[/.if /.then] let
4 dict begin
/.if ' ? [/.if /.then] let*' if
count array astore /.stack exch def
/_restore {clear .stack aload pop}.
.stack aload pop .if {
_restore .then
} {
_restore
} ifelse
end}.
% ift - allow stack invariant ifelse condition
/ifte {
[/.if /.then /.else] let
4 dict begin
/.if ' ? [/.if /.then ' /.else] let*' ifelse
count array astore /.stack exch def
/_restore {count array astore pop .stack aload pop}.
.stack aload pop .if {
_restore .then
} {
_restore .else
} ifelse
end}.
% switch statement.
Line 126 ⟶ 155:
/dip {
[/.v /.q] let
.q /.v '
end}.
/apply {exec}.
Line 133 ⟶ 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 163 ⟶ 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 183 ⟶ 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