Karma
No loops at all. Each line of code is a place you can walk down to and back up from, and it remembers where you left.
revcompgeek, 2008 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
revcompgeek designed Karma as a Turing tarpit with no explicit loops. Instead, the program is a list of lines, and four jump commands move between them: down to the next line, back up to the one above, or to the start of the current line.
Read the story
The trick is that every line remembers where execution last left it. So "go down, then come back up" behaves like calling a function and returning. A line that jumps back to its own start is the only loop there is.
Data lives in a stack and a deque, both of bytes. Comparisons look at the top of the stack against the front of the deque, so the deque often holds the argument of the "function" you are calling.
The machine
A stack and a deque of bytes. Popping either when empty gives 0 in this implementation.
| Char | Does |
|---|---|
0-9 | Push the digit |
+ - * / % & \| ^ | Pop first (top) and second, push first op second |
~ ! | Bitwise NOT, logical NOT of the top |
= > | Pop, push 1 if it equals (is greater than) the deque's front, else 0 |
@ | Pop; skip the next command unless it was 1 |
} { | Pop and insert at the deque's front; take from the front and push |
[ ] | Same at the deque's back |
# \ | Drop, clone the top |
? : ; | Read a byte; print a byte as a character; print as a number |
, . | Go down a line: to its start, or to where it last stopped |
' | Go up a line, to where it last stopped |
< | Go to the start of the current line |
The program ends when a line runs out of characters. An unknown command or a jump off the program is an error.
Example
A cut-down version of the function example.
A cut-down version of the function example. Line 2 puts 2 on the deque and calls down. Line 3 asks "is the deque's front 1?"; no, so @, passes the call down to line 4. Line 4 matches, prints 4·5·6 = 120 as x, and its 1' climbs back up. Each line's @' pops that 1 and climbs on, until line 2 resumes and calls function 1, which prints 1.
Patterns it teaches
- Stack machines: one stack for work, one deque for arguments.
- Turing tarpit: loops are removed and rebuilt from line jumps.
- Turing completeness: the specification sketches a Bitwise Cyclic Tag emulator.
Try this
- Print
42with;, then a newline with:. - Add a function 3 to the example that prints
!(33), and call it. - Hard: write a cat that loops with
<and stops when?gives 0.
Further
See also
Deque-based
Stack-based
Write your own in Run Convert a program from Karma
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: karma.
Command line
shib run karma -e ', This line will be ignored
12+;
The last line will end and therefore so will the program, this is never executed.'shib manual karmaSource
- Module
langs/stack/ports.js- Interpreter
langs/stack/karma.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org