Hex literals
This commit is contained in:
@@ -208,10 +208,15 @@ shared_ptr<Token> Lexer::nextToken() {
|
|||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
return token;
|
return token;
|
||||||
|
|
||||||
token = matchInteger();
|
token = matchIntegerDec();
|
||||||
if (token != nullptr)
|
if (token != nullptr)
|
||||||
return token;
|
return token;
|
||||||
|
|
||||||
|
token = matchIntegerHex();
|
||||||
|
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)
|
||||||
@@ -260,17 +265,39 @@ shared_ptr<Token> Lexer::match(TokenKind kind, string lexme, bool needsSeparator
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<Token> Lexer::matchInteger() {
|
shared_ptr<Token> Lexer::matchIntegerDec() {
|
||||||
int nextIndex = currentIndex;
|
int nextIndex = currentIndex;
|
||||||
|
|
||||||
while (nextIndex < source.length() && isDigit(nextIndex))
|
while (nextIndex < source.length() && isDecDigit(nextIndex))
|
||||||
nextIndex++;
|
nextIndex++;
|
||||||
|
|
||||||
if (nextIndex == currentIndex || !isSeparator(nextIndex))
|
if (nextIndex == currentIndex || !isSeparator(nextIndex))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
string lexme = source.substr(currentIndex, nextIndex - currentIndex);
|
string lexme = source.substr(currentIndex, nextIndex - currentIndex);
|
||||||
shared_ptr<Token> token = make_shared<Token>(TokenKind::INTEGER, lexme, currentLine, currentColumn);
|
shared_ptr<Token> token = make_shared<Token>(TokenKind::INTEGER_DEC, lexme, currentLine, currentColumn);
|
||||||
|
advanceWithToken(token);
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
shared_ptr<Token> Lexer::matchIntegerHex() {
|
||||||
|
int nextIndex = currentIndex;
|
||||||
|
|
||||||
|
// match 0x
|
||||||
|
if (nextIndex > source.length()-2)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
if (source.at(nextIndex++) != '0' || source.at(nextIndex++) != 'x')
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
while (nextIndex < source.length() && isHexDigit(nextIndex))
|
||||||
|
nextIndex++;
|
||||||
|
|
||||||
|
if (nextIndex == currentIndex || !isSeparator(nextIndex))
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
string lexme = source.substr(currentIndex, nextIndex - currentIndex);
|
||||||
|
shared_ptr<Token> token = make_shared<Token>(TokenKind::INTEGER_HEX, lexme, currentLine, currentColumn);
|
||||||
advanceWithToken(token);
|
advanceWithToken(token);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
@@ -278,7 +305,7 @@ shared_ptr<Token> Lexer::matchInteger() {
|
|||||||
shared_ptr<Token> Lexer::matchReal() {
|
shared_ptr<Token> Lexer::matchReal() {
|
||||||
int nextIndex = currentIndex;
|
int nextIndex = currentIndex;
|
||||||
|
|
||||||
while (nextIndex < source.length() && isDigit(nextIndex))
|
while (nextIndex < source.length() && isDecDigit(nextIndex))
|
||||||
nextIndex++;
|
nextIndex++;
|
||||||
|
|
||||||
if (nextIndex >= source.length() || source.at(nextIndex) != '.')
|
if (nextIndex >= source.length() || source.at(nextIndex) != '.')
|
||||||
@@ -286,7 +313,7 @@ shared_ptr<Token> Lexer::matchReal() {
|
|||||||
else
|
else
|
||||||
nextIndex++;
|
nextIndex++;
|
||||||
|
|
||||||
while (nextIndex < source.length() && isDigit(nextIndex))
|
while (nextIndex < source.length() && isDecDigit(nextIndex))
|
||||||
nextIndex++;
|
nextIndex++;
|
||||||
|
|
||||||
if (!isSeparator(nextIndex))
|
if (!isSeparator(nextIndex))
|
||||||
@@ -329,11 +356,16 @@ bool Lexer::isWhiteSpace(int index) {
|
|||||||
return character == ' ' || character == '\t';
|
return character == ' ' || character == '\t';
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Lexer::isDigit(int index) {
|
bool Lexer::isDecDigit(int index) {
|
||||||
char character = source.at(index);
|
char character = source.at(index);
|
||||||
return character >= '0' && character <= '9';
|
return character >= '0' && character <= '9';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Lexer::isHexDigit(int index) {
|
||||||
|
char character = source.at(index);
|
||||||
|
return (character >= '0' && character <= '9') || (character >= 'a' && character <= 'f');
|
||||||
|
}
|
||||||
|
|
||||||
bool Lexer::isIdentifier(int index) {
|
bool Lexer::isIdentifier(int index) {
|
||||||
char character = source.at(index);
|
char character = source.at(index);
|
||||||
bool isDigit = character >= '0' && character <= '9';
|
bool isDigit = character >= '0' && character <= '9';
|
||||||
|
|||||||
@@ -18,14 +18,16 @@ private:
|
|||||||
|
|
||||||
shared_ptr<Token> nextToken();
|
shared_ptr<Token> nextToken();
|
||||||
shared_ptr<Token> match(TokenKind kind, string lexme, bool needsSeparator);
|
shared_ptr<Token> match(TokenKind kind, string lexme, bool needsSeparator);
|
||||||
shared_ptr<Token> matchInteger();
|
shared_ptr<Token> matchIntegerDec();
|
||||||
|
shared_ptr<Token> matchIntegerHex();
|
||||||
shared_ptr<Token> matchReal();
|
shared_ptr<Token> matchReal();
|
||||||
shared_ptr<Token> matchIdentifier();
|
shared_ptr<Token> matchIdentifier();
|
||||||
shared_ptr<Token> matchEnd();
|
shared_ptr<Token> matchEnd();
|
||||||
shared_ptr<Token> matchInvalid();
|
shared_ptr<Token> matchInvalid();
|
||||||
|
|
||||||
bool isWhiteSpace(int index);
|
bool isWhiteSpace(int index);
|
||||||
bool isDigit(int index);
|
bool isDecDigit(int index);
|
||||||
|
bool isHexDigit(int index);
|
||||||
bool isIdentifier(int index);
|
bool isIdentifier(int index);
|
||||||
bool isSeparator(int index);
|
bool isSeparator(int index);
|
||||||
void advanceWithToken(shared_ptr<Token> token);
|
void advanceWithToken(shared_ptr<Token> token);
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ vector<TokenKind> Token::tokensBinary = {
|
|||||||
};
|
};
|
||||||
vector<TokenKind> Token::tokensLiteral = {
|
vector<TokenKind> Token::tokensLiteral = {
|
||||||
TokenKind::BOOL,
|
TokenKind::BOOL,
|
||||||
TokenKind::INTEGER,
|
TokenKind::INTEGER_DEC,
|
||||||
|
TokenKind::INTEGER_HEX,
|
||||||
TokenKind::REAL
|
TokenKind::REAL
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -121,8 +122,10 @@ string Token::toString() {
|
|||||||
|
|
||||||
case TokenKind::BOOL:
|
case TokenKind::BOOL:
|
||||||
return "BOOL(" + lexme + ")";
|
return "BOOL(" + lexme + ")";
|
||||||
case TokenKind::INTEGER:
|
case TokenKind::INTEGER_DEC:
|
||||||
return "INTEGER(" + lexme + ")";
|
return "INTEGER_DEC(" + lexme + ")";
|
||||||
|
case TokenKind::INTEGER_HEX:
|
||||||
|
return "INTEGER_HEX(" + lexme + ")";
|
||||||
case TokenKind::REAL:
|
case TokenKind::REAL:
|
||||||
return "REAL(" + lexme + ")";
|
return "REAL(" + lexme + ")";
|
||||||
case TokenKind::IDENTIFIER:
|
case TokenKind::IDENTIFIER:
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ enum class TokenKind {
|
|||||||
RETURN,
|
RETURN,
|
||||||
|
|
||||||
BOOL,
|
BOOL,
|
||||||
INTEGER,
|
INTEGER_DEC,
|
||||||
|
INTEGER_HEX,
|
||||||
REAL,
|
REAL,
|
||||||
IDENTIFIER,
|
IDENTIFIER,
|
||||||
TYPE,
|
TYPE,
|
||||||
|
|||||||
@@ -130,8 +130,12 @@ Expression(ExpressionKind::LITERAL, ValueType::NONE) {
|
|||||||
boolValue = token->getLexme().compare("true") == 0;
|
boolValue = token->getLexme().compare("true") == 0;
|
||||||
valueType = ValueType::BOOL;
|
valueType = ValueType::BOOL;
|
||||||
break;
|
break;
|
||||||
case TokenKind::INTEGER:
|
case TokenKind::INTEGER_DEC:
|
||||||
sint32Value = stoi(token->getLexme());
|
sint32Value = stoi(token->getLexme(), nullptr, 10);
|
||||||
|
valueType = ValueType::SINT32;
|
||||||
|
break;
|
||||||
|
case TokenKind::INTEGER_HEX:
|
||||||
|
sint32Value = stoi(token->getLexme(), nullptr, 16);
|
||||||
valueType = ValueType::SINT32;
|
valueType = ValueType::SINT32;
|
||||||
break;
|
break;
|
||||||
case TokenKind::REAL:
|
case TokenKind::REAL:
|
||||||
|
|||||||
Reference in New Issue
Block a user