]> gitweb.factorcode.org Git - factor.git/commitdiff
math.parser: allow _ to separate numbers.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 24 Dec 2021 04:29:35 +0000 (20:29 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 24 Dec 2021 04:29:35 +0000 (20:29 -0800)
Better compatibility with python, java, go, etc.

core/math/parser/parser-tests.factor
core/math/parser/parser.factor

index 4235153295dde5637e460e0a3913972a547042b4..cb1b2653bca07f470e26883576f4decef71149f8 100644 (file)
@@ -359,6 +359,11 @@ unit-test
 { f } [ "0b1," string>number ] unit-test
 { f } [ "0o1," string>number ] unit-test
 
+{ f } [ "1_" string>number ] unit-test
+{ 12 } [ "1_2" string>number ] unit-test
+{ f } [ "1_2_" string>number ] unit-test
+{ 123 } [ "1_2_3" string>number ] unit-test
+
 ! #372
 ! hex float requires exponent
 { f } [ "0x1.0" string>number ] unit-test
index 8ac8c8b46d2de8782654b72d8478383cc2633860..d868b2e864ee0a5afdcd3300b50ae6098857d9e4 100644 (file)
@@ -214,10 +214,9 @@ DEFER: @pos-digit
 DEFER: @neg-digit
 
 : @exponent-digit-or-punc ( float-parse i number-parse n char -- float-parse n/f )
-    {
-        { CHAR: , [ [ @exponent-digit ] require-next-digit ] }
-        [ @exponent-digit ]
-    } case ; inline
+    dup ",_" member-eq? [
+        drop [ @exponent-digit ] require-next-digit
+    ] [ @exponent-digit ] if ; inline
 
 : @exponent-digit ( float-parse i number-parse n char -- float-parse n/f )
     { float-parse fixnum number-parse integer fixnum } declare
@@ -246,10 +245,9 @@ DEFER: @neg-digit
     [ exponent-char? [ drop ->exponent ] ] dip if ; inline
 
 : @mantissa-digit-or-punc ( float-parse i number-parse n char -- float-parse n/f )
-    {
-        { CHAR: , [ [ @mantissa-digit ] require-next-digit ] }
-        [ @mantissa-digit ]
-    } case ; inline
+    dup ",_" member-eq? [
+        drop [ @mantissa-digit ] require-next-digit
+    ] [ @mantissa-digit ] if ; inline
 
 : @mantissa-digit ( float-parse i number-parse n char -- float-parse n/f )
     { float-parse fixnum number-parse integer fixnum } declare
@@ -266,11 +264,14 @@ DEFER: @neg-digit
     <float-parse> [ @mantissa-digit ] require-next-digit ?make-float ; inline
 
 : @denom-digit-or-punc ( i number-parse n char -- n/f )
-    {
-        { CHAR: , [ [ @denom-digit ] require-next-digit ] }
-        { CHAR: . [ ->mantissa ] }
-        [ [ @denom-digit ] or-exponent ]
-    } case ; inline
+    dup ",_" member-eq? [
+        drop [ @denom-digit ] require-next-digit
+    ] [
+        {
+            { CHAR: . [ ->mantissa ] }
+            [ [ @denom-digit ] or-exponent ]
+        } case
+    ] if ; inline
 
 : @denom-digit ( i number-parse n char -- n/f )
     { fixnum number-parse integer fixnum } declare
@@ -287,11 +288,14 @@ DEFER: @neg-digit
     @split [ @denom-first-digit ] require-next-digit ?make-ratio ;
 
 : @num-digit-or-punc ( i number-parse n char -- n/f )
-    {
-        { CHAR: , [ [ @num-digit ] require-next-digit ] }
-        { CHAR: / [ ->denominator ] }
-        [ @num-digit ]
-    } case ; inline
+    dup ",_" member-eq? [
+        drop [ @num-digit ] require-next-digit
+    ] [
+        {
+            { CHAR: / [ ->denominator ] }
+            [ @num-digit ]
+        } case
+    ] if ; inline
 
 : @num-digit ( i number-parse n char -- n/f )
     { fixnum number-parse integer fixnum } declare
@@ -302,13 +306,16 @@ DEFER: @neg-digit
     @split [ @num-digit ] require-next-digit ?add-ratio ;
 
 : @pos-digit-or-punc ( i number-parse n char -- n/f )
-    {
-        { CHAR: , [ [ @pos-digit ] require-next-digit ] }
-        { CHAR: + [ ->numerator ] }
-        { CHAR: / [ ->denominator ] }
-        { CHAR: . [ ->mantissa ] }
-        [ [ @pos-digit ] or-exponent ]
-    } case ; inline
+    dup ",_" member-eq? [
+        drop [ @pos-digit ] require-next-digit
+    ] [
+        {
+            { CHAR: + [ ->numerator ] }
+            { CHAR: / [ ->denominator ] }
+            { CHAR: . [ ->mantissa ] }
+            [ [ @pos-digit ] or-exponent ]
+        } case
+    ] if ; inline
 
 : @pos-digit ( i number-parse n char -- n/f )
     { fixnum number-parse integer fixnum } declare
@@ -335,13 +342,16 @@ DEFER: @neg-digit
     } case ; inline
 
 : @neg-digit-or-punc ( i number-parse n char -- n/f )
-    {
-        { CHAR: , [ [ @neg-digit ] require-next-digit ] }
-        { CHAR: - [ ->numerator ] }
-        { CHAR: / [ ->denominator ] }
-        { CHAR: . [ ->mantissa ] }
-        [ [ @neg-digit ] or-exponent ]
-    } case ; inline
+    dup ",_" member-eq? [
+        drop [ @neg-digit ] require-next-digit
+    ] [
+        {
+            { CHAR: - [ ->numerator ] }
+            { CHAR: / [ ->denominator ] }
+            { CHAR: . [ ->mantissa ] }
+            [ [ @neg-digit ] or-exponent ]
+        } case
+    ] if ; inline
 
 : @neg-digit ( i number-parse n char -- n/f )
     { fixnum number-parse integer fixnum } declare