esolang

DUP

FALSE with one door left open: you can touch the return stack, so loops and tail calls become ordinary code.

Ian Osgood, 2005 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 known programs · Test method

Run it

Expected: 0123456789

The idea

Ian Osgood wrote a FALSE interpreter in JavaScript and kept tweaking it. The result is DUP, named after his favourite Forth word. Most changes are small comforts: if takes both a "then" and an "else" block, there is OVER, / gives remainder and quotient at once, and strings are stored in memory instead of printed.

Read the story

One change is big. In FALSE the parentheses were unused. In DUP, ( moves a value from the data stack to the return stack and ) moves it back. The return stack holds the places where each block resumes when it ends. So a program can pick up its own continuation, drop it, or push a block there to jump into it when the current block finishes. The specification builds until, while and tail calls this way.

That access is what makes DUP Turing complete: the return stack is a second unbounded stack you control.

The machine

A data stack, a return stack, 26 letter variables and a separate memory of numbered cells.

CharDoes
123 'cPush a number or a character code
$ % \ @ ^ øDup, drop, swap, rot, over, pick
+ - * _Arithmetic; _ negates
//MOD: leaves remainder, then quotient
& \| ~And, xor, not
= > <Compare; true is -1
[ … ]Push a block (its code position)
!Run the block on top
c [yes] [no] ?Run one of two blocks
[test] [body] #While loop
( )Move a value to / from the return stack
: ;Store / fetch at a letter or a numeric address
"…"Store a string at the address on the stack, leave the address after it
` , . | Read a character, print a character, print a number
[block]⇒CDefine C as a new operator
{ … }Comment

Popping an empty stack gives 0.

Example

u is until, written in DUP itself.

u is until, written in DUP itself. It saves the body on the return stack with (, runs it, and looks at the flag. On false it throws away its own return address, pushes u onto the return stack and ends, so the closing ] jumps straight back into u: a loop with no loop command. On true it drops its saved values and returns normally. The body prints the counter and asks "is it above 9?".

Patterns it teaches

Try this

  1. Use "…" and the string printer [[$][\$;,1+\1-]#%%]p: to print your name.
  2. Write dip (run a block under the top value) with ( and ).
  3. Hard: write a for loop from 1 to n in the style of until, without #.

Further

Stack-based

Write your own in Run Convert a program from DUP

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: dup.

Command line

shib run dup -e '[$(![)%)%][)%)u;(]?]u:  {until}
0[$.1+$9>]u;!%'
shib manual dup

Source

Module
langs/stack/ports.js
Interpreter
langs/stack/dup.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org