esolang

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

Expected: Hello, World!

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.

CommandString modeFloat mode
+ xAppend xAdd x
- xRemove every x, or the first x charactersSubtract x; alone it negates
* nKeep the string and add n more copiesMultiply; alone the sign
% nKeep the first n charactersRemainder
FRead it as a float and switch to float mode
SWrite it as a string and switch to string mode
A "s"Split by s into the array
[ ... ]Repeat while the value is not emptyRepeat while it is not 0
h j k lMove 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.

Expected: 7

The text 3 becomes the float 3 with F, then it is doubled and one is added.

Expected: abcabc

* keeps the string and adds one more copy, as the reference's loop does.

Expected: 0

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

Try this

  1. Print Hello five times on one line.
  2. Count down from 10 to 0 and print only the 0.
  3. Hard: build a word in two cells and print the second.

Further

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 carrot

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/carrot.js

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