Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix underscore float parsing #596

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions codon/parser/ast/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "expr.h"

#include <algorithm>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -149,9 +151,14 @@ FloatExpr::FloatExpr(double floatValue)
this->floatValue = std::make_unique<double>(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<double>(result);
else
Expand Down
2 changes: 2 additions & 0 deletions test/parser/simplify_expr.codon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading