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!
=== Anatomy of some typical procedures === Procedures often take arguments. They are handled by the procedure in a very specific way, different from that of other programming languages. To examine a [[Fibonacci number]] program in PostScript: <syntaxhighlight lang="postscript"> /fib { dup dup 1 eq exch 0 eq or not { dup 1 sub fib exch 2 sub fib add } if } def </syntaxhighlight> A recursive definition is used on the stack. The Fibonacci number function takes one argument. First, it is tested for being 1 or 0. Decomposing each of the program's key steps, reflecting the stack, assuming calculation of <code>fib(4)</code> : stack: 4 dup stack: 4 4 dup stack: 4 4 4 1 eq stack: 4 4 false exch stack: 4 false 4 0 eq stack: 4 false false or stack: 4 false not stack: 4 true Since the expression evaluates to true, the inner procedure is evaluated. stack: 4 dup stack: 4 4 1 sub stack: 4 3 fib : ''(recursive call here)'' stack: 4 F(3) exch stack: F(3) 4 2 sub stack: F(3) 2 fib : ''(recursive call here)'' stack: F(3) F(2) add stack: F(3)+F(2) which is the expected result. This procedure does not use named variables, purely the stack. Named variables can be created by using the <code>/a exch def</code> construct. For example, <code>{/n exch def n n mul}</code> is a squaring procedure with a named variable <code>n</code>. Assuming that <code>/sq {/n exch def n n mul} def</code> and <code>3 sq</code> is called, the procedure <code>sq</code> is analysed in the following way: stack: 3 /n exch stack: /n 3 def stack: ''empty'' (it has been defined) n stack: 3 n stack: 3 3 mul stack: 9 which is the expected result.
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)