Category:Initlib: Difference between revisions

Line 23:
/empty? {dup length 0 eq?}.
/zero? {dup 0 eq?}.
% stack invariant, execute any predicate and 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 .ifpred {/.top exch def
_restore .thentop
end}.
 
 
% stacky functions
Line 80 ⟶ 91:
} loop pop pop]
}.
 
/all? {
} {
{empty?} ? {pop true exit} if
uncons {?} dip exch not {pop false exit} if
} {loop
}.
 
/any? {
{
{empty?} ? {pop false exit} if
uncons {?} dip exch {pop true exit} if
} ifelseloop
}.
/pipe {
{
{empty?} ? {pop exit} if
uncons {i} dip
} ifelseloop
}.
% 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.
Line 97 ⟶ 136:
% ift - allow stack invariant if condition
/ift {
[/.if /.then] let
4 dict begin
[/.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 ' ? /.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 178 ⟶ 203:
ifte
end}.
 
/cvstr {
4 dict begin
count array astore /.stackelements exch def
/len elements length def
/str len string def
/i 0 def
_restore {
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
 
 
418

edits