From 924a8fff6bc517519eb91ca73adf06505b3f5a87 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 14 Feb 2017 14:24:25 +0000 Subject: Fix exponentional notation in number literals --- Changelog.md | 1 + libsolidity/ast/Types.cpp | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index 212d84fa..4135a3b7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ Features: * Introduce ``.transfer(value)`` for sending Ether. * Code generator: Support ``revert()`` to abort with rolling back, but not consuming all gas. * Inline assembly: Support ``revert`` (EIP140) as an opcode. + * Parser: Support scientific notation in numbers (e.g. ``2e8`` and ``200e-2``). * Type system: Support explicit conversion of external function to address. Bugfixes: diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index d2793b6d..aecf830d 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -576,11 +576,31 @@ tuple RationalNumberType::isValidLiteral(Literal const& _literal rational x; try { - rational numerator; - rational denominator(1); - + auto expPoint = find(_literal.value().begin(), _literal.value().end(), 'e'); + if (expPoint == _literal.value().end()) + expPoint = find(_literal.value().begin(), _literal.value().end(), 'E'); auto radixPoint = find(_literal.value().begin(), _literal.value().end(), '.'); - if (radixPoint != _literal.value().end()) + + if (expPoint != _literal.value().end()) + { + if ( + !all_of(_literal.value().begin(), expPoint, ::isdigit) || + !all_of(expPoint + 1, _literal.value().end(), ::isdigit) + ) + return make_tuple(false, rational(0)); + + bigint exp = bigint(string(expPoint + 1, _literal.value().end())); + + if (exp > numeric_limits::max() || exp < numeric_limits::min()) + return make_tuple(false, rational(0)); + + x = bigint(string(_literal.value().begin(), expPoint)); + x *= boost::multiprecision::pow( + bigint(10), + exp.convert_to() + ); + } + else if (radixPoint != _literal.value().end()) { if ( !all_of(radixPoint + 1, _literal.value().end(), ::isdigit) || @@ -594,6 +614,9 @@ tuple RationalNumberType::isValidLiteral(Literal const& _literal [](char const& a) { return a == '0'; } ); + rational numerator; + rational denominator(1); + denominator = bigint(string(fractionalBegin, _literal.value().end())); denominator /= boost::multiprecision::pow( bigint(10), -- cgit