diff options
author | VoR0220 <catalanor0220@gmail.com> | 2016-03-30 04:08:51 +0800 |
---|---|---|
committer | VoR0220 <catalanor0220@gmail.com> | 2016-05-10 00:41:02 +0800 |
commit | 4b749fc33335cee2de50e31776ddae1f73649a7b (patch) | |
tree | a8f28bbb649a1f4e80845aaef5a9b34c2f6c4f05 /libsolidity/ast | |
parent | 4d283b2b304f9c9a85fb6f03346cc7c9d0054daa (diff) | |
download | dexon-solidity-4b749fc33335cee2de50e31776ddae1f73649a7b.tar.gz dexon-solidity-4b749fc33335cee2de50e31776ddae1f73649a7b.tar.zst dexon-solidity-4b749fc33335cee2de50e31776ddae1f73649a7b.zip |
changed names for Rational Constants and categories
Diffstat (limited to 'libsolidity/ast')
-rw-r--r-- | libsolidity/ast/Types.cpp | 50 | ||||
-rw-r--r-- | libsolidity/ast/Types.h | 10 |
2 files changed, 32 insertions, 28 deletions
diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index e4f2b035..4c3947df 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -181,8 +181,8 @@ TypePointer Type::forLiteral(Literal const& _literal) case Token::FalseLiteral: return make_shared<BoolType>(); case Token::Number: - if (ConstantNumberType::isValidLiteral(_literal)) - return make_shared<ConstantNumberType>(_literal); + if (RationalNumberType::isValidLiteral(_literal)) + return make_shared<RationalNumberType>(_literal); else return TypePointer(); case Token::StringLiteral: @@ -326,7 +326,7 @@ string IntegerType::toString(bool) const TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const { - if (_other->category() != Category::NumberConstant && _other->category() != category()) + if (_other->category() != Category::RationalNumber && _other->category() != category()) return TypePointer(); auto commonType = dynamic_pointer_cast<IntegerType const>(Type::commonType(shared_from_this(), _other)); @@ -441,7 +441,7 @@ string FixedPointType::toString(bool) const TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const { - if (_other->category() != Category::NumberConstant + if (_other->category() != Category::RationalNumber && _other->category() != category() && _other->category() != Category::Integer ) @@ -459,7 +459,7 @@ TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePoi return commonType; } -bool ConstantNumberType::isValidLiteral(Literal const& _literal) +bool RationalNumberType::isValidLiteral(Literal const& _literal) { try { @@ -495,7 +495,7 @@ bool ConstantNumberType::isValidLiteral(Literal const& _literal) return true; } -ConstantNumberType::ConstantNumberType(Literal const& _literal) +RationalNumberType::RationalNumberType(Literal const& _literal) { rational numerator; rational denominator = bigint(1); @@ -554,7 +554,7 @@ ConstantNumberType::ConstantNumberType(Literal const& _literal) } } -bool ConstantNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const +bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const { if (_convertTo.category() == Category::Integer) { @@ -593,7 +593,7 @@ bool ConstantNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const return false; } -bool ConstantNumberType::isExplicitlyConvertibleTo(Type const& _convertTo) const +bool RationalNumberType::isExplicitlyConvertibleTo(Type const& _convertTo) const { if (m_value.denominator() == 1) { @@ -605,7 +605,7 @@ bool ConstantNumberType::isExplicitlyConvertibleTo(Type const& _convertTo) const return fixType && fixType->isExplicitlyConvertibleTo(_convertTo); } -TypePointer ConstantNumberType::unaryOperatorResult(Token::Value _operator) const +TypePointer RationalNumberType::unaryOperatorResult(Token::Value _operator) const { rational value; switch (_operator) @@ -626,10 +626,10 @@ TypePointer ConstantNumberType::unaryOperatorResult(Token::Value _operator) cons default: return TypePointer(); } - return make_shared<ConstantNumberType>(value); + return make_shared<RationalNumberType>(value); } -TypePointer ConstantNumberType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const +TypePointer RationalNumberType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const { if (_other->category() == Category::Integer) { @@ -649,7 +649,7 @@ TypePointer ConstantNumberType::binaryOperatorResult(Token::Value _operator, Typ else if (_other->category() != category()) return TypePointer(); - ConstantNumberType const& other = dynamic_cast<ConstantNumberType const&>(*_other); + RationalNumberType const& other = dynamic_cast<RationalNumberType const&>(*_other); if (Token::isCompareOp(_operator)) { if (m_value.denominator() == 1) @@ -746,26 +746,26 @@ TypePointer ConstantNumberType::binaryOperatorResult(Token::Value _operator, Typ default: return TypePointer(); } - return make_shared<ConstantNumberType>(value); + return make_shared<RationalNumberType>(value); } } -bool ConstantNumberType::operator==(Type const& _other) const +bool RationalNumberType::operator==(Type const& _other) const { if (_other.category() != category()) return false; - ConstantNumberType const& other = dynamic_cast<ConstantNumberType const&>(_other); + RationalNumberType const& other = dynamic_cast<RationalNumberType const&>(_other); return m_value == other.m_value; } -string ConstantNumberType::toString(bool) const +string RationalNumberType::toString(bool) const { if (m_value.denominator() == 1) return "int_const " + m_value.numerator().str(); return "rational_const " + m_value.numerator().str() + '/' + m_value.denominator().str(); } -u256 ConstantNumberType::literalValue(Literal const*) const +u256 RationalNumberType::literalValue(Literal const*) const { u256 value; // we ignore the literal and hope that the type was correctly determined @@ -780,7 +780,7 @@ u256 ConstantNumberType::literalValue(Literal const*) const return value; } -TypePointer ConstantNumberType::mobileType() const +TypePointer RationalNumberType::mobileType() const { if (m_value.denominator() == 1) { @@ -793,7 +793,7 @@ TypePointer ConstantNumberType::mobileType() const return fixType; } -shared_ptr<IntegerType const> ConstantNumberType::integerType() const +shared_ptr<IntegerType const> RationalNumberType::integerType() const { bigint value = wholeNumbers(); bool negative = (value < 0); @@ -808,7 +808,7 @@ shared_ptr<IntegerType const> ConstantNumberType::integerType() const ); } -shared_ptr<FixedPointType const> ConstantNumberType::fixedPointType() const +shared_ptr<FixedPointType const> RationalNumberType::fixedPointType() const { //do calculations up here bigint integers = wholeNumbers(); @@ -844,10 +844,14 @@ shared_ptr<FixedPointType const> ConstantNumberType::fixedPointType() const } //todo: change name of function -bigint ConstantNumberType::fractionalBitsNeeded() const +bigint RationalNumberType::findFractionNumberAndBits(bool getWholeNumber = false) const { - auto value = m_value - wholeNumbers(); - for (unsigned fractionalBits = 0; value < boost::multiprecision::pow(bigint(2), 256); fractionalBits += 8, value *= 10) + rational value; + if (getWholeNumber) + value = m_value; + else + value = m_value - wholeNumbers(); + for (unsigned fractionalBits = 0; value < boost::multiprecision::pow(bigint(2), 256); fractionalBits += 8, value *= 256) { if (value.denominator() == 1) return value.numerator()/value.denominator(); diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 2039c85e..ef0a69b3 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -135,7 +135,7 @@ class Type: private boost::noncopyable, public std::enable_shared_from_this<Type public: enum class Category { - Integer, NumberConstant, StringLiteral, Bool, FixedPoint, Array, + Integer, RationalNumber, StringLiteral, Bool, FixedPoint, Array, FixedBytes, Contract, Struct, Function, Enum, Tuple, Mapping, TypeType, Modifier, Magic, Module }; @@ -356,17 +356,17 @@ private: * Example expressions: 2, 3.14, 2+10.2, ~10. * There is one distinct type per value. */ -class ConstantNumberType: public Type +class RationalNumberType: public Type { public: - virtual Category category() const override { return Category::NumberConstant; } + virtual Category category() const override { return Category::RationalNumber; } /// @returns true if the literal is a valid integer. static bool isValidLiteral(Literal const& _literal); - explicit ConstantNumberType(Literal const& _literal); - explicit ConstantNumberType(rational _value): + explicit RationalNumberType(Literal const& _literal); + explicit RationalNumberType(rational _value): m_value(_value) {} virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override; |