Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Stack Operators

Final has two kinds of stack operators:

  • explicit stack selection
  • Context Selector (cs) stack selection

As explained in the previous section, cs-aware operators referring to the Data Stack do not use a selector, DS is the default.

Some of these operators have been inherited from Forth, which as explained earlier, often required programmers to engage in mind-numbing stack value juggling; Final’s design and coding conventions have greatly reduced the need for such juggling, so don’t worry about words like rot or pick and such–they don’t find much use in Final. dup drop peek and the interstack movement operations make up the vast majority of Final’s stack repertoire.

The term tos means Top of Stack.

Explicit Stack Selection Operators

These operators explicitly state the stacks involved. All the to/from ones imply DS as the unspecified half. These operators are used frequently.

OpEffectDescription
>ts( val:a – ) t( – val )move value from DS to TS
ts>( – val:a ) t( val – )move value from TS to DS
tdropt( val:a – )discard tos of TS
>xs( val:a – ) x( – val )move value from DS to XS
xs>( – val:a ) x( val – )move value from XS to DS
xdropx( val:a – )discard tos of XS
>ys( val:a – ) y( – val )move value from DS to YS
ys>( – val:a ) y( val – )move value from YS to DS
ydropy( val:a – )discard tos of YS
>zs( val:a – ) z( – val )move value from DS to ZS
zs>( – val:a ) z( val – )move value from ZS to DS
zdropz( val:a – )discard tos of ZS

CS-Aware Stack Operators

These operators act on the stack selected by a Context Selector (t:, x:, y:, z:, or none for DS).

OpEffectDescription
dupcs( val:a – val val )duplicate the value on tos
dup2cs( a:a b:a – a b a b )duplicate the top two values
dropcs( val:a – )discard tos
drop2cs( a:a b:a – )discard the top two values
swapcs( a:a b:a – b a )swap the top two values
overcs( a:a b:a – a b a )copy the second value to tos
nipcs( a:a b:a – b )discard the second value
tuckcs( a:a b:a – b a b )copy tos under the second value
pickcs( … +n:i – … val )copy the nth value to tos (0 = tos)
rotcs( a:a b:a c:a – b c a )rotate the top three values
peekcs( val:a – val ) ( – val )copy tos of CS onto DS
ds>cs( val:a – ) ( – val )move tos of CS onto DS
>ds( val:a – ) cs( – val )move tos of DS onto CS
depthcs( – n:i )depth of the selected stack