Charcoal
A golfing language for ASCII art. The program draws on a canvas, and the canvas is the output.
Turing complete: yes · ●●○ moderate
Tested with 16 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
Most golfing languages build a value and print it. Charcoal moves a cursor over a canvas and writes on it, so drawing a box, a diagonal or a spiral costs a few characters. Plain text in the code prints itself to the right of the cursor, which is why Hello, World! is a whole program.
Read the story
Commands are characters outside ASCII, such as arrows, superscript digits, Greek letters for variables and fullwidth capitals such as F for a loop. That leaves every ASCII character free for text. The code page has 256 characters, so each counts as one byte.
The machine
The canvas is unbounded in every direction. At the end it is printed as the rectangle around everything written, with gaps as spaces.
| Code | Does |
|---|---|
text | Print the text to the right; ¶ is a newline |
↓text and the other seven arrows | Print in that direction; an arrow alone moves one step |
⁵ | A number; printed, it draws a line of -, |, / or \ that long |
P+text | Print without moving the cursor, here in four directions at once |
B w h s | A box with the string s drawn clockwise around it |
UR w h | A rectangle of +, - and | |
M J | Move the cursor; jump to a position |
F W ¿ « » | For each, while, if, a block |
≔ e x | Assign to a Greek letter |
S N | Read an input as a string or as a number |
¦ separates two numbers that would otherwise run together, as in B⁵¦⁵*. A loop names its value with the next free letter, starting at ι. Strings between “ and ” are compressed; their characters are the digits of a base 255 number that unpacks into text.
Example
The second program writes abc up and to the left, so the last letter ends in the top left corner.
The second program writes abc up and to the left, so the last letter ends in the top left corner. The third draws a 5 by 3 box of stars. The last reads the numbers 1, 2 and 3 and draws lines that long one after another.
This implementation is a subset, after the interpreter in the somebody1234/Charcoal repository, which TIO runs. It has the canvas, the eight directions, boxes, rectangles, loops, the common operators and four of the six kinds of compressed string. Polygons, reflecting and rotating the canvas, maps and filters, Brotli and LZMA strings and the other rarer commands stop the run with the reason.
Patterns it teaches
- Code golf: a language built for one kind of task, drawing.
- Two-dimensional control flow: the program moves a cursor on a plane.
- Encoding tricks: compressed strings pack text into base 255 digits.
Try this
- Draw a 4 by 4 square of
#. - Print the numbers 1 to 10, one per line.
- Hard: draw a diagonal staircase of
/five steps high.
Further
- The interpreter, in Python, with a wiki of every command
See also
Also in Golfing
Write your own in Run Convert a program from Charcoal
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: charcoal.
Command line
shib run charcoal -e 'WSι' --input 'abc
5
foobar'shib manual charcoalSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/charcoal.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language