The Stack class implements a stack of elements. This class uses the data type Variant for its elements.
A stack is a data structure that functions according to the LIFO principle. The principle “Last in, first out” describes abstractly what can be imagined, for example, as a stack of paper in a box. If you want to add a sheet of paper to the stack, you have to place it on top of the stack. The effort to lift parts of the stack to place the paper anywhere is simply too great. For this reason, it is also possible to remove only the topmost sheet of paper from the pile. The last added element is the first removed element (LIFO). In this sense, the stack is a data structure opposite the queue. The last element of a stack is also referred to as its topmost element. It is characteristic for the stack that it is only possible to operate at the top of the data structure - add and remove elements.
The properties of a stack are shown in the following table:
|.Size||Integer (ReadOnly)||Number of elements of the Stack|
|.IsEmpty||Boolean (ReadOnly)||True if there are no elements in the stack, otherwise false|
Table 18.104.22.168.1: Properties of the class stack
With these 4 methods, a stack can be used as a data structure according to the LIFO principle:
|Clear ()||Remove all elements from the stack (Stack)|
|Push (vElement)||Place an element (top) on the stack|
|Pop ()||Remove and return the topmost element from the stack|
|Peek ()||Return of the topmost element from the stack - without removing it|
Table 22.214.171.124.2.1: Methods of the class stack
The Gambas interpreter also manages a stack on which all function calls are logged. This information can prove invaluable in the event of a bug in the IDE, for example in case of a program crash, to identify and fix the bug. You can better understand how the error occurred.
The stack of the Stack class in the project stores all functions that were called in the current code path. A code path is a chain of function calls. The associated stack is also called a “Call trace” or “Backtrace”. A backtrace is created using the following simple algorithm:
A function that calls another function gets the stack back in the state it was before the function call. However, if a program error occurs in a nested function, the backtrace stack can be examined. Then all functions can be identified that were called in the error-creating code path because their stack elements could not yet be removed. The most recently called function, which causes the error directly, is therefore always the topmost element of the backtrace stack.
A backtrace is a classic example of a stack. In the project for the stack class, such a backtrace is created:
In addition to backtraces, stacks are also used for the implementation of parsers:? http://de.wikipedia.org/wiki/Umgekehrte_Polish_notation