Cleaned up literal

This commit is contained in:
Rafał Grodziński
2025-07-08 22:41:27 +09:00
parent 22f71bdc33
commit 707a282704
3 changed files with 25 additions and 60 deletions

View File

@@ -5,93 +5,59 @@
#include "Parser/ValueType.h"
shared_ptr<ExpressionLiteral> ExpressionLiteral::expressionLiteralForToken(shared_ptr<Token> token) {
switch (token->getKind()) {
case TokenKind::INTEGER_CHAR: {
string charString = token->getLexme();
optional<int> charValue = Utils::charStringToInt(charString);
if (!charValue)
return nullptr;
shared_ptr<ExpressionLiteral> expression = make_shared<ExpressionLiteral>();
expression->valueType = ValueType::valueTypeForToken(token, nullptr, 0);
expression->sint32Value = *charValue;
return expression;
}
default:
return nullptr;
}
}
shared_ptr<ExpressionLiteral> expression = make_shared<ExpressionLiteral>();
ExpressionLiteral::ExpressionLiteral():
Expression(ExpressionKind::LITERAL, nullptr) { }
ExpressionLiteral::ExpressionLiteral(shared_ptr<Token> 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<int> 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;
}

View File

@@ -12,7 +12,6 @@ private:
public:
static shared_ptr<ExpressionLiteral> expressionLiteralForToken(shared_ptr<Token> token);
ExpressionLiteral(shared_ptr<Token> token);
ExpressionLiteral();
bool getBoolValue();
int32_t getSint32Value();

View File

@@ -460,7 +460,7 @@ shared_ptr<Expression> Parser::matchExpressionLiteral() {
shared_ptr<Token> token = tokens.at(currentIndex);
if (tryMatchingTokenKinds(Token::tokensLiteral, false, true))
return make_shared<ExpressionLiteral>(token);
return ExpressionLiteral::expressionLiteralForToken(token);
return nullptr;
}