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:
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With