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

自闭症网瘾萝莉.ら 提交于 2019-12-23 23:30:49

问题


Example with values 10 and 20 known before run-time to give better understanding of the actual question below:

/point1 { 10 20 } def places the numbers 10 and 20 into a (anonymous) procedure and then assigns it to the name point1 (so it isn't anonymous any more). Then name point can be used, i.e. whenever the interpreter finds it, it will execute { 10 20 }, i.e. 10 and 20 will be pushed onto the stack.

Stack before and after executing def:

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

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

Now the actual question: Suppose the two values 10 and 20 will be computed at run-time. How to assign them (or any number of top n stack elements) to a given name in order to use it later?

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

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

回答1:


In postscript, procedures are just arrays with the executable flag set. So you can construct an array (however you like) and then call cvx on it.

/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

So, for your hypothetical procedure, it could be done like this:

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

Another interesting thing you can do is have an executable array and a literal array of the same underlying array at the same time. You could use one defined as the procedure name and the other defined as a target. This way you can update the contents without allocating new memory every time.

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

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



回答2:


Hmm, why not just execute the script that produces the values? Then they are on the stack just as after calling "point1".

But you can use

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

so xyz contains a procedure producing the two produced numbers on the stack...



来源:https://stackoverflow.com/questions/38002304/in-postscript-how-to-place-some-stack-elements-computed-at-run-time-into-a-pr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!