]> gitweb.factorcode.org Git - factor.git/commitdiff
math.parser: move (format-float) to formatting.private
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 13 Feb 2024 01:44:19 +0000 (17:44 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 13 Feb 2024 01:44:19 +0000 (17:44 -0800)
basis/bootstrap/image/primitives/primitives.factor
basis/bootstrap/primitives.factor
basis/formatting/formatting-tests.factor
basis/formatting/formatting.factor
core/math/parser/parser-tests.factor
core/math/parser/parser.factor

index 74023659ce8fd116a476e82b2ae4592ac4d3045f..96551d5b0c6ca84b45332a59370d3f9d5e426626 100644 (file)
@@ -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"
         {
@@ -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"
         {
index e96940f971d4431a46dda8b60909655e0c25094c..5de21e77561382e974611fdb4687b083894dfe74 100644 (file)
@@ -66,6 +66,7 @@ call( -- ) ! syntax-quot
     "classes.predicate"
     "compiler.units"
     "continuations.private"
+    "formatting.private"
     "generic.single"
     "generic.single.private"
     "growable"
index 99be8c978fbfc68e49950ee7e986d2dc67b2fe87..510a070ae436e34db8417c578e043635e77380cd 100644 (file)
@@ -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
 
index 723783fee635fc99bfe717ff2dc1a8e560228d42..c66f1893c4eeb166e362a311ee39bd034cac108d 100644 (file)
@@ -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
 
@@ -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?
index 13e4514c436d41906e8b027a93f56942030af623..2cd11d8514a66e2fb4ea8a68d8526118eb661d7f 100644 (file)
@@ -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
index 778c754981c151c02527627f753bc75a14bfedd7..65e510bb9801eacccc26ced87e49f0f88fcac0fb 100644 (file)
@@ -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 ] }