Parse char types
This commit is contained in:
@@ -237,6 +237,10 @@ shared_ptr<Token> Lexer::nextToken() {
|
|||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
return token;
|
return token;
|
||||||
|
|
||||||
|
token = matchIntegerChar();
|
||||||
|
if (token != nullptr)
|
||||||
|
return token;
|
||||||
|
|
||||||
// type
|
// type
|
||||||
token = match(TokenKind::TYPE, "bool", true);
|
token = match(TokenKind::TYPE, "bool", true);
|
||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
@@ -354,6 +358,27 @@ shared_ptr<Token> Lexer::matchIntegerBin() {
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shared_ptr<Token> Lexer::matchIntegerChar() {
|
||||||
|
int nextIndex = currentIndex;
|
||||||
|
|
||||||
|
if (currentIndex >= source.size() || source.at(nextIndex) != '\'')
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
bool isClosing = false;
|
||||||
|
do {
|
||||||
|
nextIndex++;
|
||||||
|
isClosing = source.at(nextIndex) == '\'' && source.at(nextIndex - 1) != '\\';
|
||||||
|
} while (nextIndex < source.length() && !isClosing);
|
||||||
|
|
||||||
|
if (!isClosing)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
string lexme = source.substr(currentIndex, nextIndex - currentIndex + 1);
|
||||||
|
shared_ptr<Token> token = make_shared<Token>(TokenKind::INTEGER_CHAR, lexme, currentLine, currentColumn);
|
||||||
|
advanceWithToken(token);
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<Token> Lexer::matchReal() {
|
shared_ptr<Token> Lexer::matchReal() {
|
||||||
int nextIndex = currentIndex;
|
int nextIndex = currentIndex;
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ private:
|
|||||||
shared_ptr<Token> matchIntegerDec();
|
shared_ptr<Token> matchIntegerDec();
|
||||||
shared_ptr<Token> matchIntegerHex();
|
shared_ptr<Token> matchIntegerHex();
|
||||||
shared_ptr<Token> matchIntegerBin();
|
shared_ptr<Token> matchIntegerBin();
|
||||||
|
shared_ptr<Token> matchIntegerChar();
|
||||||
shared_ptr<Token> matchReal();
|
shared_ptr<Token> matchReal();
|
||||||
shared_ptr<Token> matchIdentifier();
|
shared_ptr<Token> matchIdentifier();
|
||||||
shared_ptr<Token> matchEnd();
|
shared_ptr<Token> matchEnd();
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ vector<TokenKind> Token::tokensLiteral = {
|
|||||||
TokenKind::INTEGER_DEC,
|
TokenKind::INTEGER_DEC,
|
||||||
TokenKind::INTEGER_HEX,
|
TokenKind::INTEGER_HEX,
|
||||||
TokenKind::INTEGER_BIN,
|
TokenKind::INTEGER_BIN,
|
||||||
|
TokenKind::INTEGER_CHAR,
|
||||||
TokenKind::REAL
|
TokenKind::REAL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ enum class TokenKind {
|
|||||||
INTEGER_DEC,
|
INTEGER_DEC,
|
||||||
INTEGER_HEX,
|
INTEGER_HEX,
|
||||||
INTEGER_BIN,
|
INTEGER_BIN,
|
||||||
|
INTEGER_CHAR,
|
||||||
REAL,
|
REAL,
|
||||||
IDENTIFIER,
|
IDENTIFIER,
|
||||||
TYPE,
|
TYPE,
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ string Logger::toString(shared_ptr<Token> token) {
|
|||||||
return "INT_HEX(" + token->getLexme() + ")";
|
return "INT_HEX(" + token->getLexme() + ")";
|
||||||
case TokenKind::INTEGER_BIN:
|
case TokenKind::INTEGER_BIN:
|
||||||
return "INT_BIN(" + token->getLexme() + ")";
|
return "INT_BIN(" + token->getLexme() + ")";
|
||||||
|
case TokenKind::INTEGER_CHAR:
|
||||||
|
return "INT_CHAR(" + token->getLexme() + ")";
|
||||||
case TokenKind::REAL:
|
case TokenKind::REAL:
|
||||||
return "REAL(" + token->getLexme() + ")";
|
return "REAL(" + token->getLexme() + ")";
|
||||||
case TokenKind::IDENTIFIER:
|
case TokenKind::IDENTIFIER:
|
||||||
|
|||||||
@@ -32,6 +32,36 @@ Expression(ExpressionKind::LITERAL, ValueType::NONE) {
|
|||||||
valueType = ValueType::SINT32;
|
valueType = ValueType::SINT32;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case TokenKind::INTEGER_CHAR: {
|
||||||
|
string charString = token->getLexme();
|
||||||
|
|
||||||
|
valueType = ValueType::SINT32;
|
||||||
|
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;
|
||||||
|
}
|
||||||
case TokenKind::REAL:
|
case TokenKind::REAL:
|
||||||
real32Value = stof(token->getLexme());
|
real32Value = stof(token->getLexme());
|
||||||
valueType = ValueType::REAL32;
|
valueType = ValueType::REAL32;
|
||||||
|
|||||||
Reference in New Issue
Block a user