Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Stack-oriented programming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Stack-based algorithms == PostScript is an example of a postfix stack-based language. An expression example in this language is {{nowrap|<code>2 3 mul</code>}}('mul' being the command for the multiplication operation). Calculating the expression involves understanding how stack orientation works. Stack orientation can be presented as the following conveyor belt analogy. At the end of a conveyor belt (the ''input''), plates marked <code>2</code>, <code>3</code>, and <code>mul</code> are placed in sequence. The plate at the end of the conveyor (<code>2</code>) can be taken, however other plates cannot be accessed until the plate at the end is removed. The plates can only be stored in a stack, and can only be added or removed from atop the stack, not from the middle or bottom. Blank plates (and a marker) can be supplied and plates can be permanently discarded. [[File:Human stack.svg|220px]] Take plate <code>2</code> and put it on the stack, then take plate <code>3</code> and put it on the stack. Next, take the <code>mul</code> plate. This is an instruction to perform. Then, take the top two plates off the stack, multiply their labels (<code>2</code> and <code>3</code>), and write the result (<code>6</code>) on a new plate. Discard the two old plates (<code>2</code> and <code>3</code>) and the plate <code>mul</code>, and put the new plate on the stack. With no more plates remaining on the conveyor, the result of the calculation (<code>6</code>) is shown on the plate atop the stack. This is a very simple calculation. What if a more complex calculation is needed, such as {{nowrap|<code>(2 + 3) × 11 + 1</code>}}? If it is first written in postfix form, that is, {{nowrap|<code>2 3 add 11 mul 1 add</code>}}, the calculation can be performed in exactly the same manner and achieve the correct result. The steps of the calculation are shown in the table below. Each column shows an input element (the plate at the end of the conveyor), and the contents of the stack after processing that input. {| class="wikitable" |- ! Input | style="text-align: right;" | {{mono|2}} | style="text-align: right;" | {{mono|3}} | style="text-align: center;" | {{mono|add}} | style="text-align: right;" | {{mono|11}} | style="text-align: center;" | {{mono|mul}} | style="text-align: right;" | {{mono|1}} | style="text-align: center;" | {{mono|add}} |- ! Stack | style="text-align: right; vertical-align: bottom;" | {{mono|2}} | style="text-align: right; vertical-align: bottom;" | {{mono|3<br />2}} | style="text-align: right; vertical-align: bottom;" | {{mono|5}} | style="text-align: right; vertical-align: bottom;" | {{mono|11<br />5}} | style="text-align: right; vertical-align: bottom;" | {{mono|55}} | style="text-align: right; vertical-align: bottom;" | {{mono|1<br />55}} | style="text-align: right; vertical-align: bottom;" | {{mono|56}} |} After processing all the input, the stack contains <code>56</code>, which is the answer. From this, the following can be concluded: a stack-based programming language has only one way to handle data, by taking one piece of data from atop the stack, termed ''pop''ping, and putting data back atop the stack, termed ''push''ing. Any expression that can be written ''conventionally'', or in another programming language, can be written in postfix (or prefix) form and thus be amenable to being interpreted by a stack-oriented language.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)