GASOIL
The program is a stack too. Every instruction is popped off it and gone, so a loop has to put itself back.
Morex, 2009 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 9 known programs · Test method
Run it
The idea
Morex made GASOIL in 2009, after the HP-41C and HP-28S calculators. The name stands for General Application Stack Oriented Imperative Language. It has two stacks. The data stack holds numbers, strings and blocks of code in parentheses. The program stack holds the code still to run.
Read the story
There is no instruction pointer. Each step pops one element off the program stack, so the program shrinks as it runs. A loop is a call that pushes a block back on top, and an endless loop never runs out of room, because nothing waits to return. Even WHILE and FOR are postfix, so their blocks go on the data stack first.
The machine
| Element | Does |
|---|---|
2 -4 3.1416 "text" (a; b) | Push onto the data stack |
CALL CCALL | Put a named block on the program stack, CCALL only on a true condition |
PARSE | Put a block from the data stack on the program stack |
ITE WHILE UNTIL FOR | If, loops and a counted loop over a memory cell |
+ - * / MOD SQRT INT RND | Arithmetic |
= != < <= > >= NOT AND OR XOR | Comparison and logic, giving 1 or 0 |
DUP DROP SWAP12 and kin | Stack work, counting from #1 at the top |
STO RCL | Store #2 at address #1; recall |
READ WRITE | Read a line; print a value |
& STRLEN SUBSTR INSTR REPLACE ASCII CHR | Strings |
NOP comment | Nothing, so it writes comments |
A program is a list of blocks, each under the name it is called by. The specification shows the data stack as the output, so this implementation prints whatever is left on it at the end, one value a line.
Example
The second adds 1 to 20.
The second adds 1 to 20. FOR counts memory cell 0 from 1 to 20 and runs (0;RCL;+) each time, which adds the cell to the sum below it.
The Fibonacci numbers call suma again as long as the newest number is below 100. Each call adds the top two and leaves them all on the stack.
The specification also has a prime finder, the whole of 99 bottles of beer built as one string, and a brainfuck interpreter with a block for each of the eight commands. It reads the brainfuck program as its first line of input and calls the block named after each character. All of them run.
Patterns it teaches
- Stack machines: one stack for data, one for the program itself.
- Code as data: blocks sit on the data stack until a loop or
PARSEruns them. - Self-modifying code: the program is used up as it runs, and
PARSEturns a string built at run time into code.
Further
See also
Self-modifying
Stack-based
Write your own in Run Convert a program from GASOIL
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: gasoil.
Command line
shib run gasoil -e 'main
("Hello World!"; WRITE)'shib manual gasoilSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/gasoil.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org