Category:Initlib: Difference between revisions

Content added Content deleted
Line 23: Line 23:
/empty? {dup length 0 eq?}.
/empty? {dup length 0 eq?}.
/zero? {dup 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 .pred /.top exch def
_restore .top
end}.



% stacky functions
% stacky functions
Line 80: Line 91:
} loop pop pop]
} 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
} 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.
% do on all elements of a tree.
Line 97: Line 136:
% ift - allow stack invariant if condition
% ift - allow stack invariant if condition
/ift {
/ift {
[/.if /.then] let
4 dict begin
[/.if /.then] let*
/.if ' ? /.then ' if
count array astore /.stack exch def
/_restore {clear .stack aload pop}.
.stack aload pop .if {
_restore .then
} {
_restore
} ifelse
end}.
end}.
% ift - allow stack invariant ifelse condition
% ift - allow stack invariant ifelse condition
/ifte {
/ifte {
[/.if /.then /.else] let
4 dict begin
[/.if /.then /.else] let*
/.if ' ? /.then ' /.else ' 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}.
end}.
% switch statement.
% switch statement.
Line 178: Line 203:
ifte
ifte
end}.
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