WSFN (programming language)

From HandWiki

WSFN (Which Stands for Nothing)[1] is an interpreted programming language for controlling robots created by Li-Chen Wang. It was designed to be as small as possible, a "tiny" language,[2] similar to Wang's earlier effort, Palo Alto Tiny BASIC.[3][4] WSFN was first published in Dr. Dobb's Journal in September 1977. The language consists primarily of single-letter commands to tell a robot to move in certain directions, while other commands perform tests or basic mathematical operations. These can be grouped into named macros to produce more complex programs. The original version also included code that simulated the robot as a cursor on the VDM-1 display, or graphically on a Cromemco Dazzler display. This is similar to the turtle graphics added to the Logo programming language in 1969.

Extended WSFN is an implementation created for the Atari 8-bit family of home computers written by Harry Stewart and published by the Atari Program Exchange[5] in 1981. In addition to supporting turtle graphics, it adds a number of commands to control the graphics and sound capabilities of that platform. It was offered as a "beginner's language with emphasis on graphics".[citation needed]

Syntax

WSFN consists of a number of single-letter commands to control the movement of a turtle or robot. Any of these commands can be repeated by prefixing it with a number.[2] For instance, F moves the turtle Forward one step, while 25F moves 25 steps. R and L make the turtle turn one unit to the Right or Left, respectively, and it can also be reset to point North. The step sizes and turn units are defined by the robot hardware, but are set to one pixel and 45 degrees in the turtle graphics versions. Missing in the robot versions, in the computer versions H returns the turtle Home in the center of the screen and C Clears any previous drawing. Thus, one can draw a square with the string:[6]

BCWHN25F2R25F2R25F2R25F

These instructions set the drawing color to Black, Clears the screen (which fills with the current color), sets the color to White, Homes the turtle, resets the turtle to point North, then draws a series of four lines 25 steps long, rotating 90 degrees to the Right between each line. The result is a white square with its lower-left corner in the center of the screen.[6]

Lists of commands can be surrounded with parentheses to create macros. For instance, the same square can be drawn by placing the code to draw one side of the square inside the parentheses, and then calling it four times:[6]

BCWHN4(25F2R)

Macros can be called within other macros. For instance, this code draws a series of eight squares, each offset by 45 degrees, rotating around the center of the screen:[6]

BCWHN8(4(25F2R)R)

Macros can be assigned a name using the Define command (Extended WSFN used = instead). This code defines a macro named "X" to clear the screen and reset the drawing, and another "Z" that draws a square. It then uses these to draw the same rotating square as the example above:[7]

DX(BCWHN)
DZ4(25F2R)
X8(ZR)

WSFN has rudimentary math capabilities consisting of a single accumulator A that can be incremented and decremented with + and -. The letter A can be placed anywhere a number could appear. One can make the series of squares grow larger by incrementing the accumulator 5 times between each step:[2]

DX(BCWHN)
25A
DZ4(AF2R)
X8(Z5+AR)

A side-effect of the syntax is that A- would set the accumulator to zero, because it performs the decrement instruction by the number in A. Likewise, A+ doubles the value in the accumulator.[2]

Program control is equally rudimentary, consisting of a number of commands that handled IF/THEN/ELSE structures. The most basic form is the Test command, which follows one of two paths if the accumulator was greater or equal to zero. For instance, this command causes the turtle to turn 90 degrees left if the accumulator is non-zero, or 45 to the right if it is zero:[2]

T(2L)R

Variations on the T branching construct include ?, which randomly jumps to the first or second branch 50% of the time, and Sensor, which tests if the contact sensor on the robot has been triggered.[8] Extended WSFN modified the S to return the color in front of the turtle, allowing hit detection on previous drawing, and added the Edge test, which jumps to the right side macro if the turtle hit the edge of the drawing area.[9] The original WSFN lacks an equivalent of E, and instead wraps the drawing area so the turtle re-appears on the opposite side of the screen.[2] Extended WSFN supports this style of playfield wrapping as an option.[10]

Because it uses one-letter commands and recursive syntax, WSFN code is exceedingly cryptic. For example, this is a WSFN program to draw Sierpiński curves:[11]

DIT(-I2FI5RG5RI2FI+)2R
DG4F
DY (HN63F2R61FRC4 (2FI))

Note that the definition of the macro "I" includes calls to I within it. This is a key aspect of the WSFN concept; the language is highly recursive in nature, which makes programming self-similar patterns like fractals easy to accomplish in a few lines of code.[11]

A key concept of Extended WSFN is that the keyboard is always active, even while macros are running. This allows keyboard input to interrupt running programs. Using this technique, one can make macros for moving the turtle in certain ways, assign them to letters on the keyboard, and then perform these movements by pressing different keys in succession. This can be aided by adding the Wait command in places to give the user time to respond as the drawing takes place.[12]

Keywords

From the original Dr. Dobbs article.[13]

WSFN keywords
Keyword Description
A Loops by the value of the accumulator (A- sets it to zero, A+ doubles it)
B Set the drawing color to black
C Clear screen (fill with current color)
D Define macro
F Move the turtle forward
H Return the turtle to the home position
L Rotate the turtle to the left
N Point the turtle north (up)
R Rotate the turtle to the right
S Test contact sensor on the robot (IF/THEN/ELSE)
T Test for non-zero accumulator (IF/THEN/ELSE)
W Set the drawing color to white
= Define a macro name
? Random test, like T but follows each branch 50% of the time
+ Increment accumulator
- Decrement accumulator

From the Extended WSFN manual.[14]

Additional/Redefined Extended WSFN keywords
Keyword Description
B Beep
D Pen down
E Tests if the turtle is at the screen edge
P Set the pen color to the value in the accumulator
S Set the accumulator to the color in front of the turtle (Sense, as in the original version)
U Pen up
W Wait one jiffy (​130 of a second)
= Define macro
=# Define variable
# Loops by the value in a variable
* Adding * to the directional commands, *L or *R, changed the angle to 22.5 degrees instead of 45
$ Test joystick against selected direction (IF/THEN/ELSE)
% Read value of paddle controller into accumulator
& Write value in accumulator to selected color register
; Read the direction of the turtle, 0 through 7, into the accumulator
@ Set the accumulator to zero (same as A- in WSFN)

References

  1. The Blue Book for the Atari Computer. WIDL Video. 1983. ISBN 9780684179216. https://books.google.com/books?id=cu66AAAAIAAJ&q=WSFN+%22Which+Stands+for+Nothing%22. 
  2. 2.0 2.1 2.2 2.3 2.4 2.5 Wang 1977, p. 334.
  3. Wang, Li-Chen (May 1976). "Palo Alto Tiny BASIC". Dr. Dobb's Journal 1 (5): 12–25. 
  4. Rauskolb, Robert (December 1976). "Dr. Wang's Palo Alto Tiny BASIC". Interface Age: 92–108. http://www.bitsavers.org/pdf/interfaceAge/197612/092-108.pdf. 
  5. APX listing at atariarchives.org
  6. 6.0 6.1 6.2 6.3 Stewart 1982, p. 4.
  7. Wang 1977, p. 345.
  8. Wang 1977, p. 334,335.
  9. Stewart 1982, p. 10.
  10. Stewart 1982, p. 3.
  11. 11.0 11.1 Wang 1977, p. 336.
  12. Stewart 1982, p. 5.
  13. Wang 1977.
  14. Stewart 1982.

Bibliography