Starting Forth chapter 1 examples.
Intro to PrimaryColorForth
The goal here is to simply reproduce all of the examples from chapter 1 of Starting Forth in ColorForth.
Washing Machine Example
washer wash spin rinse spin ;
In ColorForth red words signify the start of a new definition. So "washer" is the new procedure. The ";" represents the call return. The green words are procedures that have already been defined. Let's look at the definition of rinse.
rinse faucets open till-full faucets close ;
The definition includes "things" (faucets) and verbs (open and close). The word "till-full" represents a delay loop. After the faucets "open" the word waits "till-full" before they "close".
Note. Unlike traditional Forth words cannot be defined from the command line. All words must be defined in the editor. Then the block containing the source text must be loaded. For example if you defined some new words in block 22 you would type:
22 load
Putting on a show
Forth was originally designed around a TTY display model. But ColorForth uses a GUI display, albeit one that is MUCH simpler than MS Windows, X-Windows or MacIntosh. So getting things displayed on the screen is a bit tricker than traditional Forth, but simpler than raw programming of most GUI operating system. The ColorForth word "show" creates a display task using the words following it.
Consider this definition.
ok show text 45 emit keyboard ;
This creates a new definition "ok" that draws a "*" at the top left corner of the screen. The word "text" moves the cursor to the top left of the screen and sets the drawing color to white. The word "emit" takes a number off the stack and draws the corresponding charecter. It's the same as "emit" in traditional Forth. Note that the charecter set is different from ASCII. The word "keyboard" redraws the keyboard and stack.
Chapter 1 of Starting Forth builds up an example on how to draw the letter "F" as a large charecter using "ASCII art". Here is a walkthrough of that example using colorForth.
The first word "space" draws a blank space on the screen.
space 0 emit ;
The next word "spaces" draws a certain number of spaces. For instance "5 spaces" would draw 5 spaces. Note that the "for..next" loop will be covered in detail later.
spaces for space next ;
The word "star" is similar to "space" but draws a star.
star 45 emit ;
The word "stars is similar to "spaces" but draws a number of stars.
stars for star next ;
The next words define the "F" using combinations of the above defined words. Note that "cr" stands for "carriage return". That simply moves the cursor to the next line.
side star spaces star ;
margin cr 30 spaces ;
blip margin star ;
bar margin 5 stars ;
f bar blip bar blip blip cr ;
ok show text f keyboard ;
After loading the block containing these words and typing "ok" from the command line a large "F" will be displayed on the screen.
The goal here is to simply reproduce all of the examples from chapter 1 of Starting Forth in ColorForth.
Washing Machine Example
washer wash spin rinse spin ;
In ColorForth red words signify the start of a new definition. So "washer" is the new procedure. The ";" represents the call return. The green words are procedures that have already been defined. Let's look at the definition of rinse.
rinse faucets open till-full faucets close ;
The definition includes "things" (faucets) and verbs (open and close). The word "till-full" represents a delay loop. After the faucets "open" the word waits "till-full" before they "close".
Note. Unlike traditional Forth words cannot be defined from the command line. All words must be defined in the editor. Then the block containing the source text must be loaded. For example if you defined some new words in block 22 you would type:
22 load
Putting on a show
Forth was originally designed around a TTY display model. But ColorForth uses a GUI display, albeit one that is MUCH simpler than MS Windows, X-Windows or MacIntosh. So getting things displayed on the screen is a bit tricker than traditional Forth, but simpler than raw programming of most GUI operating system. The ColorForth word "show" creates a display task using the words following it.
Consider this definition.
ok show text 45 emit keyboard ;
This creates a new definition "ok" that draws a "*" at the top left corner of the screen. The word "text" moves the cursor to the top left of the screen and sets the drawing color to white. The word "emit" takes a number off the stack and draws the corresponding charecter. It's the same as "emit" in traditional Forth. Note that the charecter set is different from ASCII. The word "keyboard" redraws the keyboard and stack.
Chapter 1 of Starting Forth builds up an example on how to draw the letter "F" as a large charecter using "ASCII art". Here is a walkthrough of that example using colorForth.
The first word "space" draws a blank space on the screen.
space 0 emit ;
The next word "spaces" draws a certain number of spaces. For instance "5 spaces" would draw 5 spaces. Note that the "for..next" loop will be covered in detail later.
spaces for space next ;
The word "star" is similar to "space" but draws a star.
star 45 emit ;
The word "stars is similar to "spaces" but draws a number of stars.
stars for star next ;
The next words define the "F" using combinations of the above defined words. Note that "cr" stands for "carriage return". That simply moves the cursor to the next line.
side star spaces star ;
margin cr 30 spaces ;
blip margin star ;
bar margin 5 stars ;
f bar blip bar blip blip cr ;
ok show text f keyboard ;
After loading the block containing these words and typing "ok" from the command line a large "F" will be displayed on the screen.
Labels: colorforth
4 Comments:
Thanks, John. This simple-looking example taught me things about the editor that I could not pick up from the archives, as well as illustrating the free-and-easy style of colorforth as programming tool.
Nick Maroudas
Huffman codes in octal binary-triplets are shown on Chuck's website "48 Characters" together with the characters' numerical order in decimal. Thus character 45 (the star) is binary 1 111 101 ie 175 octal. There are more than these 48 characters available (0 to 47 emit), so 48 emit will start you on the capitals. There is a very nice d.i.y block which sets up interactive graphics that allow you to redesign a character.
Nick Maroudas
The character designer is at block 34 - ikon. The first <34 load>
shows the first ikon - a blank of 16 pixels by 24. You can step thru the ikons by pressing + or - on the graphics keyboard. To escape and restore the usual text kbd, press the space bar. From then onward, you enter < ok > to restore the ikon, and < h > to restore the graphic kbd. To redesign an ikon, there are two identical rows of navigator keys: r, l, u & d. The bottom row moves a "cross-hair" grid across the ikon without changing anything. The upper row of keys does an xor on the color of the pixel at the centre of the cross, as it moves across that "target" pixel. The trick is to start with the lower keyrow (to get a target pixel in the centre of your "sights") then move the cross with one of the upper keys to xor the colour of that central pixel.
Nick Maroudas
Nick, thanks for your comments. I'm glad you pointed out the charecter map on Chuck's website. I need to link to that for the tutorial.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home