diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6ef218 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea + diff --git a/main b/main new file mode 100755 index 0000000..4dbfaee Binary files /dev/null and b/main differ diff --git a/src/main.cpp b/src/main.cpp index 4850ead..e768a83 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -217,11 +217,14 @@ class Interpreter { vector tokens; Logger log; int tokenIndex = 0; - Token consume() { - return tokens.at(tokenIndex++); + optional consume() { + tokenIndex++; + if (tokens.size() > tokenIndex) return tokens.at(tokenIndex); + else return {}; } - Token peek(int index = 1) { - return tokens.at(tokenIndex + index); + optional peek(int index = 1) { + if (tokens.size() > tokenIndex + index) return tokens.at(tokenIndex + index); + else return {}; } public: @@ -229,14 +232,18 @@ class Interpreter { tokens = tokenList; log.debug("Alright we got " + to_string(tokens.size()) + " tokens"); while (tokenIndex < tokens.size()) { - Token currentToken = consume(); + if (peek().has_value()) Token currentToken = consume().value(); + else break; int peekAhead = 1; vector currentInstruction; - while (peek(peekAhead).keyword != keywords::SEMICOLON) { - raise(SIGINT); - currentInstruction.push_back(peek(peekAhead)); - peekAhead ++; + if (peek(peekAhead).has_value()) while (peek(peekAhead).value().keyword != keywords::SEMICOLON) { + if (peek(peekAhead).has_value()) { + currentInstruction.push_back(peek(peekAhead).value()); + peekAhead ++; + } else break; } + consume(); + } } }; diff --git a/test.mx b/test.mx index 7cabac1..70b2168 100644 --- a/test.mx +++ b/test.mx @@ -1,3 +1,3 @@ fun int main() { - return 1; + return 1; }