aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2017-03-15 21:55:45 +0800
committerGitHub <noreply@github.com>2017-03-15 21:55:45 +0800
commit0c8a766146cf1416143a5b1a66050b8d6c233907 (patch)
treed8c307ff316496c26a7dbaf9f5a3a17d240dc030 /libsolidity
parent6258fe71854824fb28edf49bbc244e079c9c4f78 (diff)
parent07d775294b0c3270ee9d7bd496837193919c3d94 (diff)
downloaddexon-solidity-0c8a766146cf1416143a5b1a66050b8d6c233907.tar.gz
dexon-solidity-0c8a766146cf1416143a5b1a66050b8d6c233907.tar.zst
dexon-solidity-0c8a766146cf1416143a5b1a66050b8d6c233907.zip
Merge pull request #1698 from ethereum/exp-notation
Fix scientific notation in number literals
Diffstat (limited to 'libsolidity')
-rw-r--r--libsolidity/ast/Types.cpp115
-rw-r--r--libsolidity/ast/Types.h3
2 files changed, 91 insertions, 27 deletions
diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp
index 0e11c3ec..e7f53422 100644
--- a/libsolidity/ast/Types.cpp
+++ b/libsolidity/ast/Types.cpp
@@ -32,6 +32,7 @@
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
+#include <boost/algorithm/string/predicate.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/adaptor/transformed.hpp>
@@ -571,39 +572,99 @@ TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePoi
return commonType;
}
-tuple<bool, rational> RationalNumberType::isValidLiteral(Literal const& _literal)
+tuple<bool, rational> RationalNumberType::parseRational(string const& _value)
{
- rational x;
+ rational value;
try
{
- rational numerator;
- rational denominator(1);
-
- auto radixPoint = find(_literal.value().begin(), _literal.value().end(), '.');
- if (radixPoint != _literal.value().end())
+ auto radixPoint = find(_value.begin(), _value.end(), '.');
+
+ if (radixPoint != _value.end())
{
if (
- !all_of(radixPoint + 1, _literal.value().end(), ::isdigit) ||
- !all_of(_literal.value().begin(), radixPoint, ::isdigit)
+ !all_of(radixPoint + 1, _value.end(), ::isdigit) ||
+ !all_of(_value.begin(), radixPoint, ::isdigit)
)
return make_tuple(false, rational(0));
- //Only decimal notation allowed here, leading zeros would switch to octal.
+
+ // Only decimal notation allowed here, leading zeros would switch to octal.
auto fractionalBegin = find_if_not(
- radixPoint + 1,
- _literal.value().end(),
+ radixPoint + 1,
+ _value.end(),
[](char const& a) { return a == '0'; }
);
- denominator = bigint(string(fractionalBegin, _literal.value().end()));
+ rational numerator;
+ rational denominator(1);
+
+ denominator = bigint(string(fractionalBegin, _value.end()));
denominator /= boost::multiprecision::pow(
- bigint(10),
- distance(radixPoint + 1, _literal.value().end())
+ bigint(10),
+ distance(radixPoint + 1, _value.end())
);
- numerator = bigint(string(_literal.value().begin(), radixPoint));
- x = numerator + denominator;
+ numerator = bigint(string(_value.begin(), radixPoint));
+ value = numerator + denominator;
}
else
- x = bigint(_literal.value());
+ value = bigint(_value);
+ return make_tuple(true, value);
+ }
+ catch (...)
+ {
+ return make_tuple(false, rational(0));
+ }
+}
+
+tuple<bool, rational> RationalNumberType::isValidLiteral(Literal const& _literal)
+{
+ rational value;
+ try
+ {
+ auto expPoint = find(_literal.value().begin(), _literal.value().end(), 'e');
+ if (expPoint == _literal.value().end())
+ expPoint = find(_literal.value().begin(), _literal.value().end(), 'E');
+
+ if (boost::starts_with(_literal.value(), "0x"))
+ {
+ // process as hex
+ value = bigint(_literal.value());
+ }
+ else if (expPoint != _literal.value().end())
+ {
+ // parse the exponent
+ bigint exp = bigint(string(expPoint + 1, _literal.value().end()));
+
+ if (exp > numeric_limits<int32_t>::max() || exp < numeric_limits<int32_t>::min())
+ return make_tuple(false, rational(0));
+
+ // parse the base
+ tuple<bool, rational> base = parseRational(string(_literal.value().begin(), expPoint));
+ if (!get<0>(base))
+ return make_tuple(false, rational(0));
+ value = get<1>(base);
+
+ if (exp < 0)
+ {
+ exp *= -1;
+ value /= boost::multiprecision::pow(
+ bigint(10),
+ exp.convert_to<int32_t>()
+ );
+ }
+ else
+ value *= boost::multiprecision::pow(
+ bigint(10),
+ exp.convert_to<int32_t>()
+ );
+ }
+ else
+ {
+ // parse as rational number
+ tuple<bool, rational> tmp = parseRational(_literal.value());
+ if (!get<0>(tmp))
+ return tmp;
+ value = get<1>(tmp);
+ }
}
catch (...)
{
@@ -616,33 +677,33 @@ tuple<bool, rational> RationalNumberType::isValidLiteral(Literal const& _literal
case Literal::SubDenomination::Second:
break;
case Literal::SubDenomination::Szabo:
- x *= bigint("1000000000000");
+ value *= bigint("1000000000000");
break;
case Literal::SubDenomination::Finney:
- x *= bigint("1000000000000000");
+ value *= bigint("1000000000000000");
break;
case Literal::SubDenomination::Ether:
- x *= bigint("1000000000000000000");
+ value *= bigint("1000000000000000000");
break;
case Literal::SubDenomination::Minute:
- x *= bigint("60");
+ value *= bigint("60");
break;
case Literal::SubDenomination::Hour:
- x *= bigint("3600");
+ value *= bigint("3600");
break;
case Literal::SubDenomination::Day:
- x *= bigint("86400");
+ value *= bigint("86400");
break;
case Literal::SubDenomination::Week:
- x *= bigint("604800");
+ value *= bigint("604800");
break;
case Literal::SubDenomination::Year:
- x *= bigint("31536000");
+ value *= bigint("31536000");
break;
}
- return make_tuple(true, x);
+ return make_tuple(true, value);
}
bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h
index 0a4878b8..78326aa6 100644
--- a/libsolidity/ast/Types.h
+++ b/libsolidity/ast/Types.h
@@ -416,6 +416,9 @@ public:
private:
rational m_value;
+
+ /// @returns true if the literal is a valid rational number.
+ static std::tuple<bool, rational> parseRational(std::string const& _value);
};
/**