Jump to content

Stack-based language

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Finlay McWalter (talk | contribs) at 10:40, 20 August 2005 (dab Forth programming language). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, a stack-based language is a computer language that uses the stack to store intermediate results during computation. Forth and Java's byte-code are examples of stack-based languages.

A stack-based language tends to be used as intermediate representation of a program during its compilation.

Advantages of the stack-based languages over others include:

  • An interpreter or compiler for the language tends to be small and simple.

The disadvantages include:

  • It may be more tricky to optimize code.

Example

A piece of code to compute the sum of 10 * (20 + 30) would be like:

push 10  // push 10 onto the stack
push 20  // push 20
push 30  // push 30
add      // pop two values from the stack, and push its sum
mul      // pop two values from the stack, and push its product