The Pop-11 stack is implicitly referred to in the following contexts:
1. Any Pop-11 expression which denotes some object, is implicitly
an instruction to create or find the object and put it on the
stack. (For most types of object this really amounts to putting a
"pointer" to the object on the stack. The object itself remains
somewhere in memory. For integers and single precision decimals,
a copy of the internal representation is put on the stack.)
E.g.
"3 + 5" puts 8 on the stack
"hd(tl([the black cat]))" puts "black" on the stack
x,y,z; puts the values of x, y and z
on the stack, in that order.
2. When a procedure is invoked with some arguments, the arguments
are put on the stack and then the procedure is run. E.g.
3 + 4
Puts 3 on the stack, then 4, then runs +
hd(list)
Puts the value of list on the stack then runs hd
3. If a procedure invoked with arguments has some input variables,
then when the procedure is run, the appropriate number of items
is taken off the stack and assigned to the variables within the
procedure. For example, consider the procedure perim defined in
Chapter 1, with the heading
define perim(len, breadth) -> total;
The command to run perim, will cause the item on top of the stack
to be removed and assigned to to the input local variable
breadth, then the next item will be removed from the stack and
assigned to len, and then the instructions in the procedure
definition will be obeyed, using these variables.
4. If a procedure is defined to produce any results, then invoking
the procedure will implicitly cause the results to be put on
the stack when the procedure is finished. For example, if the
numbers 3 and 4 are on the stack, and the addition procedure +
runs, then the procedure, when it has finished, will replace the
top two items on the stack with the single result, the number 7.
So the instruction
3 + 4 =>
is equivalent to these four instructions
3;
4;
+;
=>
Another example is what happens when the perim procedure
finishes. Because it has an "output" local variable in the header
line, i.e. "total", then Pop-11 will ensure that just before the
procedure finishes the value of the variable will be put on the
stack.
Exercise: Explain why case 4 is just a special example of case 1.
5. When an assignment instruction is run, e.g.
x -> y
the left hand side, in accordance with point 1 above, causes
the value of the expression to be put on the top of the stack
(i.e. the value of the variable x is put on the stack), and then
the rest of the instruction ( "-> y") causes the item on the top
of the stack to be removed and stored as the value of the
variable y. (I.e. the value is stored in the area of memory
associated with the variable.)
NOTE: there is a potential point of confusion. When the left hand
side of such an expression puts the value of x on the stack it
does not remove the value from x. Thus x still has the same value
as before and it can be re-used. For example the instructions:
x; x; x;
cause the value of x to be put on the stack three times.
By contrast, when the top of the stack is assigned to a variable
it is removed from the top of the stack. Thus if there is only
one thing on the stack you can obey the instruction to move it
to y, thus
99; ;;; Put 99 on the stack
-> y; ;;; move top of stack to y
But attempting to do the same again
-> y; ;;; move top of stack to y
will cause an error message to be printed out if there was
originally only one thing on the stack.
;;; MISHAP - ste: STACK EMPTY (missing argument? missing result?)
Note: although you can assign the top of the stack to a variable, you
cannot assign it to a constant object, such as the number 37, or a
string, or a word. If you try any of these:
3 + 4 -> 7;
"cat" -> 'dog';
33 -> [a b c];
then you will get the following error message:
;;; MISHAP - iue: IMPERMISSIBLE UPDATE EXPRESSION (e.g. after -> or ->>)
33 -> 44;