スタックに積んで渡された値を辞書に登録するとローカル変数のように使えて便利です。
登録は以下のような手続きで行います。
/valueName exch def
スタックの値を登録するのに exch でスタックの順番を入れ替えて def しています。
その様子を見てみます。
- showStack はスタックの状態を表示する手続きです。次々と表示すると境目が分からなくなってしまいますのでスタックを表示してから区切りに--------------を表示します。
- /str exch def は/strをスタックに積み、exch でスタックの順番を入れ替え、defでスタックを消費して辞書に登録します。その様子を表示するために間に showStack を入れまくっています。
%! /showStack { pstack (--------------\n) print }def /ldict 1 dict def /testLocal { ldict begin % /str exch def showStack /str showStack exch showStack def showStack % /num exch def /num showStack exch showStack def showStack (str=) print str == (num=) print num == end } def 123 (hello) testLocal
$ ps2pdf dict.ps % testLocal手続きが呼ばれたところです。スタックの状態は 123 (hello) testLocalで渡されたまま。 (hello) 123 -------------- % /str がスタックに積まれました。 /str (hello) 123 -------------- % /str と(hello)の順番を入れかえます。def はこの順番になっているとき、名前 /str に(hello)を関連付けるのです。 (hello) /str 123 -------------- % def が実行されたのでスタックが消費されました。 123 -------------- % /num がスタックに積まれます。 /num 123 -------------- % /num と123の順番を入れかえます。 123 /num -------------- % def が実行されたのでスタックが消費されました。 -------------- % 定義された値を表示してみます。 str=(hello) num=123