1
0
mirror of synced 2024-11-14 11:07:43 +01:00

patterns: Allow casting integers to str

This commit is contained in:
WerWolv 2021-09-21 23:45:45 +02:00
parent 85b8698e35
commit d1c05174b6
3 changed files with 14 additions and 3 deletions

View File

@ -158,13 +158,13 @@ namespace hex {
inline void trimLeft(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
return !std::isspace(ch) && ch >= 0x20;
}));
}
inline void trimRight(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
return !std::isspace(ch) && ch >= 0x20;
}).base(), s.end());
}

View File

@ -445,6 +445,17 @@ namespace hex::pl {
return new ASTNodeLiteral(u128(char16_t(endianAdjustedValue)));
case Token::ValueType::Boolean:
return new ASTNodeLiteral(bool(endianAdjustedValue));
case Token::ValueType::String:
{
std::string string(sizeof(value), '\x00');
std::memcpy(string.data(), &value, string.size());
hex::trim(string);
if (typePattern->getEndian() != std::endian::native)
std::reverse(string.begin(), string.end());
return new ASTNodeLiteral(string);
}
default:
LogConsole::abortEvaluation(hex::format("cannot cast value to '{}'", Token::getTypeName(type)), this);
}

View File

@ -162,7 +162,7 @@ namespace hex::pl {
ASTNode* Parser::parseCastExpression() {
if (peek(KEYWORD_BE) || peek(KEYWORD_LE) || peek(VALUETYPE_ANY)) {
auto type = parseType();
auto type = parseType(true);
auto builtinType = dynamic_cast<ASTNodeBuiltinType*>(type->getType());
if (builtinType == nullptr)