如何在不使用PostScript滚动的情况下将堆栈顶部的数字移到底部?

问题描述

我想在字典中定义一些东西,将其命名为top,这样它就可以将堆栈顶部的数字移到底部,并将所有其他数字保留在适当的位置。我想不使用roll运算符就这样做。我已经尝试了好几个小时,但是对于PostScript还是陌生的,并且感到沮丧,因为这看起来像是一个简单的任务,我可以用任何其他语言迅速解决

这是我到目前为止所了解的,但不知道为什么它不起作用:

/top {
  1 dict begin
  count 1 gt 
    {
      /first exch def
      /second exch def
      first top second
    }
} def

解决方法

您可以将所有内容存储在一个数组中,然后getgetinterval您想要的片段。

/first { 0 get } def
/rest { 1  1 index length 1 sub  getinterval } def

% a b c d ... n  --  b c d ... n a
/top {
  1 dict begin
    count array astore /stack exch def
    stack rest aload pop
    stack first
  end
} def

这也可能是更多因素。

<<
    /first { 0 get }
    /rest  { 1  1 index length 1 sub  getinterval }
    /aa    { array astore }
    /:=    { exch def }
    /spill { aload pop }
>> currentdict copy

% a b c d ... n  --  b c d ... n a
/top {
  1 dict begin
    count aa /stack :=
    stack rest spill
    stack first
  end
} def

/spill可以用几种方式编写

/spill { {} forall } def

甚至

% assumes array does not contain executable names or arrays
/spill { cvx exec } def

编辑:我可能误解了这个问题,并显示了想要的相反内容。上面的代码执行count -1 roll。要count 1 roll将顶部元素移到底部,

/head { 0  1 index length 1 sub  getinterval } def
/last { dup length 1 sub  get } def

% a b c ... x y z  --  z a b c .. x y
/top {
  1 dict begin
    count array astore /stack exch def
    stack last
    stack head aload pop
  end
} def