Skip to content

Commit

Permalink
math.parser: move (format-float) to formatting.private
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Feb 13, 2024
1 parent 4a21a2c commit fb0342e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 58 deletions.
22 changes: 11 additions & 11 deletions basis/bootstrap/image/primitives/primitives.factor
Expand Up @@ -204,6 +204,17 @@ CONSTANT: all-primitives {
}
}
}
{
"formatting.private"
{
{
"(format-float)" ( n fill width precision format locale -- byte-array )
"format_float"
{ float byte-array fixnum fixnum byte-array byte-array } { byte-array }
make-flushable
}
}
}
{
"generic.single.private"
{
Expand Down Expand Up @@ -393,17 +404,6 @@ CONSTANT: all-primitives {
}
}
}
{
"math.parser.private"
{
{
"(format-float)" ( n fill width precision format locale -- byte-array )
"format_float"
{ float byte-array fixnum fixnum byte-array byte-array } { byte-array }
make-flushable
}
}
}
{
"math.private"
{
Expand Down
1 change: 1 addition & 0 deletions basis/bootstrap/primitives.factor
Expand Up @@ -66,6 +66,7 @@ call( -- ) ! syntax-quot
"classes.predicate"
"compiler.units"
"continuations.private"
"formatting.private"
"generic.single"
"generic.single.private"
"growable"
Expand Down
30 changes: 30 additions & 0 deletions basis/formatting/formatting-tests.factor
Expand Up @@ -4,6 +4,36 @@ USING: calendar formatting kernel literals math math.functions
sequences strings system tools.test ;
IN: formatting.tests

{
B{ 49 46 53 53 69 43 48 53 }
} [
155000.0 B{ 0 } -1 3 B{ 69 0 } B{ 67 0 } (format-float)
] unit-test

{
B{ 32 32 32 32 32 32 32 49 46 53 53 69 43 48 53 }
} [
155000.0 B{ 0 } 15 3 B{ 69 0 } B{ 67 0 } (format-float)
] unit-test

! Missing locale
{ "" } [
33.4 "" 4 4 "f" "missing" format-float
] unit-test

! Literal byte arrays are mutable, so (format-float) isn't foldable.
: trouble ( -- str ba )
155000.0 B{ } -1 3 B{ 69 0 } [
B{ 67 0 } (format-float) >string
] keep ;

{
"1.55E+05"
"1.550e+05"
} [
trouble CHAR: e 0 rot set-nth trouble drop
] unit-test

[ "%s" printf ] must-infer
[ "%s" sprintf ] must-infer

Expand Down
12 changes: 9 additions & 3 deletions basis/formatting/formatting.factor
Expand Up @@ -5,13 +5,20 @@ calendar.private combinators combinators.smart generalizations
io io.streams.string kernel math math.functions math.parser
multiline namespaces peg.ebnf present prettyprint quotations
sequences sequences.generalizations splitting strings unicode ;
FROM: math.parser.private => format-float ;
IN: formatting

ERROR: unknown-format-directive value ;

<PRIVATE

PRIMITIVE: (format-float) ( n fill width precision format locale -- byte-array )

: pad-null ( format -- format )
0 over length 1 + <byte-array> [ copy ] keep ; foldable

: format-float ( n fill width precision format locale -- string )
[ pad-null ] 4dip [ pad-null ] bi@ (format-float) >string ; inline

: compose-all ( seq -- quot )
[ ] [ compose ] reduce ; inline

Expand Down Expand Up @@ -72,8 +79,7 @@ ERROR: unknown-format-directive value ;
[ "e" format-float-fast ] [ format-scientific-simple ] if ;

: format-fast-decimal? ( x digits -- x' digits ? )
over float? [ t ]
[
over float? [ t ] [
2dup
[ drop dup integer? [ abs 53 2^ < ] [ drop f ] if ]
[ over ratio?
Expand Down
30 changes: 0 additions & 30 deletions core/math/parser/parser-tests.factor
Expand Up @@ -482,36 +482,6 @@ unit-test
{ B{ 222 173 190 239 } } [ "deADbeEF" hex-string>bytes ] unit-test
[ "0" hex-string>bytes ] [ invalid-hex-string-length? ] must-fail-with

{
B{ 49 46 53 53 69 43 48 53 }
} [
155000.0 B{ 0 } -1 3 B{ 69 0 } B{ 67 0 } (format-float)
] unit-test

{
B{ 32 32 32 32 32 32 32 49 46 53 53 69 43 48 53 }
} [
155000.0 B{ 0 } 15 3 B{ 69 0 } B{ 67 0 } (format-float)
] unit-test

! Missing locale
{ "" } [
33.4 "" 4 4 "f" "missing" format-float
] unit-test

! Literal byte arrays are mutable, so (format-float) isn't foldable.
: trouble ( -- str ba )
155000.0 B{ } -1 3 B{ 69 0 } [
B{ 67 0 } (format-float) >string
] keep ;

{
"1.55E+05"
"1.550e+05"
} [
trouble CHAR: e 0 rot set-nth trouble drop
] unit-test

{ "143.99999999999997" } [ 0x1.1ffffffffffffp7 number>string ] unit-test
{ "144.0" } [ 0x1.2p7 number>string ] unit-test
{ "144.00000000000003" } [ 0x1.2000000000001p7 number>string ] unit-test
Expand Down
14 changes: 0 additions & 14 deletions core/math/parser/parser.factor
Expand Up @@ -5,20 +5,6 @@ layouts make math math.order math.private sbufs sequences
sequences.private strings ;
IN: math.parser

<PRIVATE
PRIMITIVE: (format-float) ( n fill width precision format locale -- byte-array )

: format-string ( format -- format )
0 suffix >byte-array ; foldable

! Used as primitive for formatting vocabulary
: format-float ( n fill width precision format locale -- string )
[ format-string ] 4dip
[ format-string ] bi@
(format-float) >string ; inline

PRIVATE>

: digit> ( ch -- n )
{
{ [ dup CHAR: 9 <= ] [ CHAR: 0 - dup 0 < [ drop 255 ] when ] }
Expand Down

0 comments on commit fb0342e

Please sign in to comment.