Преглед изворни кода

fix: allow numbers to have commas in calc

Sebastian Bensusan пре 4 година
родитељ
комит
a357d7d983

+ 1 - 1
src/main/frontend/extensions/calc.cljc

@@ -27,7 +27,7 @@
 
 (defn eval* [env ast]
   (insta/transform
-   {:number     edn/read-string
+   {:number     (comp edn/read-string #(str/replace % "," ""))
     :scientific edn/read-string
     :expr       identity
     :add        +

+ 1 - 1
src/main/grammar/calc.bnf

@@ -19,7 +19,7 @@ acos = <#'\s*'> <'acos('> expr <')'> <#'\s*'>
 asin = <#'\s*'> <'asin('> expr <')'> <#'\s*'>
 <term> = scientific | number | variable | <#'\s*'> <'('> expr <')'> <#'\s*'>
 scientific = #'\s*[0-9]+\.?[0-9]*(e|E)-?[0-9]+()\s*'
-number = #'\s*[0-9]+\.?[0-9]*()\s*'
+number = #'\s*\d+(,\d+)*(\.\d*)?\s*'
 variable = #'\s*[a-zA-Z]+(\_+[a-zA-Z]+)*\s*'
 toassign = #'\s*[a-zA-Z]+(\_+[a-zA-Z]+)*\s*'
 assignment = toassign <#'\s*'> <'='> <#'\s*'> expr

+ 10 - 1
src/test/frontend/extensions/calc_test.cljc

@@ -14,7 +14,15 @@
       98123      "98123"
       1.0        " 1.0 "
       22.1124131 "22.1124131"
-      100.01231  " 100.01231 "))
+      100.01231  " 100.01231 ")
+    (testing "even when they have the commas in the wrong place"
+      (are [value expr] (= value (run expr))
+        98123      "9812,3"
+        98123      "98,123"
+        98123      "9,8,123"
+        1123.0     " 112,3.0 "
+        22.1124131 "2,2.1124131"
+        100.01231  " 1,00.01231 ")))
   (testing "basic operations work"
     (are [value expr] (= value (run expr))
       1             "1 + 0"
@@ -23,6 +31,7 @@
       3             " 1 +2 "
       1             "(2-1 ) "
       211           "100  + 111"
+      2111          "1,000  + 11,11"
       0             "1 + 2 + 3 + 4 + 5 -1-2-3-4-5"
       1             "1 * 1"
       2             "1*2"