2020-03-18 21:52:55 -04:00
|
|
|
( requires core, str )
|
|
|
|
( string being sent to parse routines are always null
|
|
|
|
terminated )
|
2020-03-17 21:44:32 -04:00
|
|
|
|
|
|
|
: (parsec) ( a -- n f )
|
|
|
|
( apostrophe is ASCII 39 )
|
2020-03-18 21:52:55 -04:00
|
|
|
DUP C@ 39 = NOT IF 0 EXIT THEN ( a 0 )
|
2020-04-15 21:29:39 -04:00
|
|
|
DUP 2+ C@ 39 = NOT IF 0 EXIT THEN ( a 0 )
|
2020-03-17 21:44:32 -04:00
|
|
|
( surrounded by apos, good, return )
|
2020-04-15 21:29:39 -04:00
|
|
|
1+ C@ 1 ( n 1 )
|
2020-03-18 21:52:55 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
( returns negative value on error )
|
2020-04-18 09:13:23 -04:00
|
|
|
: _ ( c -- n )
|
2020-03-18 21:52:55 -04:00
|
|
|
( '0' is ASCII 48 )
|
|
|
|
48 -
|
2020-04-18 09:13:23 -04:00
|
|
|
DUP 0< IF EXIT THEN ( bad )
|
2020-03-18 21:52:55 -04:00
|
|
|
DUP 10 < IF EXIT THEN ( good )
|
|
|
|
( 'a' is ASCII 97. 59 = 97 - 48 )
|
|
|
|
49 -
|
2020-04-18 09:13:23 -04:00
|
|
|
DUP 0< IF EXIT THEN ( bad )
|
2020-03-18 21:52:55 -04:00
|
|
|
DUP 6 < IF 10 + EXIT THEN ( good )
|
|
|
|
( bad )
|
|
|
|
255 -
|
|
|
|
;
|
|
|
|
|
|
|
|
: (parseh) ( a -- n f )
|
|
|
|
( '0': ASCII 0x30 'x': 0x78 0x7830: 30768 )
|
|
|
|
DUP @ 30768 = NOT IF 0 EXIT THEN ( a 0 )
|
2020-03-19 16:03:35 -04:00
|
|
|
( We have "0x" prefix )
|
2020-04-15 21:29:39 -04:00
|
|
|
2+
|
2020-03-18 21:52:55 -04:00
|
|
|
( validate slen )
|
|
|
|
DUP SLEN ( a l )
|
2020-04-18 09:13:23 -04:00
|
|
|
DUP NOT IF DROP 0 EXIT THEN ( a 0 )
|
2020-03-18 21:52:55 -04:00
|
|
|
4 > IF DROP 0 EXIT THEN ( a 0 )
|
2020-04-18 09:13:23 -04:00
|
|
|
0 ( a r )
|
2020-03-18 21:52:55 -04:00
|
|
|
BEGIN
|
2020-04-18 09:13:23 -04:00
|
|
|
SWAP C@+ ( r a+1 c )
|
|
|
|
DUP NOT IF 2DROP 1 EXIT THEN ( r, 1 )
|
|
|
|
_ ( r a n )
|
|
|
|
DUP 0< IF ROT 2DROP 0 EXIT THEN ( a 0 )
|
|
|
|
ROT 16 * + ( a r*16+n )
|
2020-03-18 21:52:55 -04:00
|
|
|
AGAIN
|
2020-03-17 21:44:32 -04:00
|
|
|
;
|
|
|
|
|
2020-03-19 16:03:35 -04:00
|
|
|
( returns negative value on error )
|
2020-04-18 09:13:23 -04:00
|
|
|
: _ ( c -- n )
|
2020-03-19 16:03:35 -04:00
|
|
|
( '0' is ASCII 48 )
|
|
|
|
48 -
|
2020-04-18 09:13:23 -04:00
|
|
|
DUP 0< IF EXIT THEN ( bad )
|
2020-03-19 16:03:35 -04:00
|
|
|
DUP 2 < IF EXIT THEN ( good )
|
|
|
|
( bad )
|
|
|
|
255 -
|
|
|
|
;
|
|
|
|
|
|
|
|
: (parseb) ( a -- n f )
|
|
|
|
( '0': ASCII 0x30 'b': 0x62 0x6230: 25136 )
|
|
|
|
DUP @ 25136 = NOT IF 0 EXIT THEN ( a 0 )
|
|
|
|
( We have "0b" prefix )
|
2020-04-15 21:29:39 -04:00
|
|
|
2+
|
2020-03-19 16:03:35 -04:00
|
|
|
( validate slen )
|
|
|
|
DUP SLEN ( a l )
|
|
|
|
DUP 0 = IF DROP 0 EXIT THEN ( a 0 )
|
|
|
|
16 > IF DROP 0 EXIT THEN ( a 0 )
|
|
|
|
0 ( a r )
|
|
|
|
BEGIN
|
2020-04-18 09:13:23 -04:00
|
|
|
SWAP C@+ ( r a+1 c )
|
|
|
|
DUP NOT IF 2DROP 1 EXIT THEN ( r 1 )
|
|
|
|
_ ( r a n )
|
|
|
|
DUP 0< IF ROT 2DROP 0 EXIT THEN ( a 0 )
|
|
|
|
ROT 2 * + ( a r*2+n )
|
2020-03-19 16:03:35 -04:00
|
|
|
AGAIN
|
|
|
|
;
|
|
|
|
|
2020-03-17 21:44:32 -04:00
|
|
|
: (parse) ( a -- n )
|
2020-03-29 09:10:23 -04:00
|
|
|
(parsec) IF EXIT THEN
|
|
|
|
(parseh) IF EXIT THEN
|
|
|
|
(parseb) IF EXIT THEN
|
|
|
|
(parsed) IF EXIT THEN
|
2020-03-17 21:44:32 -04:00
|
|
|
( nothing works )
|
2020-04-09 08:27:14 -04:00
|
|
|
LIT< (wnf) (find) IF EXECUTE ELSE ABORT THEN
|
2020-03-17 21:44:32 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
' (parse) (parse*) !
|