Next: 1.5.1 Efficiency issues Up: 1. Onyx Language Tutorial Previous: 1.4 Memory management Contents Index

# 1.5 Stacks

Stacks in Onyx are pretty typical, and a rich set of stack manipulation operators is provided.. 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 mainly concerned with the operand stack, often referred to as ostack. ostack 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. Tthe 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 Efficiency issues
- 1.5.2 Using stacks as queues
- 1.5.3 Using the operand stack as two stacks

Next: 1.5.1 Efficiency issues Up: 1. Onyx Language Tutorial Previous: 1.4 Memory management Contents Index