Sunday, May 16, 2010

Parsing Prefixes

Luke (docl in the #retro irc channel) has contributed an update to the prefix handling code that adds support for parsing prefixes. This allows for some interesting stuff we couldn't do before:

  : __$ ( $-n ) hex >number decimal ;
  : __% ( $-n ) binary >number decimal ;

These allow for parsing numbers as hex or binary, based on a prefix. Luke has gone much further though:

{
   variable end
   : pad 1024 here + ;
   : keep ( - ) @compiler 0; drop keepString literal, ;
   : >pad ( $-$ ) pad over getLength 1+ copy pad keep ;
   : >$ ( n- ) !@end end ++ ;
   : "? ( -f ) @end @1- char: " = ;
   : chop ( $-$ ) end -- 0 !@end ;
   : "; ( $-$ ) "? if chop >pad rdrop ;then ;
   : __" ( "-a )
     dup dup getLength + !end
     "; 32 >$ repeat "; key dup emit >$ again ; parsing prefix
}

With this, we can do strings without a leading space:

  : hello ( - ) "hello, world!" type cr ;

Luke's __" is now part of RetroForth 10.6. I've also added __$, __#, __%, and __' (parsing hex, decimal, binary, and single characters). This restores more of the functionality dropped between 9.3 and 10.0, and adds some new features to the table.

No comments: