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?
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:
If you have a top function which can get the top element without popping it, you could do:
Or even:
which reads as nice as pseudocode. :)