parent
540066318c
commit
62f9bf097b
|
@ -59,30 +59,6 @@ template<class T> std::string toString(T t) {
|
|||
return ostr.str();
|
||||
}
|
||||
|
||||
long long stoll(const std::string &s)
|
||||
{
|
||||
long long ret;
|
||||
bool hex = (s.compare(0, 2, "0x") == 0);
|
||||
std::istringstream istr(hex ? s.substr(2) : s);
|
||||
if (hex)
|
||||
istr >> std::hex;
|
||||
istr >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
unsigned long long stoull(const std::string &s)
|
||||
{
|
||||
unsigned long long ret;
|
||||
bool hex = (s.compare(0, 2, "0x") == 0);
|
||||
std::istringstream istr(hex ? s.substr(2) : s);
|
||||
if (hex)
|
||||
istr >> std::hex;
|
||||
istr >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2) {
|
||||
return tok1 && tok2 && tok1->location.sameline(tok2->location);
|
||||
}
|
||||
|
@ -498,12 +474,12 @@ void simplecpp::TokenList::constFoldMulDivRem(Token *tok) {
|
|||
|
||||
long long result;
|
||||
if (tok->op == '*')
|
||||
result = (stoll(tok->previous->str) * stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) * std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else if (tok->op == '/' || tok->op == '%') {
|
||||
long long rhs = stoll(tok->next->str);
|
||||
long long rhs = std::strtoll(tok->next->str.c_str(),0,0);
|
||||
if (rhs == 0)
|
||||
throw std::overflow_error("division/modulo by zero");
|
||||
long long lhs = stoll(tok->previous->str);
|
||||
long long lhs = std::strtoll(tok->previous->str.c_str(),0,0);
|
||||
if (rhs == -1 && lhs == std::numeric_limits<long long>::min())
|
||||
throw std::overflow_error("division overflow");
|
||||
if (tok->op == '/')
|
||||
|
@ -530,9 +506,9 @@ void simplecpp::TokenList::constFoldAddSub(Token *tok) {
|
|||
|
||||
long long result;
|
||||
if (tok->op == '+')
|
||||
result = stoll(tok->previous->str) + stoll(tok->next->str);
|
||||
result = std::strtoll(tok->previous->str.c_str(),0,0) + std::strtoll(tok->next->str.c_str(),0,0);
|
||||
else if (tok->op == '-')
|
||||
result = stoll(tok->previous->str) - stoll(tok->next->str);
|
||||
result = std::strtoll(tok->previous->str.c_str(),0,0) - std::strtoll(tok->next->str.c_str(),0,0);
|
||||
else
|
||||
continue;
|
||||
|
||||
|
@ -554,17 +530,17 @@ void simplecpp::TokenList::constFoldComparison(Token *tok) {
|
|||
|
||||
int result;
|
||||
if (tok->str == "==")
|
||||
result = (stoll(tok->previous->str) == stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) == std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else if (tok->str == "!=")
|
||||
result = (stoll(tok->previous->str) != stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) != std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else if (tok->str == ">")
|
||||
result = (stoll(tok->previous->str) > stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) > std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else if (tok->str == ">=")
|
||||
result = (stoll(tok->previous->str) >= stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) >= std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else if (tok->str == "<")
|
||||
result = (stoll(tok->previous->str) < stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) < std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else if (tok->str == "<=")
|
||||
result = (stoll(tok->previous->str) <= stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) <= std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else
|
||||
continue;
|
||||
|
||||
|
@ -588,11 +564,11 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
|
|||
continue;
|
||||
long long result;
|
||||
if (tok->op == '&')
|
||||
result = (stoll(tok->previous->str) & stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) & std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else if (tok->op == '^')
|
||||
result = (stoll(tok->previous->str) ^ stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) ^ std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else /*if (tok->op == '|')*/
|
||||
result = (stoll(tok->previous->str) | stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) | std::strtoll(tok->next->str.c_str(),0,0));
|
||||
tok = tok->previous;
|
||||
tok->setstr(toString(result));
|
||||
deleteToken(tok->next);
|
||||
|
@ -612,9 +588,9 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) {
|
|||
|
||||
int result;
|
||||
if (tok->str == "||")
|
||||
result = (stoll(tok->previous->str) || stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) || std::strtoll(tok->next->str.c_str(),0,0));
|
||||
else /*if (tok->str == "&&")*/
|
||||
result = (stoll(tok->previous->str) && stoll(tok->next->str));
|
||||
result = (std::strtoll(tok->previous->str.c_str(),0,0) && std::strtoll(tok->next->str.c_str(),0,0));
|
||||
|
||||
tok = tok->previous;
|
||||
tok->setstr(toString(result));
|
||||
|
@ -1140,7 +1116,7 @@ void simplifyNumbers(simplecpp::TokenList &expr) {
|
|||
if (tok->str.size() == 1U)
|
||||
continue;
|
||||
if (tok->str.compare(0,2,"0x") == 0)
|
||||
tok->setstr(toString(stoull(tok->str)));
|
||||
tok->setstr(toString(::strtoull(tok->str.substr(2).c_str(), NULL, 16)));
|
||||
else if (tok->str[0] == '\'')
|
||||
tok->setstr(toString(tok->str[1] & 0xffU));
|
||||
}
|
||||
|
@ -1152,7 +1128,7 @@ long long evaluate(simplecpp::TokenList expr, const std::map<std::string, std::s
|
|||
simplifyNumbers(expr);
|
||||
expr.constFold();
|
||||
// TODO: handle invalid expressions
|
||||
return expr.cbegin() && expr.cbegin() == expr.cend() && expr.cbegin()->number ? stoll(expr.cbegin()->str) : 0LL;
|
||||
return expr.cbegin() && expr.cbegin() == expr.cend() && expr.cbegin()->number ? std::strtoll(expr.cbegin()->str.c_str(),0,0) : 0LL;
|
||||
}
|
||||
|
||||
const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok) {
|
||||
|
|
Loading…
Reference in New Issue