From c4bd4dc1cc701c757411d69cb000550995dd56bc Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Fri, 16 Dec 2011 17:20:05 -0800 Subject: [PATCH] math.parser: hex> etc. shouldn't take radix prefix Fixes #453 --- core/math/parser/parser-tests.factor | 23 +++++++++++++++++++++++ core/math/parser/parser.factor | 14 +++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/core/math/parser/parser-tests.factor b/core/math/parser/parser-tests.factor index ad396555ac..640f8c509b 100644 --- a/core/math/parser/parser-tests.factor +++ b/core/math/parser/parser-tests.factor @@ -333,3 +333,26 @@ unit-test [ -0x1.0000,0000,0000,2p0 ] [ "-0x1.0000,0000,0000,1,Cp0" string>number ] unit-test [ -0x1.0000,0000,0000,2p0 ] [ "-0x1.0000,0000,0000,1,81p0" string>number ] unit-test [ -0x1.0000,0000,0000,2p0 ] [ "-0x1.0000,0000,0000,1,8000,0000,0000,0001p0" string>number ] unit-test + +! #453 +! hex> dec> oct> bin> shouldn't admit radix prefixes + +[ 0x0b ] [ "0b" hex> ] unit-test +[ 0x0b0 ] [ "0b0" hex> ] unit-test +[ f ] [ "0o0" hex> ] unit-test +[ f ] [ "0x0" hex> ] unit-test + +[ f ] [ "0b" dec> ] unit-test +[ f ] [ "0b0" dec> ] unit-test +[ f ] [ "0o0" dec> ] unit-test +[ f ] [ "0x0" dec> ] unit-test + +[ f ] [ "0b" oct> ] unit-test +[ f ] [ "0b0" oct> ] unit-test +[ f ] [ "0o0" oct> ] unit-test +[ f ] [ "0x0" oct> ] unit-test + +[ f ] [ "0b" bin> ] unit-test +[ f ] [ "0b0" bin> ] unit-test +[ f ] [ "0o0" bin> ] unit-test +[ f ] [ "0x0" bin> ] unit-test diff --git a/core/math/parser/parser.factor b/core/math/parser/parser.factor index bae9f473fe..43abb5a010 100644 --- a/core/math/parser/parser.factor +++ b/core/math/parser/parser.factor @@ -267,12 +267,20 @@ DEFER: @neg-digit [ @pos-first-digit ] } case ; inline +: @first-char-no-radix ( i number-parse n char -- n/f ) + { + { CHAR: - [ [ @neg-digit ] require-next-digit ?neg ] } + { CHAR: + [ [ @pos-digit ] require-next-digit ] } + [ @pos-digit ] + } case ; inline + PRIVATE> -: base> ( str radix -- n/f ) - [ @first-char ] require-next-digit ; +: string>number ( str -- n/f ) + 10 [ @first-char ] require-next-digit ; -: string>number ( str -- n/f ) 10 base> ; inline +: base> ( str radix -- n/f ) + [ @first-char-no-radix ] require-next-digit ; : bin> ( str -- n/f ) 2 base> ; inline : oct> ( str -- n/f ) 8 base> ; inline