diff options
| author | Cori Barker <coribarker2@gmail.com> | 2026-04-16 10:50:20 +0100 |
|---|---|---|
| committer | Cori Barker <coribarker2@gmail.com> | 2026-04-16 10:50:20 +0100 |
| commit | 2aa179fdd5033148c885056ea4953eae6426a5b3 (patch) | |
| tree | 0d0f9bf911074318cd1f347983153c76503c0f83 | |
| parent | 32a1609b59fcbbfe24e223c078d51f8bfa08566f (diff) | |
Extended lexer to support all types of tokens needed.
| -rw-r--r-- | include/token_type.hpp | 19 | ||||
| -rw-r--r-- | src/lexer.cpp | 93 |
2 files changed, 103 insertions, 9 deletions
diff --git a/include/token_type.hpp b/include/token_type.hpp index d123aaa..a62369d 100644 --- a/include/token_type.hpp +++ b/include/token_type.hpp @@ -3,20 +3,35 @@ enum class TokenType { INT, STRING, + FUNCTION, NUMBER, IDENTIFIER, + RETURN, + + IF, + WHILE, + FOR, PLUS, MINUS, MULTIPLY, DIVIDE, + EQUAL, + LESS_EQUAL, + GREATER_EQUAL, + LESS, + GREATER, ASSIGN, + ARROW, SEMICOLON, + LEFT_BRACKET, + RIGHT_BRACKET, + LEFT_BRACE, + RIGHT_BRACE, END_OF_FILE, - INVALID - + INVALID, }; diff --git a/src/lexer.cpp b/src/lexer.cpp index 3b134eb..0db900e 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -18,9 +18,39 @@ std::vector<Token> Lexer::tokenise() { } else if (std::isalpha(src[position])) { if (src.substr(position, 3) == "int") { tokens.push_back(Token(TokenType::INT, "int", line, column)); - advance(); - advance(); - advance(); + for (int i=0; i<3; i++) { + advance(); + } + + } else if (src.substr(position, 8) == "function") { + tokens.push_back(Token(TokenType::FUNCTION, "function", line, column)); + for (int i=0; i<8; i++) { + advance(); + } + + } else if (src.substr(position, 6) == "return") { + tokens.push_back(Token(TokenType::RETURN, "return", line, column)); + for (int i=0; i<6; i++) { + advance(); + } + + } else if (src.substr(position, 2) == "if") { + tokens.push_back(Token(TokenType::IF, "if", line, column)); + for (int i=0; i<2; i++) { + advance(); + } + + } else if (src.substr(position, 5) == "while") { + tokens.push_back(Token(TokenType::WHILE, "while", line, column)); + for (int i=0; i<5; i++) { + advance(); + } + + } else if (src.substr(position, 3) == "for") { + tokens.push_back(Token(TokenType::FOR, "for", line, column)); + for (int i=0; i<3; i++) { + advance(); + } } else { std::string identifier; @@ -39,8 +69,15 @@ std::vector<Token> Lexer::tokenise() { advance(); } else if (src[position] == '-') { - tokens.push_back(Token(TokenType::MINUS, std::string(1, src[position]), line, column)); - advance(); + if (src[position + 1] == '>') { + tokens.push_back(Token(TokenType::ARROW, "->", line, column)); + advance(); + advance(); + + } else { + tokens.push_back(Token(TokenType::MINUS, std::string(1, src[position]), line, column)); + advance(); + } } else if (src[position] == '*') { tokens.push_back(Token(TokenType::MULTIPLY, std::string(1, src[position]), line, column)); @@ -51,13 +88,55 @@ std::vector<Token> Lexer::tokenise() { advance(); } else if (src[position] == '=') { - tokens.push_back(Token(TokenType::ASSIGN, std::string(1, src[position]), line, column)); - advance(); + if (src[position + 1] == '=') { + tokens.push_back(Token(TokenType::EQUAL, "==", line, column)); + advance(); + advance(); + } else { + tokens.push_back(Token(TokenType::ASSIGN, std::string(1, src[position]), line, column)); + advance(); + } } else if (src[position] == ';') { tokens.push_back(Token(TokenType::SEMICOLON, std::string(1, src[position]), line, column)); advance(); + } else if (src[position] == '<') { + if (src[position + 1] == '=') { + tokens.push_back(Token(TokenType::LESS_EQUAL, "<=", line, column)); + advance(); + advance(); + } else { + tokens.push_back(Token(TokenType::LESS, std::string(1, src[position]), line, column)); + advance(); + } + + } else if (src[position] == '>') { + if (src[position + 1] == '=') { + tokens.push_back(Token(TokenType::GREATER_EQUAL, ">=", line, column)); + advance(); + advance(); + } else { + tokens.push_back(Token(TokenType::GREATER, std::string(1, src[position]), line, column)); + advance(); + } + + } else if (src[position] == '(') { + tokens.push_back(Token(TokenType::LEFT_BRACKET, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == ')') { + tokens.push_back(Token(TokenType::RIGHT_BRACKET, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == '{') { + tokens.push_back(Token(TokenType::LEFT_BRACE, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == '}') { + tokens.push_back(Token(TokenType::RIGHT_BRACE, std::string(1, src[position]), line, column)); + advance(); + } else { tokens.push_back(Token(TokenType::INVALID, std::string(1, src[position]), line, column)); advance(); |
