The following Postscript program draws a rectangular box.
Code:
/mybox {
/Yh exch def
/Xw exch def
/Y0 exch def
/X0 exch def
newpath
X0 Y0 moveto
0 Yh rlineto
Xw 0 rlineto
% 0 Yh neg rlineto
closepath
} def
50 50 300 500 mybox stroke
It works, but I don't understand how the following line is executed.
I've read a bunch of tutorials, and apparently exch exchanges the top 2 operands in the stack, while def is used to define a variable. But I don't see how the combination of the two operands sets Yh = 500. Maybe somebody can enlighten me.
Edit!
I think I understand it now.
What exch does is to change
500 /Yh exch def
into
/Yh 500 def
Which sets Yh = 500.
I came to this conclusion after Googling how "exch def" works and found this PDF which explains it.