Keg
Only symbols are commands. Every other character, letters and spaces included, pushes itself.
JonoCode9374, 2018 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 4 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
JonoCode9374 made Keg in 2018 for golfing, with readability as a second goal. Its main idea is that you never quote text. Hello pushes five character codes. Only a small set of symbols are commands, so text costs nothing extra. The author says Keg strings come out a byte or two shorter than in other golfing languages.
Read the story
The other idea is structures that read well. [yes|no] is an if, (n|body) is a for loop and {cond|body} a while loop. Leave out a part and it gets a sensible default: a for loop without a count runs once per item on the stack. Closing brackets can be left off at the end of a program.
Keg grew a lot later (the Reg and Keg+ extensions add dozens of Unicode commands). This implementation covers the core language and a few of the extras.
The machine
One stack and one register. Numbers are integers, and division makes floats.
| Char | Does |
|---|---|
0–9, other characters | Push the digit, or the character's code |
\c # | Push the code of a command character; comment to end of line |
! : _ $ ^ | Length, duplicate, drop, swap, reverse |
' " | Rotate: bottom to top, top to bottom |
, . ? | Print a character, print a number, read a line |
& | Store the top in the register, or push it back |
+ - * / % < > = | Pop x, then y, push y op x |
[ … \| … ] | If, popping the condition |
( n \| … ) { c \| … } | For and while loops |
@name n\| … ƒ @nameƒ | Define and call a function that takes n items |
If a program prints nothing, the whole stack is printed at the end.
Example
The letters push themselves, \, and \! push the two characters that are commands.
The letters push themselves, \, and \! push the two characters that are commands. ^ reverses the stack so H is on top, and the loop runs once per item, printing each as a character. Without the ^(!|,) part Keg would print the stack by itself, since nothing else was printed.
Patterns it teaches
- Stack machines: one stack, with loop headers worked out on a temporary one.
- Code golf: free strings and implicit output are two classic golf savings.
- Turing completeness: brainfuck compiles to Keg, with the stack rotations as the tape moves.
Try this
- Print
Hithree times with a for loop. - Print the numbers 1 to 10, one per line.
- Hard: write a function that squares a number and use it on 7.
Further
See also
Golfing languages
Stack-based
Write your own in Run Convert a program from Keg
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: keg.
Command line
shib run keg -e 'Hello\, World\!'shib manual kegSource
- Module
langs/stack/ports.js- Interpreter
langs/stack/keg.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org