Skip to content

Commit 3f6306e

Browse files
committed
math.parser: hex> etc. shouldn't take radix prefix
Fixes factor#453
1 parent 3bcb62d commit 3f6306e

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

core/math/parser/parser-tests.factor

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,26 @@ unit-test
333333
[ -0x1.0000,0000,0000,2p0 ] [ "-0x1.0000,0000,0000,1,Cp0" string>number ] unit-test
334334
[ -0x1.0000,0000,0000,2p0 ] [ "-0x1.0000,0000,0000,1,81p0" string>number ] unit-test
335335
[ -0x1.0000,0000,0000,2p0 ] [ "-0x1.0000,0000,0000,1,8000,0000,0000,0001p0" string>number ] unit-test
336+
337+
! #453
338+
! hex> dec> oct> bin> shouldn't admit radix prefixes
339+
340+
[ 0x0b ] [ "0b" hex> ] unit-test
341+
[ 0x0b0 ] [ "0b0" hex> ] unit-test
342+
[ f ] [ "0o0" hex> ] unit-test
343+
[ f ] [ "0x0" hex> ] unit-test
344+
345+
[ f ] [ "0b" dec> ] unit-test
346+
[ f ] [ "0b0" dec> ] unit-test
347+
[ f ] [ "0o0" dec> ] unit-test
348+
[ f ] [ "0x0" dec> ] unit-test
349+
350+
[ f ] [ "0b" oct> ] unit-test
351+
[ f ] [ "0b0" oct> ] unit-test
352+
[ f ] [ "0o0" oct> ] unit-test
353+
[ f ] [ "0x0" oct> ] unit-test
354+
355+
[ f ] [ "0b" bin> ] unit-test
356+
[ f ] [ "0b0" bin> ] unit-test
357+
[ f ] [ "0o0" bin> ] unit-test
358+
[ f ] [ "0x0" bin> ] unit-test

core/math/parser/parser.factor

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,20 @@ DEFER: @neg-digit
267267
[ @pos-first-digit ]
268268
} case ; inline
269269

270+
: @first-char-no-radix ( i number-parse n char -- n/f )
271+
{
272+
{ CHAR: - [ [ @neg-digit ] require-next-digit ?neg ] }
273+
{ CHAR: + [ [ @pos-digit ] require-next-digit ] }
274+
[ @pos-digit ]
275+
} case ; inline
276+
270277
PRIVATE>
271278

272-
: base> ( str radix -- n/f )
273-
<number-parse> [ @first-char ] require-next-digit ;
279+
: string>number ( str -- n/f )
280+
10 <number-parse> [ @first-char ] require-next-digit ;
274281

275-
: string>number ( str -- n/f ) 10 base> ; inline
282+
: base> ( str radix -- n/f )
283+
<number-parse> [ @first-char-no-radix ] require-next-digit ;
276284

277285
: bin> ( str -- n/f ) 2 base> ; inline
278286
: oct> ( str -- n/f ) 8 base> ; inline

0 commit comments

Comments
 (0)