by showmyiq » Sun Mar 17, 2013 10:01 am
We are going to discuss some basic concepts of manipulating the stack. Right now all we need is GhostScript console, so you can close the SciTE editor.
The first command I want to show you is – stack.
This command is going to print the current elements/numbers in the stack.
You can use the pstack command too, it will do the same.
Ok, now write on the terminal stack and inspect the results. If your stack is empty, you should receive just new row with no information outputted. If you already pushed some elements in the stack, you can use the other useful command – clear. It is going to clear all the elements in the stack. It’s like closing the GhostScript and open it again.
Ok, you already know 2 commands. Now let’s see how you can push elements in the stack. Well just type them in the terminal and they will be pushed automatically. Try to type this one: 1 2 stack
You should receive this:
GS> 1 2 stack
2
1
GS<2>
Ok, so far – so good. Let’s analyze the current stack. As you can see with just one row and using only space as delimiter, we have successfully engaged 3 operations. With 1, we are pushing in the stack number 1. With 2, we are pushing in the stack number 2. With stack, we are printing out the current elements in the stack. Easy as that.
Because we are dealing with stack, I should remind you that stack is like that – Last IN, First Out. The top of the stack is always the last element we have pushed.
You noticed that the console was changed now to GS<2>. This means that you have 2 elements in the stack already and they will stay there until you have pop them (pulled them out).
You can now clear the stack with the clear command or push some more elements and see the change of the stack. But how to pull out elements from the stack?
Well, with the command pop. If you want to destroy the element of the top of the stack you should destroy it with pop. Here is an example:
GS<2>pop stack
1
GS<1>
As you can see pop destroyed the top element of the stack and the stack command printed out the remaining elements in the stack (in our case 1). The console section was changed to GS<1>, so you now have 1 element in the stack with value equals to 1.
I recommend playing with those commands until going to the next chapter.