Carrot
A program is text until the first caret. Everything after that edits the text.
kritixilithos · Turing complete: unknown · ●○○ easy
Tested with 8 known programs · Hello World on Try It Online: identical except the line end · Test method
Run it
The idea
Carrot was written in JavaScript by kritixilithos. Its trick is the starting mode. A program begins in caret mode, where every character is just added to the value, so any text without a ^ is a program that prints itself. Hello, World! needs no quotes and no command.
Read the story
After the first ^ the program is in normal mode. A command is one character, followed by its literal arguments, numbers and "strings". Another ^ opens a new caret block that appends text, and a v opens one that puts text in front.
The machine
The value lives in a cell of the garden, a grid that grows in every direction as you walk off it. Each cell holds three values, a string, a float and an array, and is in one of three modes; the commands act on the one in use.
| Command | String mode | Float mode |
|---|---|---|
+ x | Append x | Add x |
- x | Remove every x, or the first x characters | Subtract x; alone it negates |
* n | Keep the string and add n more copies | Multiply; alone the sign |
% n | Keep the first n characters | Remainder |
F | Read it as a float and switch to float mode | |
S | Write it as a string and switch to string mode | |
A "s" | Split by s into the array | |
[ ... ] | Repeat while the value is not empty | Repeat while it is not 0 |
h j k l | Move left, down, up, right |
At the end the value of the current cell is printed with a newline.
Example
There is no caret, so the whole program is text.
There is no caret, so the whole program is text.
The text 3 becomes the float 3 with F, then it is doubled and one is added.
* keeps the string and adds one more copy, as the reference's loop does.
The loop subtracts one while the float is not 0.
This implementation is a subset. It stops with a reason on input, regular expressions, arrays written in the program, subshells and printing an array, rather than guess what Node would write.
Patterns it teaches
- Code golf: a constant output costs exactly its own length.
- Every text runs: any text without a caret is a valid program.
Try this
- Print
Hellofive times on one line. - Count down from 10 to 0 and print only the 0.
- Hard: build a word in two cells and print the second.
Further
- The reference interpreter, in JavaScript, with commands.txt
See also
Also in Golfing
Write your own in Run Convert a program from Carrot
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: carrot.
Command line
shib run carrot -e 'abc^*1'shib manual carrotSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/carrot.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language