Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid stackunderflow (use variable # of arguments) in PostScript

I have a simple function with two variables as

/func {
/var1 exch def
/var2 exch def
... process ...
} def

(var2)(var1)func

I want to make var2 optional. However, if not providing var2, it results in stackunderflow error. How can I make a if statement to catch var2 only if the stack is not empty, and probably assign a default value if the stack is empty.

Something like

(Stack is no empty) {/var2 exch def}{/var2 (default) def} ifelse
like image 794
Googlebot Avatar asked Dec 01 '25 09:12

Googlebot


1 Answers

The count operator counts the number of operands on the stack. You might like to instead use [ to put a mark on the stack and then use counttomark instead. This saves you getting confused by operands being left over, or not yet used, when your routine is called from other routines. Of course it means you have to supply the [ as an operand on the stack.

The other usual method is to have the top operand be an integer counting the number of additional operands..

like image 176
KenS Avatar answered Dec 02 '25 23:12

KenS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!