aboutsummaryrefslogtreecommitdiffstats

Reverse Polish Scheme: Or, call/cc, The Ultimate Opcode

Programming languages should not be designed by piling feature on top of feature, but by taking away as many features as possible until what is left is completely unusable. Reverse Polish Scheme demonstrates that having one way of forming expressions, with no restrictions on how it is used, suffices to create a pile of slow, uncomposable and difficult to understand programs.

Reverse Polish Scheme (RPS) is a homoiconic stack-based programming language vaguely based off of Forth but really just Scheme in disguise. RPS has a single stack that can be saved and restored using call/cc to capture a part of the continuation of the program. The call/cc primitive is powerful enough to do express stack manipulations like swap, pick, and roll, function calls that return using the continuation passing style, non-local exits, closures, and delimited continuations.

Another name for RPS is FIFTH, because that's what you'll want to drink after writing in this language!

Core

RPS is written in a subset of Scheme's syntax.

Line comments start with ;. Nested block comments start with #| and end with |#. Writing a number, string, #t, or #f will push that literal value onto the stack. Writing an identifier will execute that identifier.

A literal vector is introduced with #( and ends with ). A literal list (a linked list where each list cell is a vector of two elements) starts with ( and ends with ). The empty list (nil) is represented by (). A literal identifier is introduced with '. (Note that lists and vectors are not quoted.)

A procedure is a linked list whose first element can be used for data storage, and whose tail is a linked list of instructions to execute.

If the interpreter hits the end of a procedure, it will halt.

The primitive procedures are:

Vectors

  • n vector: Allocate a vector of n values.
  • vec vector-length: Get the length of a vector. Returns #f if the input is not a vector.
  • v n ref: Get the nth element of v.
  • value n v set!: Set the nth element of v.

Control Flow

  • sr jump: Jump to a procedure.
  • conditional on-false on-true if: If conditional is truthy, jump to on-true. Otherwise jump to on-false.
  • proc to from call/cc: Jump to proc with the continuation cc pushed to the stack. When m cc jump is called (where cc is the object returned by call/cc), control returns to the instruction after call/cc, except that the stack is the m values on the stack before the jump appended to the values on the stack from from inclusive to to exclusive at the site of call/cc's invocation.

Operations on Values

  • x y eqv?: Compare two values on the top of the stack, returns a boolean.
  • x symbol?: Predicate for symbols.
  • x integer?: Predicate for fixnums.
  • x real?: Predicate for flonums.
  • x y +: Addition.
  • x y *: Multiplication.

Implementation

A simple and very portable R4RS Scheme implementation of the Core is located in RPS.scm. One can use quasiquote as a macro system (there is no lexical environment and hence no need for hygenic macros). Some macros that implement stack operations can be found in macros.scm.

RPS has been tested using Scheme 9 From Empty Space.

License

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004

Copyright (C) 2025 Peter McGoron <code@mcgoron.com>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

 0. You just DO WHAT THE FUCK YOU WANT TO.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

If you are concerned about the legalities of this license, then this software is not for you. Go back to Java.