|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > Smart Package Robot 's Stack Commands |
MiniRobotLanguage (MRL)
Stacks and STK.-Commands
Stacks are Last-In-First-Out (LIFO) data structures that allow efficient data manipulation.
Intention
The **STK.-Commands** provide a powerful way to manage stacks in the SPR. Stacks are used to store and retrieve data in a Last-In-First-Out (LIFO) manner, making them ideal for scenarios where the order of data processing is critical. Each stack is accessed via a **handle**, which is a unique identifier for the stack. The handle allows you to perform operations such as pushing, popping, peeking, and more.
Stacks can store data of different types, including strings, integers, and floating-point numbers. The **STK.-Commands** also support advanced operations like importing/exporting data, reversing the stack, rotating elements, and cloning stacks.
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. This means that the last element added to the stack will be the first one to be removed. Stacks are commonly used in programming for tasks such as:
•Function call management (call stack).
•Undo mechanisms in applications.
•Expression evaluation and syntax parsing.
•Backtracking algorithms.
A **handle** is a unique identifier for a stack. When you create a new stack using the **STK.New** command, a handle is returned. This handle is used to perform all subsequent operations on the stack, such as pushing, popping, or clearing data. Handles ensure that multiple stacks can be managed independently within the same script.
The **STK.-Commands** provide a comprehensive set of operations for managing stacks. Below is a summary of the available commands:
•STK.New: Allocates a new stack and returns its handle.
•STK.End: Frees the allocated stack and returns null.
•STK.Validate: Checks if the handle is valid for the stack.
•STK.Clear: Deletes all data in the stack.
•STK.Count: Returns the number of items in the stack.
•STK.Push: Pushes a value onto the stack.
•STK.Peek: Peeks at the top value without removing it.
•STK.Pop: Pops the top value from the stack.
•STK.Import: Imports a string into the stack.
•STK.Export: Exports the stack as a string.
•STK.GetType: Returns the data type of the stack.
•STK.Reverse: Reverses the order of elements in the stack.
•STK.Rotate: Rotates the top elements of the stack.
•STK.InvRotate: Inversely rotates the top elements of the stack.
•STK.Add: Adds the top elements and pushes the result onto the stack.
•STK.Dup: Duplicates the top element.
•STK.Swap: Swaps the top two elements.
•STK.Clone: Clones the stack and returns the handle to the cloned stack.
•STK.Swapx: Swaps elements at specified positions.
•STK.Show: Displays a debug print of the stack.
Below are some examples demonstrating the use of **STK.-Commands**:
' Example 1: Create a stack, push values, and pop them
STK.New|$$STK
STK.Push|$$STK|Hello
STK.Push|$$STK|World
STK.Pop|$$STK|$$RES
DBP.$$RES ' Output: World
STK.End|$$STK
' Example 2: Reverse a stack
STK.New|$$STK
STK.Push|$$STK|1
STK.Push|$$STK|2
STK.Push|$$STK|3
STK.Reverse|$$STK
STK.Pop|$$STK|$$RES
DBP.$$RES ' Output: 1
STK.End|$$STK
' Example 3: Clone a stack
STK.New|$$STK
STK.Push|$$STK|A
STK.Push|$$STK|B
STK.Clone|$$STK|$$CLN
STK.Pop|$$CLN|$$RES
DBP.$$RES ' Output: B
STK.End|$$STK
STK.End|$$CLN
The **STK.-Commands** provide a powerful and flexible way to manage stacks in the SPR. Whether you need to store data temporarily, reverse elements, or perform advanced operations like cloning and rotating, these commands have you covered. By using handles, you can manage multiple stacks independently, making them ideal for complex scripting scenarios.