Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 447
Alaya Avatar asked Mar 14 '23 14:03

Alaya


1 Answers

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. :)

like image 162
luser droog Avatar answered May 16 '23 09:05

luser droog