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

Cur*_*urd 2 postscript

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

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

执行前后堆栈def

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

Dict before:               Dict after:
-                          point1 --> { 10 20 }
Run Code Online (Sandbox Code Playgroud)

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

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

Dict before:               Dict after:
-                          <name> --> { <x> <y> }
Run Code Online (Sandbox Code Playgroud)

lus*_*oog 5

在 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
Run Code Online (Sandbox Code Playgroud)

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

/makepairproc { % x y  ->  { x y }
    [ 3 1 roll ] cvx
} def
Run Code Online (Sandbox Code Playgroud)

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

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

30 40 point1arr astore  %update contents
point1  % 30 40         %execute contents
Run Code Online (Sandbox Code Playgroud)