~technomancy/fennel

fix number serialization v1 APPLIED

Andrey Orst: 1
 fix number serialization

 2 files changed, 9 insertions(+), 12 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~technomancy/fennel/patches/19779/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] fix number serialization Export this patch

very small numbers like (* (math.cos 1.5707963267949) 15) horribly
broke previous representation by making it produce numbers like
-0.-5.2372220082647e-14

I wonder if we can just always substitute , to . for now, until some
weird locale pop up that uses another character?
---
 src/fennel/compiler.fnl | 13 +++++--------
 src/fennel/view.fnl     |  8 ++++----
 2 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/fennel/compiler.fnl b/src/fennel/compiler.fnl
index 4e1a5b7..d000a4a 100644
--- a/src/fennel/compiler.fnl
+++ b/src/fennel/compiler.fnl
@@ -476,15 +476,12 @@ if opts contains the nval option."
                (symbol-to-expression ast scope true))]
      (handle-compile-opts [e] parent opts ast))))

;; We can't use tostring on numbers because some locales use , for
;; decimal separators, which will not be accepted by Lua.  Separate
;; guard clause for 0 frac is needed because math.modf returns
;; positive zero and negatife frac for -0.* numbers.
;; We do gsub transformation because some locales use , for
;; decimal separators, which will not be accepted by Lua.
(fn serialize-number [n]
  (match (math.modf n)
    (int 0) (tostring int)
    ((0 frac) ? (< frac 0)) (.. "-0." (: (tostring frac) :gsub "^-?0." ""))
    (int frac) (.. int "." (: (tostring frac) :gsub "^-?0." ""))))
  (pick-values 1
    (-> (tostring n)
        (string.gsub "," "."))))

(fn compile-scalar [ast _scope parent opts]
  (let [serialize (match (type ast)
diff --git a/src/fennel/view.fnl b/src/fennel/view.fnl
index d35113b..8938e23 100644
--- a/src/fennel/view.fnl
+++ b/src/fennel/view.fnl
@@ -180,10 +180,10 @@

(fn number->string [n]
  ;; Transform number to a string without depending on correct `os.locale`
  (match (math.modf n)
    (int 0) (tostring int)
    ((0 frac) ? (< frac 0)) (.. "-0." (: (tostring frac) :gsub "^-?0." ""))
    (int frac) (.. int "." (: (tostring frac) :gsub "^-?0." ""))))
  (pick-values 1
    (-> n
        tostring
        (string.gsub "," "."))))

(fn colon-string? [s]
  ;; Test if given string is valid colon string.
-- 
2.29.2
Andrey Orst <andreyorst@gmail.com> writes: