\ prefix 05.09.13 NAB \ Gforth-style numeric prefixes \ for temporarily overriding the \ value of BASE: \ % for binary $ for hex \ & for decimal ' for ASCII \ Loading this file automatically \ adds the prefix capability. \ See the examples at the bottom. needs case needs string module prefix : >prefixed-number ( d1. c-addr1 u1 -- d2. c-addr2 u2 ) over >r 1 /string r> c@ case [char] $ of hex endof [char] % of 2 base ! endof [char] & of decimal endof [char] ' of 2swap 2over chars over + swap do 256 1 m*/ i c@ m+ loop 2swap chars + 0 exit endof >r -1 /string r> endcase [ action-of >number compile, ] ; :noname ( c-addr u ) [ action-of unknown ] literal catch if base @ >r [ ' >prefixed-number ] literal is >number [ action-of unknown ] literal catch [ action-of >number ] literal is >number r> base ! throw then ; is unknown end-module needs tester { decimal -> } { $abc -> 2748 } { $ABC -> $abc } { -$41 -> -65 } { %1001101 -> 77 } { %1001.0001 -> 145. } { 'a -> char a } { 'b -> 98 } { 'AB -> char A 256 * char B + } { 'ab -> char a 256 * char b + } { &905 -> 905 }