Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2021
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. Implementation of Forth "NUMBER"
  2. Features:
  3. o Default base is 10
  4. o Arbitrary base specifiable like so: 16:deadbeef
  5. o Case insensitive up through base 36, then case insensitive
  6. o Handles integers and floating point numbers
  7. o Base 16 shortcut: x:deadbeef
  8. o Binary shortcut: b:0010011
  9.  
  10.  
  11. :: 0-9? 48 - 10 .U< 0=; ;;
  12. :: A-Z? 17 - 27 .U< 0=; 10 + ;;
  13. :: _a-z? 32 - 27 .U< 0=; 36 + ;;
  14. :: !0 .C@ 0= 1 ?ERR ;
  15. :: e? .C@ 101 = 0=; 1+ !0 1 R2+! ;;
  16. :: :? .C@ 58 = 0=; 1+ >R NIP 0 R> !0 ;
  17. :: :! .C@++ 58 <> 2 ?ERR 0 SWAP !0 ;
  18. :: -? .C@ 45 = IF 1+ !0 -1 ;, THEN 1 ;
  19. :: a->a? OVER 37 < 0=; 35 .> 0=; 26 - ;
  20. :: <BASE? a->A? OVER .U>= 3 ?ERR
  21. :: DIGIT .C@++ 0-9? A-Z? _a-z? 4 ERR [[
  22. :: FOLD OVER >R ROT R! -ROT DIGIT R> SWAP <BASE? SWAP R> * + ROT DROP SWAP ;
  23. :: POST 0? FOLD ME ;
  24. :: .POST e? 0? FOLD ME ;
  25. :: WRAP POST DROP NIP * ;
  26. :: ILIT? STATE @ 0=; TAIL? OFF ILIT ;
  27. :: FLIT? STATE @ 0=; TAIL? OFF FLIT ;
  28. :: FMT SWAP S>F 1.0E1 F** S>F F* ;
  29. :: OUT SWAP ROT DROP ROT * FMT FLIT? 5 n;
  30. :: EXP >R -? 10 ROT 0 SWAP WRAP R> - OUT ;
  31. :: .? .C@ 46 = 0=; 1+ R! .POST DUP R> - EXP ;
  32. :: !. OVER C@ 46 = 5 ?ERR ;
  33. :: PRE :? .? 0? FOLD ME ;
  34. :: BASE? >R OVER C@ = 0=;; NIP 1+ R> SWAP :! ;
  35. :: BASE 120 16 BASE? 98 2 BASE? 0 SWAP PRE ;
  36. : CSTR .C@++ OVER + 0 SWAP C! ;
  37. : NUMBER CSTR !0 -? !. 10 ROT BASE WRAP ILIT? ;
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement