From 37ff25a907e8eecf8223f999533ed2aa9e0cee0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ibrahim=20Numanagi=C4=87?= Date: Tue, 1 Oct 2024 21:35:11 +0200 Subject: [PATCH] Fix underscore float parsing (#596) * Fix underscore float parsing * Add tests * Update float parsing --------- Co-authored-by: A. R. Shajii --- codon/parser/ast/expr.cpp | 11 +++++++++-- test/parser/simplify_expr.codon | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/codon/parser/ast/expr.cpp b/codon/parser/ast/expr.cpp index ca73724f..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,9 +151,14 @@ 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(), 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(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 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