在 PostScript 中:如何将一些堆栈元素(在 运行 时间计算)放入可以分配给名称的过程中?

In PostScript: How to place some stack elements (computed at run-time) into a procedure that can be assigned to a name?

在 运行 时间 之前 已知值 10 和 20 的示例,以便更好地理解以下实际问题:

/point1 { 10 20 } def 将数字 10 和 20 放入(匿名)过程 然后将其分配给名称 point1(因此它不再是匿名的)。 然后可以使用 name point ,即每当解释器找到它时,它 将执行 { 10 20 },即 10 和 20 将被压入堆栈。

执行前后堆栈def:

Stacke before:             Stack after:
{ 10 20 }                  -
/point1

Dict before:               Dict after:
-                          point1 --> { 10 20 }

现在真正的问题是:假设两个值 10 和 20 将在 运行-time 计算。如何分配它们(或任意数量的顶部 n 堆栈元素) 改成给定名称以便以后使用?

Stacke before:             Stack after:
<y>                        -
<x>
/<name>

Dict before:               Dict after:
-                          <name> --> { <x> <y> }

嗯,为什么不直接执行生成值的脚本呢?然后他们就像在调用 "point1" 之后一样在堆栈上。

但是你可以使用

/xyz [ <call you procedure producing the numbers> ] cvx def

所以 xyz 包含一个在堆栈上生成两个生成数字的过程...

在 postscript 中,过程只是设置了 可执行标志 的数组。因此,您可以构造一个数组(随心所欲),然后对其调用 cvx

/x 3 def
/y 4 def
[ x y ] cvx  % { 3 4 }
x y [ 3 1 roll ] cvx
x y 2 array astore cvx
{ //x //y }
({//x //y}) cvx exec
({//x //y}) token pop exch pop

因此,对于您假设的程序,可以这样完成:

/makepairproc { % x y  ->  { x y }
    [ 3 1 roll ] cvx
} def

您可以做的另一件有趣的事情是同时拥有一个可执行数组和一个 相同底层数组 的文字数组。您可以使用一个定义为 procedure 名称,另一个定义为目标。这样就可以不用每次都分配新的内存来更新内容了。

/point1 { 10 20 } def
/point1arr //point1 cvlit def

30 40 point1arr astore  %update contents
point1  % 30 40         %execute contents