{
  "id": "enwiki-001344653843",
  "slug": "stack-oriented-programming",
  "title": "Stack-oriented programming",
  "qid": "",
  "lead": "Stack-oriented programming is a programming paradigm that relies on one or more stacks to manipulate data and/or pass parameters. Programming constructs in other programming languages need to be modified for use in a stack-oriented system. Most stack-oriented languages operate in postfix or Reverse Polish notation: arguments or parameters for a command are listed before that command. For example, postfix notation would be written instead of (prefix or Polish notation), or (infix notation). The programming languages Forth, Factor, RPL, PostScript, BibTeX style design language and many assembly languages fit this paradigm. Stack-based algorithms manipulate data by popping data from and pushing data to the stack. Operators govern how the stack manipulates data. To emphasize the effect of a statement, a comment is often used showing the top of the stack before and after the statement; this is known as the stack effect diagram. Some stack-oriented languages may use multiple stacks for different purposes; for example, PostScript uses separate stacks for variables, dictionaries, procedures, some typical procedures, and control flow statements. Analysis of the language model allows expressions and programs to be interpreted simply.",
  "body": "Stack-oriented programming is a programming paradigm that relies on one or more stacks to manipulate data and/or pass parameters. Programming constructs in other programming languages need to be modified for use in a stack-oriented system. Most stack-oriented languages operate in postfix or Reverse Polish notation: arguments or parameters for a command are listed before that command. For example, postfix notation would be written instead of (prefix or Polish notation), or (infix notation). The programming languages Forth, Factor, RPL, PostScript, BibTeX style design language and many assembly languages fit this paradigm. Stack-based algorithms manipulate data by popping data from and pushing data to the stack. Operators govern how the stack manipulates data. To emphasize the effect of a statement, a comment is often used showing the top of the stack before and after the statement; this is known as the stack effect diagram. Some stack-oriented languages may use multiple stacks for different purposes; for example, PostScript uses separate stacks for variables, dictionaries, procedures, some typical procedures, and control flow statements. Analysis of the language model allows expressions and programs to be interpreted simply. ( before -- after ) For example, the basic Forth stack operators are described: dup ( a -- a a ) drop ( a -- ) swap ( a b -- b a ) over ( a b -- a b a ) rot ( a b c -- b c a ) The fib function below is described: fib ( n -- n' ) It is equivalent to preconditions and postconditions in Hoare logic. Both comments may also be referenced as assertions, though not necessarily in the context of Stack-based languages. /fib { dup dup 1 eq exch 0 eq or not { dup 1 sub fib exch 2 sub fib add } if } def 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 fib(4) : 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 /a exch def construct. For example, {/n exch def n n mul} is a squaring procedure with a named variable n . Assuming that /sq {/n exch def n n mul} def and 3 sq is called, the procedure sq 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. 2 3 gt { (2 is greater than three) = } { (2 is not greater than three) = } ifelse performs the near equivalent in C: if (2 > 3) { printf(\"2 is greater than three\\n\"); } else { printf(\"2 is not greater than three\\n\"); } Looping and other constructs are similar.",
  "categories": [
    "11060667",
    "20163766",
    "21493616",
    "22922474",
    "22931862",
    "23022637",
    "23603337",
    "24043274",
    "24153116",
    "24348226",
    "24348227",
    "24745121",
    "25186583",
    "26220375",
    "26748027"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1344653843",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1774118593.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001344653843",
    "type": "webpage",
    "title": "Stack-oriented programming",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/stack-oriented-programming",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          19
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          19
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001344653843",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1344653843",
    "note": "Immutable snapshot enwiki-001344653843"
  }
}