esolang

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

Expected: Hello, World!

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.

CodeDoes
textPrint the text to the right; is a newline
↓text and the other seven arrowsPrint in that direction; an arrow alone moves one step
⁵A number; printed, it draws a line of -, |, / or \ that long
P+textPrint without moving the cursor, here in four directions at once
B w h sA box with the string s drawn clockwise around it
UR w hA rectangle of +, - and |
M JMove the cursor; jump to a position
F W ¿ « »For each, while, if, a block
≔ e xAssign to a Greek letter
S NRead 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.
Expected: c b a
Expected: ***** * * *****
Expected: ------

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

Try this

  1. Draw a 4 by 4 square of #.
  2. Print the numbers 1 to 10, one per line.
  3. Hard: draw a diagonal staircase of / five steps high.

Further

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 charcoal

Source

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

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