mirror of
https://github.com/hsoft/collapseos.git
synced 2024-11-27 12:28:06 +11:00
icore: extract "_pdacc" from "(parsed)"
Makes boot binary a bit bigger, but that "_pdacc" word will be reused in high level apps.
This commit is contained in:
parent
79acf92b28
commit
c40f336959
Binary file not shown.
@ -52,37 +52,52 @@
|
|||||||
: < CMP -1 = ;
|
: < CMP -1 = ;
|
||||||
: > CMP 1 = ;
|
: > CMP 1 = ;
|
||||||
|
|
||||||
|
( r c -- r f )
|
||||||
|
( Parse digit c and accumulate into result r.
|
||||||
|
Flag f is 0 when c was a valid digit, 1 when c was WS,
|
||||||
|
-1 when c was an invalid digit. )
|
||||||
|
: _pdacc
|
||||||
|
DUP 0x21 < IF DROP 1 EXIT THEN
|
||||||
|
( parse char )
|
||||||
|
'0' -
|
||||||
|
( if bad, return "r -1" )
|
||||||
|
DUP 0 < IF DROP -1 EXIT THEN ( bad )
|
||||||
|
DUP 9 > IF DROP -1 EXIT THEN ( bad )
|
||||||
|
( good, add to running result )
|
||||||
|
SWAP 10 * + ( r*10+n )
|
||||||
|
0 ( good )
|
||||||
|
;
|
||||||
|
|
||||||
: (parsed) ( a -- n f )
|
: (parsed) ( a -- n f )
|
||||||
( read first char outside of the loop. it *has* to be
|
( read first char outside of the loop. it *has* to be
|
||||||
nonzero. )
|
nonzero. )
|
||||||
DUP C@ ( a c )
|
DUP C@ ( a c )
|
||||||
DUP NOT IF EXIT THEN ( a 0 )
|
|
||||||
( special case: do we have a negative? )
|
( special case: do we have a negative? )
|
||||||
DUP '-' = IF
|
DUP '-' = IF
|
||||||
( Oh, a negative, let's recurse and reverse )
|
( Oh, a negative, let's recurse and reverse )
|
||||||
DROP 1 + ( a+1 )
|
DROP 1 + ( a+1 )
|
||||||
(parsed) ( n f )
|
(parsed) ( n f )
|
||||||
SWAP 0 SWAP ( f 0 n )
|
0 ROT ( f 0 n )
|
||||||
- SWAP EXIT ( 0-n f )
|
- SWAP EXIT ( 0-n f )
|
||||||
THEN
|
THEN
|
||||||
( running result, staring at zero )
|
( running result from first char )
|
||||||
0 SWAP ( a r c )
|
0 SWAP ( a r c )
|
||||||
( Loop over chars )
|
_pdacc ( a r f )
|
||||||
BEGIN
|
DUP IF
|
||||||
( parse char )
|
( first char was not a valid digit )
|
||||||
'0' -
|
2DROP 0 EXIT ( a 0 )
|
||||||
( if bad, return "a 0" )
|
THEN
|
||||||
DUP 0 < IF 2DROP 0 EXIT THEN ( bad )
|
BEGIN ( a r 0 )
|
||||||
DUP 9 > IF 2DROP 0 EXIT THEN ( bad )
|
DROP SWAP 1 + ( r a+1 )
|
||||||
( good, add to running result )
|
DUP C@ ( r a c )
|
||||||
SWAP 10 * + ( a r*10+n )
|
ROT SWAP ( a r c )
|
||||||
SWAP 1 + SWAP ( a+1 r )
|
_pdacc ( a r f )
|
||||||
( read next char )
|
DUP UNTIL
|
||||||
OVER C@
|
( a r f -- f is 1 on success, -1 on error, normalize
|
||||||
DUP NOT UNTIL
|
to bool. )
|
||||||
( we're done and it's a success. We have "a r c", we want
|
1 = ( a r f )
|
||||||
"r 1". )
|
( we want "r f" )
|
||||||
DROP SWAP DROP 1
|
ROT DROP
|
||||||
;
|
;
|
||||||
|
|
||||||
( This is only the "early parser" in earlier stages. No need
|
( This is only the "early parser" in earlier stages. No need
|
||||||
|
Loading…
Reference in New Issue
Block a user