From 707a28270427fdc4f2b9b8cf72a7ebafb28ab8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Grodzi=C5=84ski?= Date: Tue, 8 Jul 2025 22:41:27 +0900 Subject: [PATCH] Cleaned up literal --- src/Parser/Expression/ExpressionLiteral.cpp | 82 ++++++--------------- src/Parser/Expression/ExpressionLiteral.h | 1 - src/Parser/Parser.cpp | 2 +- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/src/Parser/Expression/ExpressionLiteral.cpp b/src/Parser/Expression/ExpressionLiteral.cpp index a66913c..648046e 100644 --- a/src/Parser/Expression/ExpressionLiteral.cpp +++ b/src/Parser/Expression/ExpressionLiteral.cpp @@ -5,93 +5,59 @@ #include "Parser/ValueType.h" shared_ptr ExpressionLiteral::expressionLiteralForToken(shared_ptr token) { - switch (token->getKind()) { - case TokenKind::INTEGER_CHAR: { - string charString = token->getLexme(); - optional charValue = Utils::charStringToInt(charString); - if (!charValue) - return nullptr; - shared_ptr expression = make_shared(); - expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0); - expression->sint32Value = *charValue; - return expression; - } - default: - return nullptr; - } -} + shared_ptr expression = make_shared(); -ExpressionLiteral::ExpressionLiteral(): -Expression(ExpressionKind::LITERAL, nullptr) { } - -ExpressionLiteral::ExpressionLiteral(shared_ptr token): -Expression(ExpressionKind::LITERAL, nullptr) { switch (token->getKind()) { case TokenKind::BOOL: - boolValue = token->getLexme().compare("true") == 0; - valueType = ValueType::valueTypeForToken(token, nullptr, 0); + expression->boolValue = token->getLexme().compare("true") == 0; + expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0); break; case TokenKind::INTEGER_DEC: { string numString = token->getLexme(); erase(numString, '_'); - sint32Value = stoi(numString, nullptr, 10); - valueType = ValueType::valueTypeForToken(token, nullptr, 0); + expression->sint32Value = stoi(numString, nullptr, 10); + expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0); break; } case TokenKind::INTEGER_HEX: { string numString = token->getLexme(); erase(numString, '_'); - sint32Value = stoi(numString, nullptr, 16); - valueType = ValueType::valueTypeForToken(token, nullptr, 0); + expression->sint32Value = stoi(numString, nullptr, 16); + expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0); break; } case TokenKind::INTEGER_BIN: { string numString = token->getLexme(); erase(numString, '_'); numString = numString.substr(2, numString.size()-1); - sint32Value = stoi(numString, nullptr, 2); - valueType = ValueType::valueTypeForToken(token, nullptr, 0); + expression->sint32Value = stoi(numString, nullptr, 2); + expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0); break; } case TokenKind::INTEGER_CHAR: { string charString = token->getLexme(); - - valueType = ValueType::valueTypeForToken(token, nullptr, 0); - if (charString.length() == 3) { - sint32Value = charString[1]; - } else if (charString.length() == 4 && charString[1] == '\\') { - switch (charString[2]) { - case 'b': - sint32Value = '\b'; - break; - case 'n': - sint32Value = '\n'; - break; - case 't': - sint32Value = '\t'; - break; - case '\\': - sint32Value = '\\'; - break; - case '\'': - sint32Value = '\''; - break; - case '\"': - sint32Value = '\"'; - break; - } - } - break; + optional charValue = Utils::charStringToInt(charString); + if (!charValue) + return nullptr; + + expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0); + expression->sint32Value = *charValue; + return expression; } case TokenKind::REAL: - real32Value = stof(token->getLexme()); - valueType = ValueType::valueTypeForToken(token, nullptr, 0); + expression->real32Value = stof(token->getLexme()); + expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0); break; default: - exit(1); + return nullptr; } + + return expression; } +ExpressionLiteral::ExpressionLiteral(): +Expression(ExpressionKind::LITERAL, nullptr) { } + bool ExpressionLiteral::getBoolValue() { return boolValue; } diff --git a/src/Parser/Expression/ExpressionLiteral.h b/src/Parser/Expression/ExpressionLiteral.h index 6e61279..89b545e 100644 --- a/src/Parser/Expression/ExpressionLiteral.h +++ b/src/Parser/Expression/ExpressionLiteral.h @@ -12,7 +12,6 @@ private: public: static shared_ptr expressionLiteralForToken(shared_ptr token); - ExpressionLiteral(shared_ptr token); ExpressionLiteral(); bool getBoolValue(); int32_t getSint32Value(); diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index 1689305..916b078 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -460,7 +460,7 @@ shared_ptr Parser::matchExpressionLiteral() { shared_ptr token = tokens.at(currentIndex); if (tryMatchingTokenKinds(Token::tokensLiteral, false, true)) - return make_shared(token); + return ExpressionLiteral::expressionLiteralForToken(token); return nullptr; }