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