다음을 통해 공유


Pop-arazzi

When do things get popped off the stack?  The question was posed in a comment, and I think it deserves some space as a post, so here we go:

Question:

If I have the following script:

0 (>L:MyVariable, number)

will '0' still be on the stack?

Answer:

No.  It won't.  Any time we assign a value to a variable, be it a C:, G:, K: or L: variable (remember that you can't assign directly to A:, E:, P:, M:, or R: variables), the value gets popped off the stack.  But if we want a copy of it on the stack, we can use the super-intuitive 'd' operator (why the stack operators all use one character instead of splurging and using two or three, such as 'dup', baffles me).  'd' duplicates whatever is on top of the stack, so that when one copy is popped off to be assigned to MyVariable, another copy remains to be used in future operations. 

In an attempt to be perfectly clear, I'll give an example:

5  1 d (>L:MyVariable1)  + (>L:MyVariable2)

Here's the state of the stack and local variables after each of the 6 steps:

  1. 5 (put 5 on the stack)
  2. 5
    1 (put 1 on the stack)
  3. 5
    1
    1 (duplicate the top value on the stack)
  4. 5
    1  (MyVariable1 = 1)
  5. 6  (5 + 1 = 6)
  6. (MyVariable2 = 6)

While we're at it, for those of us who haven't looked at the panels SDK documentation in a while, let's review the list of stack operators.

 

Operator Operation
c Clears the stack.
d Duplicates the value that is on the top of the stack.
p Pops and discards the top value on the stack.
r Reverses the top and second values on the stack.
s0, s1, … s49 Stores the top values in internal registers.
l0, l1, … l49 Loads values from the registers on the top of stack.
sp0, sp1, … sp49 Stores the top value and pops it from the stack.

 

Note the somewhat subtle difference between 's0' and 'sp0'.

Well, I think that's all for stack operators for now!  Thanks for the question, Tom!

Fun with words: Paparazzi is the plural form of what word? Cheat here.

Comments

  • Anonymous
    November 05, 2005
    The comment has been removed
  • Anonymous
    November 07, 2005
    Hi again,

    Thanks for the answer! I appreciate it. I wasn't sure and thus kept putting 0's in my code, one after the other. Glad to hear they weren't clogging up the stack.

    Which brings up another question - if you create code that constantly adds numbers to the stack can you get a stack overflow, or are the lowest numbers discarded? If so, how big IS the stack?

    Thanks,