What does the "Duplicate the top item on the stack" mean in Whitespace language?

588 views Asked by At

I am trying to implement a Whitespace interpreter for fun, currently I am following this tutorial to learn its syntax.

The syntax looks easy, but I don't understand what "Duplicate the top item on the stack" mean. What does that mean? does it mean to get the value of stack top and save it into a special register?

1

There are 1 answers

0
luser droog On

It means take the value on top of the stack, without popping it, and push a second copy of the exact same thing.

Now there are 2 of whatever it was.

The exact way to implement will depend upon what functions you have available to manipulate your stack. If you have just push and pop, then you could do it like this:

x = pop();
push(x);
push(x);

If you have a top function which can get the top element without popping it, you could do:

x = top();
push(x);

Or even:

push(top());

which reads as nice as pseudocode. :)