Wednesday, December 27, 2006

Typing ColorForth...version 3.0

Cliche of the day: The third time's a charm

I've now extended my ColorForth text display library to the place where it's as "non-tedious" to use as possible. The first step was to be able to unpack and "type" a word rather than having to look up ColorForth charecter codes and emit them one at a time. The second step was to be able to print multiple words at one shot. That was in version 2.0. But the problem there was that I either had to put the number of words on the stack, or store them in the string variable itself. Now, after some brainstorming on the c4th IRC channel, I've come up with working code that figures out for itself how many words to type. Here it is.

s 0 hello , i speak forth
type fffffff0 and unpack if emit type ; then drop drop ;
nextw dup @ type space 1 + ;
tag f and ;
?ncom tag -9 + drop ;
print 1 +
ploop dup @ ?ncom if drop ; then nextw ploop ;
greet show text s print ;

A few notes here. Due to the way "print" works I couldn't use the old "nxtw" definition. So I modified it and changed the name. Also note, for those obsessing over lines of code (despite the fact that just about everyone knows that's no longer a good measure of program size, complexity or "tediousness") the above can be "compressed" into less lines, though IMO at the cost of readability.

s 0 hello , i speak forth
type fffffff0 and unpack if emit type ; then drop drop ;
print dup @ f and -9 + drop if drop then ; dup @ type space 1 + print ;
greet show text s 1 + print ;

There you have it. Four lines of ColorForth. (Or 3 lines depending upon where you put your variable.)

type fffffff0 and unpack if emit type ; then drop drop ; s 0 hello , i speak forth
print dup @ f and -9 + drop if drop then ; dup @ type space 1 + print ;
greet show text s 1 + print ;

And for the "ultra minimalist" (who doesn't want to bother with the tedium of looking up ColorForth charecter codes) you can do a simple "hello world" in 2 lines.

type fffffff0 and unpack if emit type ; then drop drop ; s 0 hello world
ok show text s dup push 1 + @ type 0 emit pop 2 + @ type ;

But that code isn't very robust. What happens when the string you want to type grows from 2 words to 6 as in "hello world, what a lovely day"? Still, that beats the pants off of emitting each charecter one at a time.

Anyway, this is the last I'm do anything with "hello world" programs for a while. I've got enough now to finish all of the examples in Starting Forth (chapter 1 anyway) with a minimum of tedium. This does remind me of how Chuck Moore answered the question of "hello world" programs on the ColorForth mailing list.
I've noticed the interest people have in getting colorForth to say "Hello
World".

The facility for doing that is extremely simple. Find an empty block. Edit
"Hello World" at the top.

I know that's not quite what they have in mind. But it's the way I'd
generate any sort of display. Format a block with the editor. Fill in
whatever numbers or graphics are required.
Gotta love it! "Hello world" in a single line and no need for tedious charecter code look ups and emits! Honestly though, that's what got me (and others) thinking about the "unpack" method of displaying text. The ColorForth to my understanding works by unpacking and displaying all of the 32 bit words in a block of code. But it's smart enough to understand compile words, macros, comments, numbers ect and choose the correct color and font. Right now the method I'm using only recognizes comments from non comments so it can't properly handle capitalization. But it should be intuitively obvious to the casual observer how to fix that. Perhaps I'll leave that as a "student exercise". :)

11 comments:

Anonymous said...

I noticed a bug in your posted code. In the ploop word, move the then behind the first semicolon.

johnmdrake said...

Thanks for the comment. I'll fix it!

Josef said...

I tried doing the "hello , i speak colorforth" program. It will print out the message, but then it seems colorforth freezes. I've doubled and tripled checked the words. I am using colorforth for windows.

Any thoughts?

johnmdrake said...

You have to re-enter the editor by typing "e [space]". You could "fix" this by changing the last line to:

: greet show text s print keyboard ;

And even better improvement is:

: greet show black screen text s print keyboard ;

That clears the screen before displaying the message.

Josef said...
This comment has been removed by the author.
Josef said...

That works great.

screen takes an agrument (in this case black) and changes the background to that color?

I did not see a ; after print. Is print exited by the last ; in ploop?

On a different note, I would like to help with the Primary ColorForth effort. I gave a shot at making Jeff Fox's editor page to HTML 4.01 Strict. I used a modified CSS sheet from Chuck Shattuck's colorforth page. I was hoping you would check them out.

johnmdrake said...

You are correct in your surmises about "screen" as well as "print" and "ploop". ColorForth allows multiple entry as well as exit points. Or what I call "fall through factoring". I'll check out what you did with the editor page. All help is GREATLY appreciated.

Josef said...

I just loaded the editor page. I think the style gives it a bit more colorforth feel. The css jazz is embedded at the top. If you like this style I will make the style sheet later.

Anonymous said...

Dear John,

I would like to use Chuck Moore's method inside a program. Chuck says:

Find an empty block. Edit
"Hello World" at the top.

I know that's not quite what they have in mind. But it's the way I'd
generate any sort of display. Format a block with the editor. Fill in
whatever numbers or graphics are required."

I am running a music program in real time. The score is CF source code, in full color CF blocks. Chuck's method enables me to view the next "page" of score as each new block of music plays. My problem is: I call up each new "page" block by adding the words "blk fetch 2+ edit" as Chuck suggests - but now I am in Edit and the music stops. To restart the music I have to leave Edit by pressing the space bar. In other words, I have to act like the man who turns the pages for the pianist.

Is there any way I could use Chuck's method by calling up the Display part of Edit, without leaving a realtime program?

Cheers

Nick Maroudas

Anonymous said...

Since yesterday's post I have examined Howerd Oakford's CFdos4 (at Inventio.com) because Howerd ported Chuck's editor to CF. On block 220 of CFdos4 there is a word "t" which displays the Edit block (jblk @) when you call "ok". If I substitute my variable (myblk @) I can display my pages of "score" in full color by storing their block numbers in myblk and calling ok from outside the Editor. Chuck's cheeky suggestion for howto setup a full color HelloWorld looks very promising for my realtime music score - once I learn how to graft Howerd's code from CFdos4 onto Chuck's
latest CF (the 16bit version which I got from Josh at Qualdan).

Cheers

Nick Maroudas

Raystm2 said...
This comment has been removed by the author.