Saturday, December 26, 2009

Popping Items From Middle of Stack

I initially thought there was a need to pop items from the middle of stack, specifically for functions and subroutine calls.  Essentially the thinking was that the function or subroutine call would push the arguments on the stack, then upon executing the function/subroutine, it would save the current top of the stack, push it's argument references on the stack (because that is what would be written in the internal language), and then it would pop from the two places as it filled the local variables that represented the arguments.  I won't go into any more details because this is probably not the best way to implement function/subroutine calls.

The way C/C++ works internally is that the arguments are pushed onto the stack, and the function (there are technically no subroutines in C/C++, all routines are functions, though some functions can be defined with void return value, i.e. no return value), uses the values on the stacks as the local variables for the arguments within the function (other local variable are also on the stack below the arguments).  C/C++ only passes by value, not by reference (though by reference is emulated using pointers and C++ actually does has references, but it is still done by pointers internally).

Now I think emulating this function/subroutine call mechanism where the local variables representing the arguments are the values on the stack is a better way.  This mechanism does not need to pop items from the middle of the stack, so this functionality is not necessary.  Though technically, this is the same as removing items from the middle of a list, and I believe will be needed, though not for stacks, but for more generic lists.