Added keywords
This commit is contained in:
@@ -40,6 +40,8 @@ namespace LXC::Internal
|
||||
{ "/", Lexer::Token::Div },
|
||||
{ "%", Lexer::Token::Mod },
|
||||
|
||||
{ "==", Lexer::Token::Eql },
|
||||
|
||||
{ "=", Lexer::Token::Assign },
|
||||
{ ",", Lexer::Token::Comma },
|
||||
|
||||
@@ -52,6 +54,16 @@ namespace LXC::Internal
|
||||
{ ")", Lexer::Token::CloseParen },
|
||||
{ "(", Lexer::Token::OpenParen }
|
||||
};
|
||||
|
||||
static const std::unordered_map<std::string_view, Lexer::Token::TokenType> keywords =
|
||||
{
|
||||
{ "for", Lexer::Token::For },
|
||||
{ "while", Lexer::Token::While },
|
||||
{ "if", Lexer::Token::If },
|
||||
{ "elif", Lexer::Token::ElseIf },
|
||||
{ "else", Lexer::Token::Else },
|
||||
{ "return", Lexer::Token::Return }
|
||||
};
|
||||
}
|
||||
|
||||
namespace LXC::Lexer
|
||||
@@ -113,6 +125,8 @@ namespace LXC::Lexer
|
||||
// Checks for the end of the number literal to create the token //
|
||||
if (!Internal::IsNumeric(next)) _UNLIKELY
|
||||
{
|
||||
|
||||
|
||||
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral);
|
||||
trackers.inNumLiteral = false;
|
||||
}
|
||||
@@ -128,7 +142,12 @@ namespace LXC::Lexer
|
||||
// Checks for the end of the word to create the token //
|
||||
if (!Internal::IsAlpha(next)) _UNLIKELY
|
||||
{
|
||||
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::Identifier);
|
||||
// Finds out if the word is a keyword or not //
|
||||
std::string_view fullWord(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1);
|
||||
auto it = Internal::keywords.find(fullWord);
|
||||
Token::TokenType tType = (it != Internal::keywords.end()) ? it->second : Token::Identifier;
|
||||
|
||||
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), tType);
|
||||
trackers.inIdentifier = false;
|
||||
}
|
||||
}
|
||||
@@ -141,8 +160,10 @@ namespace LXC::Lexer
|
||||
trackers.inSymbolOrOp = true;
|
||||
|
||||
// Checks for the end of the symbol or operator //
|
||||
if (!Internal::IsSymbolOrOperator(next))
|
||||
if (!Internal::IsSymbolOrOperator(next)) _LIKELY
|
||||
{
|
||||
trackers.inSymbolOrOp = false;
|
||||
|
||||
// Finds the operator/symbol if it can //
|
||||
std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1);
|
||||
auto it = Internal::symbolAndOpMap.find(fullSymbol);
|
||||
@@ -155,7 +176,7 @@ namespace LXC::Lexer
|
||||
}
|
||||
|
||||
// === Whitespace === //
|
||||
else if (Internal::IsWhitespace(current)) {}
|
||||
else if (Internal::IsWhitespace(current)) _LIKELY {}
|
||||
|
||||
// If an if-statement has not been triggered the character must be invalid //
|
||||
else
|
||||
|
||||
@@ -45,6 +45,8 @@ namespace LXC::Lexer
|
||||
TOKEN_TYPE_CASE(Token::Div);
|
||||
TOKEN_TYPE_CASE(Token::Mod);
|
||||
|
||||
TOKEN_TYPE_CASE(Token::Eql);
|
||||
|
||||
TOKEN_TYPE_CASE(Token::For);
|
||||
TOKEN_TYPE_CASE(Token::While);
|
||||
TOKEN_TYPE_CASE(Token::If);
|
||||
|
||||
Reference in New Issue
Block a user