home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Hall of Fame
/
HallofFameCDROM.cdr
/
prog1
/
4th_86.lzh
/
EVAL.4TH
< prev
next >
Wrap
Text File
|
1988-02-12
|
1KB
|
43 lines
( file: EVAL)
(
Word to convert a string to a number. To test:
" 123" EVAL . " -15" EVAL .
Leading blanks and sign (+ or -) are allowed.
Stops conversion at end of string or on first invalid
digit.
)
: NXTDIG ( count adr -- count-1 adr+1 )
SWAP 1- ( dec count) SWAP 1+ ( inc adr) ;
: EVAL ( stringadr -- number )
0 SWAP 0 SWAP DUPB@ SWAP ( stk: sum, signflag, count, adr)
( skip leading blanks)
REPEAT
OVER 0> OVER 1+ B@ 20H = AND
WHILE ( blank and still some string left)
NXTDIG ( point to next byte and decrement count)
ENDWHILE
1+ ( bump string pointer)
( check for + or - digit)
OVER IF ( count not zero)
DUPB@ IF[ "+" "-" ] ( it is a plus or minus)
DUPB@ "-" = IF ( it is minus)
1 3 POKE ( set sign flag)
THEN
NXTDIG ( skip sign character)
THEN
THEN
( now evaluate digits)
REPEAT
OVER 0> OVER B@ DUP "0" >= SWAP 3AH < AND AND
WHILE ( count <> 0 and valid digit)
DUPB@ "0" - ( get digit again)
5 PICK ( get sum) 10 * + ( digit added in)
4 POKE ( store new sum)
NXTDIG ( dec count and bump adr)
ENDWHILE
DROP ( adr) DROP ( count) IF ( sign flag is set)
-1* ( negate sum)
THEN ; ( return with result on stack)