diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lexer.cpp | 93 |
1 files changed, 86 insertions, 7 deletions
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(); |
