diff options
2 files changed, 50 insertions, 4 deletions
| -rw-r--r-- | JSImpl/src/Lexer.cpp | 26 | ||||
| -rw-r--r-- | JSImpl/src/main.cpp | 28 |
diff --git a/JSImpl/src/Lexer.cpp b/JSImpl/src/Lexer.cpp index 797af26..d17f593 100644 --- a/JSImpl/src/Lexer.cpp +++ b/JSImpl/src/Lexer.cpp @@ -125,6 +125,19 @@ Token Tokenizer::ProduceToken(TokenType type, IPLString lexeme) return{ type, m_Line, lexeme }; } +namespace +{ +inline bool IsValidIdentifierStartingChar(char c) +{ + return (c >= 'A' && c <= 'z') || c == '_'; +} + +inline bool IsValidIdentifierChar(char c) +{ + return IsValidIdentifierStartingChar(c) || (c >= '0' && c <= '9'); +} +} + Token Tokenizer::NextToken() { if (m_Code[m_Current] == '\0') @@ -192,7 +205,20 @@ Token Tokenizer::NextToken() { return Token{ word->second, m_Line, key }; } + // It's not a key word so we must revert current counter + m_Current = start; + } + // Identifier + if (IsValidIdentifierStartingChar(c)) + { + auto start = m_Current; + m_Current++; + while (IsValidIdentifierChar(m_Code[m_Current])) + { + ++m_Current; + } + return Token{ TokenType::Identifier, m_Line, IPLString(m_Code + start, m_Code + m_Current) }; } return Token{ TokenType::Eof, m_Line, "" }; }
\ No newline at end of file diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp index 8452e72..1b345c3 100644 --- a/JSImpl/src/main.cpp +++ b/JSImpl/src/main.cpp @@ -1,6 +1,7 @@ #include "Lexer.h" #include <iostream> #define LOG(msg) std::cout << msg << std::endl +#define EXECUTE_TEST(test) std::cout << #test << " "; test(); void TestLess() { auto tokens = Tokenize("<"); @@ -53,11 +54,30 @@ void TestKeyWord() } } +void TestVariableDeclaration() +{ + auto tokens = Tokenize("var pesho = 10"); + if (tokens.size() == 5 && tokens[0].Type == TokenType::Var + && tokens[1].Type == TokenType::Identifier + && tokens[2].Type == TokenType::Equal + && tokens[3].Type == TokenType::Number + && tokens[4].Type == TokenType::Eof) + { + LOG("PASS"); + } + else + { + LOG("FAIL"); + } +} + int main() { - TestLess(); - TestNumber(); - TestString(); - TestKeyWord(); + EXECUTE_TEST(TestLess); + EXECUTE_TEST(TestNumber); + EXECUTE_TEST(TestString); + EXECUTE_TEST(TestKeyWord); + EXECUTE_TEST(TestVariableDeclaration); + std::system("pause"); return 0; }
\ No newline at end of file |
