Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать

than one save object may be generated, but each will ultimately be restored as the recursive calls return.

In Example 6.6 is the same procedure as the one in Example 6.5, rewritten to use the operand stack instead of the dictionary stack. This looks much simpler and better than using the dictionary stack, but if your recursive procedure requires more than a couple of operands, you may have a difficult time writing and understanding it if you use only the operand stack for your recursion. One additional advantage to this method is that it does not require memory allocation (for the local dictionary) or use of save and restore, and there are fewer instructions executed overall.

Example 6.6: Recursion Using the Operand Stack

/recurse_proc

% int recurse_proc int

{ %def

 

dup 2 div truncate 2 mul cvi

1 index eq { %ifelse % even number

1 add recurse_proc

}if

}bind def

2 recurse_proc

There are several ways to accomplish recursion, as you have seen. None of them is perfect, due partly to the lack of true local variables in the PostScript language. It is easy enough to implement recursion, though, and you need only exercise a reasonable amount of care to keep out of trouble and to avoid execstackoverflow, dictstackoverflow, or stackoverflow errors.

CONCLUDING THOUGHTS

The operand stack is an integral part of the PostScript language, and you cannot avoid using it. Once you start to understand it and to trust it, you will be able to write programs that make very effective use of the stack. A stack-based language seems to make you think backward and upsidedown, but in fact it only makes you think in chunks, where each chunk is the result of one operation. Every operator is grouped with its operands,

Chapter 6: TRUSTING THE STACK

77

and the program is almost read from the middle outward, rather than from top to bottom or from bottom to top.

This chapter touched upon the use of conditionals and loops; the next two chapters address these in much greater detail.

EXERCISES

1. What is the result of the following short piece of PostScript code?

0 0 10 100 { add } for

2.What are the contents of the operand stack after executing the following program segment?

clear

/A /B /C /D /E /a /b /c /d /e

2 copy 6 index 6 index 12 -4 roll exch 5 3 roll

3.Write a procedure called reversestack that will reverse the contents of the operand stack (the topmost element becomes the bottom element).

4.Write a procedure called standardfonts that finds all fonts defined in FontDirectory that have a standard encoding. You can assume the presence of the built-in name StandardEncoding and use it to compare to the Encoding in each font. This procedure should not define any variables. Leave the names of the fonts on the operand stack.

78

Chapter 6: TRUSTING THE STACK