Im trying to implement a VM/interpreter for a toy language, and have gone the stack based route. Local variables, parameters etc get pushed onto the stack, and popped back off once the function completes and scope is returned to the caller (Illustration below)
All of the literature Iv read suggests using a stack, but they will then randomly access elements (say accessing func1's parameters from within func1). Is this achieved by popping everything off the stack until you reach the desired element, use it, then push everything back on in reverese?
Or is the stack not actually something like std::stack and something like a vector or just a contiguous piece of memory the VM/interpreter is managing?