Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Implementation of Forth "NUMBER"
- Features:
- o Default base is 10
- o Arbitrary base specifiable like so: 16:deadbeef
- o Case insensitive up through base 36, then case insensitive
- o Handles integers and floating point numbers
- o Base 16 shortcut: x:deadbeef
- o Binary shortcut: b:0010011
- :: 0-9? 48 - 10 .U< 0=; ;;
- :: A-Z? 17 - 27 .U< 0=; 10 + ;;
- :: _a-z? 32 - 27 .U< 0=; 36 + ;;
- :: !0 .C@ 0= 1 ?ERR ;
- :: e? .C@ 101 = 0=; 1+ !0 1 R2+! ;;
- :: :? .C@ 58 = 0=; 1+ >R NIP 0 R> !0 ;
- :: :! .C@++ 58 <> 2 ?ERR 0 SWAP !0 ;
- :: -? .C@ 45 = IF 1+ !0 -1 ;, THEN 1 ;
- :: a->a? OVER 37 < 0=; 35 .> 0=; 26 - ;
- :: <BASE? a->A? OVER .U>= 3 ?ERR
- :: DIGIT .C@++ 0-9? A-Z? _a-z? 4 ERR [[
- :: FOLD OVER >R ROT R! -ROT DIGIT R> SWAP <BASE? SWAP R> * + ROT DROP SWAP ;
- :: POST 0? FOLD ME ;
- :: .POST e? 0? FOLD ME ;
- :: WRAP POST DROP NIP * ;
- :: ILIT? STATE @ 0=; TAIL? OFF ILIT ;
- :: FLIT? STATE @ 0=; TAIL? OFF FLIT ;
- :: FMT SWAP S>F 1.0E1 F** S>F F* ;
- :: OUT SWAP ROT DROP ROT * FMT FLIT? 5 n;
- :: EXP >R -? 10 ROT 0 SWAP WRAP R> - OUT ;
- :: .? .C@ 46 = 0=; 1+ R! .POST DUP R> - EXP ;
- :: !. OVER C@ 46 = 5 ?ERR ;
- :: PRE :? .? 0? FOLD ME ;
- :: BASE? >R OVER C@ = 0=;; NIP 1+ R> SWAP :! ;
- :: BASE 120 16 BASE? 98 2 BASE? 0 SWAP PRE ;
- : CSTR .C@++ OVER + 0 SWAP C! ;
- : NUMBER CSTR !0 -? !. 10 ROT BASE WRAP ILIT? ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement