Stacks
Factor handbook ยป The implementation

Prev:Batch error reporting
Next:Images


While the data stack manipulated by user code is the primary stack we often think of (so much so that it is often referred to as just "the stack"), Factor's implementation actually makes use of multiple stacks internally.

Data stack
The data stack is the primary stack that words interact with by popping inputs and pushing outputs. Stack effect checking acts as a type system for the data stack to ensure words always have the number of inputs and outputs declared in the source.

.s prints the current data stack.

Call stack
The VM manages a call stack with stack frames that follow the call hierarchy from the currently executing word all the way back to program startup. Some words are inlined by the Optimizing compiler, so call stack frames for any such words may be omitted.

.c prints the current call stack.

Retain stack
The retain stack is used internally as a kind of temporary holding stack. Words like dip (which temporarily removes values from the data stack, calls some quotation, and then restores those values back onto the data stack) are implemented by moving the values to the retain stack. Lexical variables also make use of the retain stack to store their values efficiently.

Forth and some other concatenative languages expose a related "return stack" to user code. Factor's retain stack plays a similar role, but it is an internal language implementation detail that is not meant to be manipulated by user code.

.r prints the current retain stack.

Name stack
The name stack is used as part of implementing Dynamic variables. Each dynamic variable is part of a namespace. Namespace scopes can nest, so the name stack tracks the nesting order which is used when accessing these variables.

Catch stack
The catch stack contains a vector of Continuations, which are used for several abstractions like Exception handling and Co-operative threads. For example, exception handling with recover captures a continuation and makes use of the catch stack to restore program state if an error occurs.

In some programming language implementations, continuations are quite a complex feature to support. In Factor, continuations are straightforward: they simply capture the state of all of the stacks mentioned here.

See also
Continuation implementation details