Next: 1.5.1 Using stacks as Up: 1. Onyx Language Tutorial Previous: 1.4 Memory management Contents Index

# 1.5 Stacks

Stacks in Onyx are pretty typical. Objects are implicitly or explicitly pushed onto stacks by operators, and the stack contents can be rearranged and removed. Although stacks are a first class object in Onyx, most Onyx programs are concerned only with the operand stack, often referred to as ostack, which is used as a place to store objects, pass arguments into operators and procedures, and return results.

Onyx is a postfix language, which means that code is written such that operands precede operators. For example, the following code Calculates and prints the result:

    onyx:0> 5 3 4 add mul
    onyx:1> 1 sprint
    35
    onyx:0>

There are no parentheses to clarify operator precedence, because precedence is implicit in the code.

Stacks are either written bottom to top on one line, or top to bottom on separate lines, as in the following examples. The example stack contains the numbers 0, 1, and 2, where 0 is the top object and 2 is the bottom object:

    onyx:0> 2 1 0
    onyx:3> ostack 1 sprint
    (2 1 0)
    onyx:3> pstack
    0
    1
    2
    onyx:3>

Learning to efficiently (and accurately) manage stacks is a mind-warping process that no amount of reading is likely to impress upon the reader. There are general concepts presented here, but ultimately, the reader will have to write a good bit of code to get a handle on stacks. Even the author of Onyx found himself stumbling over stacks well after Onyx was complete, despite limited exposure to stack-based languages beforehand. The problem seems to be that programmers learn to think in a different way that doesn't exercise the parts of the brain necessary for stack manipulation. Some people might argue that stack manipulation is the job of the compiler. In any case, stack manipulation is an acquired skill that requires practice.

Subsections

- 1.5.1 Using stacks as queues
- 1.5.2 Using the operand stack as two stacks
- 1.5.3 Efficiency issues

Next: 1.5.1 Using stacks as Up: 1. Onyx Language Tutorial Previous: 1.4 Memory management Contents Index