Added keywords

This commit is contained in:
Pasha Bibko
2025-07-21 18:54:25 +01:00
parent ab5fb4f342
commit d6568a8d2f
4 changed files with 47 additions and 4 deletions

View File

@@ -43,6 +43,8 @@ namespace LXC::Lexer
Div, Div,
Mod, Mod,
Eql,
// === Keywords === // // === Keywords === //
For = TokenClass::Keyword, For = TokenClass::Keyword,

View File

@@ -40,6 +40,8 @@ namespace LXC::Internal
{ "/", Lexer::Token::Div }, { "/", Lexer::Token::Div },
{ "%", Lexer::Token::Mod }, { "%", Lexer::Token::Mod },
{ "==", Lexer::Token::Eql },
{ "=", Lexer::Token::Assign }, { "=", Lexer::Token::Assign },
{ ",", Lexer::Token::Comma }, { ",", Lexer::Token::Comma },
@@ -52,6 +54,16 @@ namespace LXC::Internal
{ ")", Lexer::Token::CloseParen }, { ")", Lexer::Token::CloseParen },
{ "(", Lexer::Token::OpenParen } { "(", 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 namespace LXC::Lexer
@@ -113,6 +125,8 @@ namespace LXC::Lexer
// Checks for the end of the number literal to create the token // // Checks for the end of the number literal to create the token //
if (!Internal::IsNumeric(next)) _UNLIKELY if (!Internal::IsNumeric(next)) _UNLIKELY
{ {
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral); ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral);
trackers.inNumLiteral = false; trackers.inNumLiteral = false;
} }
@@ -128,7 +142,12 @@ namespace LXC::Lexer
// Checks for the end of the word to create the token // // Checks for the end of the word to create the token //
if (!Internal::IsAlpha(next)) _UNLIKELY 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; trackers.inIdentifier = false;
} }
} }
@@ -141,8 +160,10 @@ namespace LXC::Lexer
trackers.inSymbolOrOp = true; trackers.inSymbolOrOp = true;
// Checks for the end of the symbol or operator // // 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 // // Finds the operator/symbol if it can //
std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1); std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1);
auto it = Internal::symbolAndOpMap.find(fullSymbol); auto it = Internal::symbolAndOpMap.find(fullSymbol);
@@ -155,7 +176,7 @@ namespace LXC::Lexer
} }
// === Whitespace === // // === 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 // // If an if-statement has not been triggered the character must be invalid //
else else

View File

@@ -45,6 +45,8 @@ namespace LXC::Lexer
TOKEN_TYPE_CASE(Token::Div); TOKEN_TYPE_CASE(Token::Div);
TOKEN_TYPE_CASE(Token::Mod); TOKEN_TYPE_CASE(Token::Mod);
TOKEN_TYPE_CASE(Token::Eql);
TOKEN_TYPE_CASE(Token::For); TOKEN_TYPE_CASE(Token::For);
TOKEN_TYPE_CASE(Token::While); TOKEN_TYPE_CASE(Token::While);
TOKEN_TYPE_CASE(Token::If); TOKEN_TYPE_CASE(Token::If);

View File

@@ -1 +1,19 @@
FILE 4 CONTENTS "A" GO B HERE 34 += 5 "ELLO THER" int add(int a, int b)
{
return a + b
}
int main(void)
{
int c = add(3, 4)
if (c == 7)
{
return 0
}
# The laws of maths have been broken #
else
{
return 1
}
}