home .. forth .. colorforth mail list archive ..

Re: [colorforth] Re: How is colorForth different from other Forths?


On Wednesday 10 December 2003 09:07 pm, Robert Patten wrote:
>      Testing words in  Other Forths  leave a  true/false flag on the
> stack for " if  " and  other branching
> words   to remove and branch on false.
>    In ColorForth   previous words may set/reset,  0 and minus
> processor, flags for " if  "or -if   to branch
> on false.  Or a testing word  that set or reset flag which  " if  " 
> uses to branch on false.   No other
> branching words are necessary.

You have to be careful about this though; this behavior is highly CPU 
dependent.  Currently, you'll find a lot of "<test> drop if" constructs 
in ColorForth's sources.  If, hypothetically, we port ColorForth to the 
6809 or 6502 CPUs, this code will break, because "drop"  will silently 
change CPU flags.

Moreover, even in x86 ColorForth, not all operations are known to set the 
flags.  Occasionally, you'll see the phrase "? if", where ? is a 
compiler word that emits "test eax,eax", an instruction that effectively 
is a no-op except that it sets the sign and zero flags based on what's 
in EAX (the register used to cache the top of stack; for those 
interested, having brought up the 6502 and 6809, it'd map to ORA #$00 in 
these architectures).

>     In ColorForth the new word is findable allowing recursion by
> repeating the name or  a loop if followed
> by ;  A previous word named the same,  in this word list is not

You can also use RECURSE in classical Forth.

: chr
  >r over c@ is-letter? if r> 1+ exit then r> ;

: bump
  >r 1- swap 1+ swap r> ;

: lcnt
  over 0= if nip nip exit then chr bump recurse ;

: letterCount ( caddr u -- n ) \ Find the number of *letters* in string
  0 lcnt ;

Note: I deliberately ignored the use of a FOR or DO loop to prove a 
point.

Given the same tail-call optimization rules that ColorForth has, the code 
the above produces would be correct according to ColorForth semantics.  
Therefore, one doesn't *NEED* to use BEGIN/AGAIN, WHILE/REPEAT, etc. in 
traditional Forth.

What makes their use mandatory in traditional Forth is the traditional 
lack of tail-call optimization.  The above code would almost certainly 
break on most Forths with sufficiently long strings.  It also helps that 
we can use ; instead of EXIT -- less characters to type makes for easier 
code entry, which leads to less bugs (I can't tell you how many times 
I've accidentally entered ezit without realizing it).

> In Forth  words like " .  ." .(  type emit dump " display a historical
> view of portions of memory.
> In colorForth,  any words after the last  executed show   is
> repeatedly shown on the screen, overwriting
> any history,  while the keyboard task accepts keys.

This can be crudely emulated in ANSI Forth if, and only if, the return 
stack truely is a return stack:

: show begin r@ execute pause again ;

: myDisplayTask show page 5 5 at-xy foo @ ." Foo = " . ."    " ;
: anotherTask begin ... 1 foo +! ... pause again ;

( ... somehow start the tasks here ... )

You won't find very many tricks like this used in ANSI Forth, because 
ANSI claims the return stack is "OK" to not hold return addresses.  
Thus, there is no guarantee that it'll hold partial continuations.

You also won't find very many tricks like this used in Scheme either, 
because Scheme does not support direct manipulation of partial 
continuations -- only complete continuations.  So while the above trick 
is possible in Scheme, it'd be horrifyingly inefficient if used in a 
deeply nested function.  Hence, if you want to run tricks like the above 
in Scheme, try as best you can to make it happen closest to global 
scope.

> Character icons are arranged in
> the icon table in Hoffman order to maximize the number of icons that

The correct spelling is Huffman.  And while it's important to note that 
the characters are believed to be in Huffman order, their encoding is 
Shannon, not Huffman.

--
Samuel A. Falvo II


---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com