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

1

5

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?

Proceeds answered 3/10, 2015 at 11:13 Comment(0)
S
8

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

Symphonious answered 7/2, 2016 at 1:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.