Category:Initlib: Difference between revisions
Content added Content deleted
m (→Library) |
|||
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. |
|||
/? { |
|||
⚫ | |||
[/.pred] let* |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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 |
|||
⚫ | |||
}. |
|||
/any? { |
|||
{ |
|||
{empty?} ? {pop false exit} if |
|||
uncons {?} dip exch {pop true exit} if |
|||
⚫ | |||
}. |
|||
/pipe { |
|||
{ |
|||
{empty?} ? {pop exit} if |
|||
uncons {i} dip |
|||
⚫ | |||
}. |
|||
% 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 |
|||
⚫ | |||
/.if ' ? /.then ' if |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end}. |
end}. |
||
% ift - allow stack invariant ifelse condition |
% ift - allow stack invariant ifelse condition |
||
/ifte { |
/ifte { |
||
[/.if /.then /.else] let |
|||
⚫ | |||
/.if ' ? /.then ' /.else ' ifelse |
|||
⚫ | |||
/_restore {count array astore pop .stack aload pop}. |
|||
.stack aload pop .if { |
|||
_restore .then |
|||
⚫ | |||
_restore .else |
|||
⚫ | |||
end}. |
end}. |
||
% switch statement. |
% switch statement. |
||
Line 178: | Line 203: | ||
ifte |
ifte |
||
end}. |
end}. |
||
/cvstr { |
|||
⚫ | |||
⚫ | |||
/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 |
|||