CFRS

From Esolang
(Redirected from CFR)
Jump to navigation Jump to search
CFRS[]
Paradigm(s) imperative
Designed by Susam Pal
Appeared in 2023
Computational class Total
Reference implementation susam.net/cfrs.html
Influenced by Logo, P′′
Influenced FXYT
File extension(s) .cfrs

CFRS[] is an extremely minimal drawing language that consists of only six simple commands:

  • C: Change colour.
  • F: Move forward by one cell and paint the new cell.
  • R: Rotate right (clockwise) by 45°.
  • S: Sleep for 20 ms.
  • [: Begin a repeatable block and continue executing subsequent code.
  • ]: Repeat current repeatable block once more and exit the block.

While the name of this language is CFRS[] in written form, it may be called "CFRS Brackets" in spoken conversations.

Background

CFRS[] is inspired by the educational programming language Logo and the esoteric programming language P′′ (P double prime). Inspired by Logo, CFRS[] has a virtual turtle that moves around on a graphical canvas and paints the canvas as it moves. Inspired by P′′, it has an extremely limited set of commands. CFRS[] is intentionally meant to be hard to write in and hard to read.

The Git project repository at github.com/susam/cfrs introduces the CFRS[] language and provides a web-based implementation written using HTML5 Canvas and JavaScript.

Implementation

A reference implementation is available at susam.net/cfrs.html.

This reference implementation comes with a small collection of demos. Type any digit between 0 and 5 to see the corresponding demo. Typing a digit may require an external keyboard. If there is no external keyboard available, append # and a digit to the page URL with an on-screen keyboard.

Canvas

The drawing canvas is divided into a grid of 256x256 cells. There are 256 rows with 256 cells in each row. Initially, all 65536 cells of the canvas are painted black.

The rows are numbered 0, 1, 2, etc. Similarly, the columns are numbered 0, 1, 2, etc. too. The cell at the top-left corner is at row 0 and column 0. The cell at the bottom-right corner is at row 255 and column 255.

Invisible Virtual Turtle

The CFRS[] commands are described in terms of movement of a virtual turtle and changes in its properties. The virtual turtle is always invisible. It has three properties:

  • Location: The cell where the turtle is currently situated.
  • Heading: The direction for the turtle's next movement if it moves.
  • Colour: The colour to be used for painting the next cell.

The current location, heading, and colour of the turtle are determined by the initial properties and the preceding commands. The initial properties of the turtle are as follows:

  • Location: Row 127 and column 127.
  • Heading: North (up).
  • Colour: White.

Commands

Colour bars drawn in CFRS[]
Overlapping octagons drawn in CFRS[]

This section describes all six commands of CFRS[] in detail.

C

The C command changes the colour used to paint the next cell. A total of 8 colours are supported. They are:

  • Black
  • Blue
  • Green
  • Cyan
  • Red
  • Magenta
  • Yellow
  • White

The initial colour is white. Each C command changes the drawing colour to the next one in the above list. When the current drawing colour is white, C changes the drawing colour to black, i.e., the drawing colour wraps around to black.

F

The F command moves the turtle forward by one cell and paints the cell it moves to with the current drawing colour. The direction of movement is determined by the current heading of the turtle. Each F command moves the turtle by exactly one cell and paints the entire cell it has moved to. This is true for diagonal movements too. For example, if the turtle moves in the northeast direction, it moves from the current cell diagonally to the next cell that is touching the top-right corner of the current cell. It then paints that new cell it has moved to.

When the turtle is at the edge of the canvas and the next command moves the turtle beyond the edge of, the turtle simply wraps around to the opposite edge of the canvas, i.e., the turtle reenters the canvas from the opposite edge.

R

The R command changes the heading of the turtle by rotating it by 1/8th of a complete turn. The initial heading is north (up).

S

The S command makes the turtle sleep for 20 ms. This can be used to produce animation effects.

[

The [ command marks the beginning of a repeatable block. It does not change the properties of the turtle. This is a control flow command that only introduces a new repeatable block without producing any visual side effects.

]

The ] command marks the end of the current repeatable block and repeats that block exactly once. As a result, when the code evaluator enters a block marked with [, the block is executed twice before the evaluator leaves the end of the block marked with ].

Examples

The screenshot of colour bars presented in the previous section is produced with the following CFRS[] code:

[[[[[[[[[[[[[[[FF]]]]]]]RRF[RRR]]]]]]C]]]

The other screenshot consisting of overlapping octagons is produced with this code:

[[[[[[[[[[[[[[F]]]]RCC]]]RR[FFF][RRR][FF]]]]]]]]

Understanding Repeatable Blocks

The following code moves the turtle twice and then rotates it once:

[F]R

The opening square bracket ([) marks the beginning of a block. Then F is executed as usual thereby moving the turtle forward once. Then the closing square bracket (]) repeats the current block thereby executing the F inside the block once more. Finally the evaluator moves ahead to R and rotates the turtle once. Note that the evaluator leaves a block after that block has been repeated twice.

The following code moves the turtle 6 times:

[FFF]

The following command moves the turtle 12 times.

[[FFF]]

The inner block executes FFF twice. The outer block repeats the inner block twice. As a result, the inner block is repeated four times and the turtle moves forward by 12 cells.

Code Normalisation and Validation

The reference implementation performs the following two normalisation steps (in the given order) on the input code before executing the code:

  • Lowercase letters are converted to uppercase.
  • Any character in the input code that does not match a valid CFRS[] command is removed.

The reference implementation generates errors when the following conditions are met:

  • A closing square bracket (]) is encountered that does not have a corresponding open square bracket ([).
  • The length of the code exceeds 256 characters.

When an error is generated, the entire canvas is painted red and the execution halts immediately.