From 4cc732bab67f9f42d158ab3d0f0bef045f1fa0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ibrahim=20Numanagic=CC=81?= Date: Tue, 1 Oct 2024 08:30:35 -0700 Subject: [PATCH 1/3] Fix underscore float parsing --- .gitignore | 1 + codon/parser/ast/expr.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 07dda88e..bc00257f 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ jit/codon/version.py temp/ playground/ scratch*.* +/_* diff --git a/codon/parser/ast/expr.cpp b/codon/parser/ast/expr.cpp index ca73724f..7256843f 100644 --- a/codon/parser/ast/expr.cpp +++ b/codon/parser/ast/expr.cpp @@ -149,9 +149,13 @@ FloatExpr::FloatExpr(double floatValue) this->floatValue = std::make_unique(floatValue); } FloatExpr::FloatExpr(const std::string &value, std::string suffix) - : Expr(), value(value), suffix(std::move(suffix)) { + : Expr(), suffix(std::move(suffix)) { + for (auto c : value) + if (c != '_') + this->value += c; double result; - auto r = fast_float::from_chars(value.data(), value.data() + value.size(), result); + auto r = fast_float::from_chars(this->value.data(), + this->value.data() + this->value.size(), result); if (r.ec == std::errc() || r.ec == std::errc::result_out_of_range) floatValue = std::make_unique(result); else From 9769e20d1a50fe291f7190dc14c965c4e565710c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ibrahim=20Numanagic=CC=81?= Date: Tue, 1 Oct 2024 08:36:25 -0700 Subject: [PATCH 2/3] Add tests --- test/parser/simplify_expr.codon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/parser/simplify_expr.codon b/test/parser/simplify_expr.codon index 95ad691e..a79523ac 100644 --- a/test/parser/simplify_expr.codon +++ b/test/parser/simplify_expr.codon @@ -39,6 +39,8 @@ print 1844674407_3709551999 #! integer '18446744073709551999' cannot fit into 64 print 5.15 #: 5.15 print 2e2 #: 200 print 2.e-2 #: 0.02 +print 1_000.0 #: 1000 +print 1_000e9 #: 1e+12 #%% float_suffix,barebones @extend From 54f0e209c14a4113afef425b9f84a12f53f76cd1 Mon Sep 17 00:00:00 2001 From: "A. R. Shajii" Date: Tue, 1 Oct 2024 12:02:02 -0400 Subject: [PATCH 3/3] Update float parsing --- .gitignore | 1 - codon/parser/ast/expr.cpp | 11 +++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index bc00257f..07dda88e 100644 --- a/.gitignore +++ b/.gitignore @@ -68,4 +68,3 @@ jit/codon/version.py temp/ playground/ scratch*.* -/_* diff --git a/codon/parser/ast/expr.cpp b/codon/parser/ast/expr.cpp index 7256843f..9abd16de 100644 --- a/codon/parser/ast/expr.cpp +++ b/codon/parser/ast/expr.cpp @@ -2,6 +2,8 @@ #include "expr.h" +#include +#include #include #include #include @@ -149,10 +151,11 @@ FloatExpr::FloatExpr(double floatValue) this->floatValue = std::make_unique(floatValue); } FloatExpr::FloatExpr(const std::string &value, std::string suffix) - : Expr(), suffix(std::move(suffix)) { - for (auto c : value) - if (c != '_') - this->value += c; + : Expr(), value(), suffix(std::move(suffix)) { + this->value.reserve(value.size()); + std::copy_if(value.begin(), value.end(), std::back_inserter(this->value), + [](char c) { return c != '_'; }); + double result; auto r = fast_float::from_chars(this->value.data(), this->value.data() + this->value.size(), result);