aboutsummaryrefslogtreecommitdiffstats
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
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
-rw-r--r--Changelog.md1
-rw-r--r--docs/types.rst3
-rw-r--r--libsolidity/ast/Types.cpp115
-rw-r--r--libsolidity/ast/Types.h3
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp33
-rw-r--r--test/libsolidity/SolidityNameAndTypeResolution.cpp24
-rw-r--r--test/libsolidity/SolidityParser.cpp15
-rw-r--r--test/libsolidity/SolidityScanner.cpp17
8 files changed, 159 insertions, 52 deletions
diff --git a/Changelog.md b/Changelog.md
index 69c75615..98be69ce 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -7,6 +7,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.
* Type system: Warn if base of exponentiation is literal (result type might be unexpected).
diff --git a/docs/types.rst b/docs/types.rst
index f89a8ee5..243a9a0c 100644
--- a/docs/types.rst
+++ b/docs/types.rst
@@ -202,6 +202,9 @@ Octal literals do not exist in Solidity and leading zeros are invalid.
Decimal fraction literals are formed by a ``.`` with at least one number on
one side. Examples include ``1.``, ``.1`` and ``1.3``.
+Scientific notation is also supported, where the base can have fractions, while the exponent cannot.
+Examples include ``2e10``, ``-2e10``, ``2e-10``, ``2.5e1``.
+
Number literal expressions retain arbitrary precision until they are converted to a non-literal type (i.e. by
using them together with a non-literal expression).
This means that computations do not overflow and divisions do not truncate
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);
};
/**
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index a4fab721..7ef34383 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -9233,6 +9233,39 @@ BOOST_AUTO_TEST_CASE(revert)
BOOST_CHECK(callContractFunction("a()") == encodeArgs(u256(42)));
}
+BOOST_AUTO_TEST_CASE(scientific_notation)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() returns (uint) {
+ return 2e10 wei;
+ }
+ function g() returns (uint) {
+ return 200e-2 wei;
+ }
+ function h() returns (uint) {
+ return 2.5e1;
+ }
+ function i() returns (int) {
+ return -2e10;
+ }
+ function j() returns (int) {
+ return -200e-2;
+ }
+ function k() returns (int) {
+ return -2.5e1;
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(20000000000)));
+ BOOST_CHECK(callContractFunction("g()") == encodeArgs(u256(2)));
+ BOOST_CHECK(callContractFunction("h()") == encodeArgs(u256(25)));
+ BOOST_CHECK(callContractFunction("i()") == encodeArgs(u256(-20000000000)));
+ BOOST_CHECK(callContractFunction("j()") == encodeArgs(u256(-2)));
+ BOOST_CHECK(callContractFunction("k()") == encodeArgs(u256(-25)));
+}
+
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp
index 97a82f00..fa310434 100644
--- a/test/libsolidity/SolidityNameAndTypeResolution.cpp
+++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp
@@ -2790,18 +2790,6 @@ BOOST_AUTO_TEST_CASE(literal_strings)
CHECK_SUCCESS(text);
}
-BOOST_AUTO_TEST_CASE(invalid_integer_literal_exp)
-{
- char const* text = R"(
- contract Foo {
- function f() {
- var x = 1e2;
- }
- }
- )";
- CHECK_ERROR(text, TypeError, "");
-}
-
BOOST_AUTO_TEST_CASE(memory_structs_with_mappings)
{
char const* text = R"(
@@ -4951,18 +4939,6 @@ BOOST_AUTO_TEST_CASE(external_function_type_to_uint)
CHECK_ERROR(text, TypeError, "Explicit type conversion not allowed");
}
-BOOST_AUTO_TEST_CASE(invalid_fixed_point_literal)
-{
- char const* text = R"(
- contract A {
- function a() {
- .8E0;
- }
- }
- )";
- CHECK_ERROR(text, TypeError, "");
-}
-
BOOST_AUTO_TEST_CASE(shift_constant_left_negative_rvalue)
{
char const* text = R"(
diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp
index e5362e78..ffb4b6f2 100644
--- a/test/libsolidity/SolidityParser.cpp
+++ b/test/libsolidity/SolidityParser.cpp
@@ -1479,6 +1479,21 @@ BOOST_AUTO_TEST_CASE(function_type_state_variable)
BOOST_CHECK(successParse(text));
}
+BOOST_AUTO_TEST_CASE(scientific_notation)
+{
+ char const* text = R"(
+ contract test {
+ uint256 a = 2e10;
+ uint256 b = 2E10;
+ uint256 c = 200e-2;
+ uint256 d = 2E10 wei;
+ uint256 e = 2.5e10;
+ }
+ )";
+ BOOST_CHECK(successParse(text));
+}
+
+
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/test/libsolidity/SolidityScanner.cpp b/test/libsolidity/SolidityScanner.cpp
index 3a5c6f24..020bce7f 100644
--- a/test/libsolidity/SolidityScanner.cpp
+++ b/test/libsolidity/SolidityScanner.cpp
@@ -115,9 +115,21 @@ BOOST_AUTO_TEST_CASE(octal_numbers)
BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
}
+BOOST_AUTO_TEST_CASE(scientific_notation)
+{
+ Scanner scanner(CharStream("var x = 2e10;"));
+ BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Var);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Assign);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.currentLiteral(), "2e10");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Semicolon);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
+}
+
BOOST_AUTO_TEST_CASE(negative_numbers)
{
- Scanner scanner(CharStream("var x = -.2 + -0x78 + -7.3 + 8.9;"));
+ Scanner scanner(CharStream("var x = -.2 + -0x78 + -7.3 + 8.9 + 2e-2;"));
BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Var);
BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier);
BOOST_CHECK_EQUAL(scanner.next(), Token::Assign);
@@ -135,6 +147,9 @@ BOOST_AUTO_TEST_CASE(negative_numbers)
BOOST_CHECK_EQUAL(scanner.next(), Token::Add);
BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
BOOST_CHECK_EQUAL(scanner.currentLiteral(), "8.9");
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Add);
+ BOOST_CHECK_EQUAL(scanner.next(), Token::Number);
+ BOOST_CHECK_EQUAL(scanner.currentLiteral(), "2e-2");
BOOST_CHECK_EQUAL(scanner.next(), Token::Semicolon);
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
}