Wednesday, December 27, 2006

Typing ColorForth

I made the following extensions to ColorForth.

s 0 hello , i speak forth

stype 1 + @
type fffffff0 and unpack if emit type ; then drop drop ;

The word "type" takes a packed word and displays it on the screen. Right now it ignores all color tags and just emits charecters using the current foreground color. Note that s is a string variable. Actually it's just a regular variable, but I'm treating it like a string variable. Variables in ColorForth really point directly to block memory. So by adding 1 to the address of s I can get the address of the text for "hello". The word "stype" adds 1 to the address on the stack and then returns data which is treated by "type" as a packed string. So the code:

greet show text s stype ;

Will display "hello" on the screen. Of course that's only part of the phrase. I added a definition to get a word, display it, and add the address for the next word.

nxtw 1 + dup @ type space ;

The word "space" was defined in a previous posting. I then added a word to unpack and display several words of text.

words for nxtw next ;

So the code:

greet show text s 5 words ;

Will display:

hello , i speak forth

I'm looking for better ways to do this. All comments welcome.


Anonymous said...

Can you please describe the "native code generation" calling method?

I am at a loss to find how colorforth uses
the NEXT macro?


johnmdrake said...

I'm not sure what you mean by "native code generation calling method".

As for the "next" macro that is for FOR..NEXT loop. FOR takes a number off of the state and is the beginning of the loop. NEXT decrements the number and jumps to the beginning of the loop.