Make binary literals sized like other integer types

This commit is contained in:
Jason Turner 2015-10-03 16:38:41 -06:00
parent 9d18360333
commit beedf13d01
4 changed files with 55 additions and 80 deletions

View File

@ -509,6 +509,8 @@ namespace chaiscript
bootstrap_pod_type<long>("long", m); bootstrap_pod_type<long>("long", m);
bootstrap_pod_type<unsigned int>("unsigned_int", m); bootstrap_pod_type<unsigned int>("unsigned_int", m);
bootstrap_pod_type<unsigned long>("unsigned_long", m); bootstrap_pod_type<unsigned long>("unsigned_long", m);
bootstrap_pod_type<long long>("long_long", m);
bootstrap_pod_type<unsigned long long>("unsigned_long_long", m);
bootstrap_pod_type<size_t>("size_t", m); bootstrap_pod_type<size_t>("size_t", m);
bootstrap_pod_type<char>("char", m); bootstrap_pod_type<char>("char", m);
bootstrap_pod_type<wchar_t>("wchar_t", m); bootstrap_pod_type<wchar_t>("wchar_t", m);

View File

@ -621,8 +621,7 @@ namespace chaiscript
template<typename IntType> static Boxed_Value buildInt(const int base, const std::string &t_val, const bool prefixed)
static Boxed_Value buildInt(const IntType &t_type, const std::string &t_val)
{ {
bool unsigned_ = false; bool unsigned_ = false;
bool long_ = false; bool long_ = false;
@ -649,23 +648,21 @@ namespace chaiscript
} }
} }
std::stringstream ss(t_val.substr(0, i));
ss >> t_type;
std::stringstream testu(t_val.substr(0, i)); const auto val = prefixed?std::string(t_val.begin()+2,t_val.end()):t_val;
uint64_t u;
testu >> t_type >> u;
static_assert(sizeof(long) == sizeof(uint64_t) || sizeof(long) * 2 == sizeof(uint64_t), "Unexpected sizing of integer types");
bool unsignedrequired = false; bool unsignedrequired = false;
try {
auto u = std::stoll(val,nullptr,base);
if ((u >> (sizeof(int) * 8)) > 0) if ((u >> (sizeof(int) * 8)) > 0)
{ {
//requires something bigger than int //requires something bigger than int
long_ = true; long_ = true;
} }
static_assert(sizeof(long) == sizeof(uint64_t) || sizeof(long) * 2 == sizeof(uint64_t), "Unexpected sizing of integer types");
if ((sizeof(long) < sizeof(uint64_t)) if ((sizeof(long) < sizeof(uint64_t))
&& (u >> ((sizeof(uint64_t) - sizeof(long)) * 8)) > 0) && (u >> ((sizeof(uint64_t) - sizeof(long)) * 8)) > 0)
{ {
@ -673,7 +670,6 @@ namespace chaiscript
longlong_ = true; longlong_ = true;
} }
const size_t size = [&]()->size_t{ const size_t size = [&]()->size_t{
if (longlong_) if (longlong_)
{ {
@ -689,12 +685,20 @@ namespace chaiscript
{ {
unsignedrequired = true; unsignedrequired = true;
} }
} catch (const std::out_of_range &) {
// it cannot fit in a signed long long...
std::cout << "forcing long long for '" << val << "'\n";
unsignedrequired = true;
long_ = sizeof(unsigned long long) == sizeof(unsigned long);
longlong_ = sizeof(unsigned long long) != sizeof(unsigned long);
}
if (unsignedrequired && !unsigned_) if (unsignedrequired && !unsigned_)
{ {
if (t_type == &std::hex || t_type == &std::oct) if (base != 10)
{ {
// with hex and octal we are happy to just make it unsigned // with bin, hex and oct we are happy to just make it unsigned
unsigned_ = true; unsigned_ = true;
} else { } else {
// with decimal we must bump it up to the next size // with decimal we must bump it up to the next size
@ -711,32 +715,20 @@ namespace chaiscript
{ {
if (longlong_) if (longlong_)
{ {
uint64_t val; return const_var(stoull(val,nullptr,base));
ss >> val;
return const_var(val);
} else if (long_) { } else if (long_) {
unsigned long val; return const_var(stoul(val,nullptr,base));
ss >> val;
return const_var(val);
} else { } else {
unsigned int val; return const_var(static_cast<unsigned int>(stoul(val,nullptr,base)));
ss >> val;
return const_var(val);
} }
} else { } else {
if (longlong_) if (longlong_)
{ {
int64_t val; return const_var(stoll(val,nullptr,base));
ss >> val;
return const_var(val);
} else if (long_) { } else if (long_) {
long val; return const_var(stol(val,nullptr,base));
ss >> val;
return const_var(val);
} else { } else {
int val; return const_var(stoi(val,nullptr,base));
ss >> val;
return const_var(val);
} }
} }
} }
@ -758,35 +750,15 @@ namespace chaiscript
if (m_position.has_more() && char_in_alphabet(*m_position, detail::float_alphabet) ) { if (m_position.has_more() && char_in_alphabet(*m_position, detail::float_alphabet) ) {
if (Hex_()) { if (Hex_()) {
auto match = Position::str(start, m_position); auto match = Position::str(start, m_position);
auto bv = buildInt(std::hex, match); auto bv = buildInt(16, match, true);
m_match_stack.emplace_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv))); m_match_stack.emplace_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
return true; return true;
} }
if (Binary_()) { if (Binary_()) {
auto match = Position::str(start, m_position); auto match = Position::str(start, m_position);
int64_t temp_int = 0; auto bv = buildInt(2, match, true);
size_t pos = 0; m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
const auto end = match.length();
while ((pos < end) && (pos < (2 + sizeof(int) * 8))) {
temp_int <<= 1;
if (match[pos] == '1') {
temp_int += 1;
}
++pos;
}
Boxed_Value i = [&]()->Boxed_Value{
if (match.length() <= sizeof(int) * 8)
{
return const_var(static_cast<int>(temp_int));
} else {
return const_var(temp_int);
}
}();
m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(i)));
return true; return true;
} }
if (Float_()) { if (Float_()) {
@ -799,11 +771,11 @@ namespace chaiscript
IntSuffix_(); IntSuffix_();
auto match = Position::str(start, m_position); auto match = Position::str(start, m_position);
if (!match.empty() && (match[0] == '0')) { if (!match.empty() && (match[0] == '0')) {
auto bv = buildInt(std::oct, match); auto bv = buildInt(8, match, false);
m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv))); m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
} }
else if (!match.empty()) { else if (!match.empty()) {
auto bv = buildInt(std::dec, match); auto bv = buildInt(10, match, false);
m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv))); m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
} else { } else {
return false; return false;

View File

@ -5,9 +5,10 @@
template<typename T> template<typename T>
bool test_literal(T val, const std::string &str) bool test_literal(T val, const std::string &str)
{ {
std::cout << "Comparing : " << val;
chaiscript::ChaiScript chai; chaiscript::ChaiScript chai;
T val2 = chai.eval<T>(str); T val2 = chai.eval<T>(str);
std::cout << "Comparing : " << val << " " << val2 << '\n'; std::cout << " " << val2 << '\n';
return val == val2; return val == val2;
} }

View File

@ -2,8 +2,8 @@ assert_equal(true, int_type.bare_equal(1.get_type_info()))
assert_equal(true, unsigned_int_type.bare_equal(1u.get_type_info())) assert_equal(true, unsigned_int_type.bare_equal(1u.get_type_info()))
assert_equal(true, unsigned_long_type.bare_equal(1lu.get_type_info())) assert_equal(true, unsigned_long_type.bare_equal(1lu.get_type_info()))
assert_equal(true, long_type.bare_equal(1l.get_type_info())) assert_equal(true, long_type.bare_equal(1l.get_type_info()))
assert_equal(true, int64_t_type.bare_equal(1ll.get_type_info())) assert_equal(true, long_long_type.bare_equal(1ll.get_type_info()))
assert_equal(true, uint64_t_type.bare_equal(1ull.get_type_info())) assert_equal(true, unsigned_long_long_type.bare_equal(1ull.get_type_info()))
assert_equal(true, double_type.bare_equal(1.6.get_type_info())) assert_equal(true, double_type.bare_equal(1.6.get_type_info()))
assert_equal(true, float_type.bare_equal(1.6f.get_type_info())) assert_equal(true, float_type.bare_equal(1.6f.get_type_info()))