diff --git a/Lexer/inc/Token.h b/Lexer/inc/Token.h index d44a738..6852465 100644 --- a/Lexer/inc/Token.h +++ b/Lexer/inc/Token.h @@ -43,6 +43,8 @@ namespace LXC::Lexer Div, Mod, + Eql, + // === Keywords === // For = TokenClass::Keyword, diff --git a/Lexer/src/Lexer.cpp b/Lexer/src/Lexer.cpp index d78f520..13491ed 100644 --- a/Lexer/src/Lexer.cpp +++ b/Lexer/src/Lexer.cpp @@ -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 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 diff --git a/Lexer/src/Token.cpp b/Lexer/src/Token.cpp index 562f8cd..51f0a36 100644 --- a/Lexer/src/Token.cpp +++ b/Lexer/src/Token.cpp @@ -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); diff --git a/example/example.lx b/example/example.lx index dcb0a0f..e81e235 100644 --- a/example/example.lx +++ b/example/example.lx @@ -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 + } +}