From 6f5805128491f2ee25da43a67995b7440cf30f5d Mon Sep 17 00:00:00 2001 From: haxala1r Date: Tue, 30 Sep 2025 17:52:53 +0300 Subject: [PATCH] Add new lex() function to streamline lexing, change main to reflect this --- src/include/lex.hpp | 2 ++ src/lex.cpp | 14 ++++++++++++++ src/main.cpp | 11 +++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/include/lex.hpp b/src/include/lex.hpp index 7914cdc..dd13749 100644 --- a/src/include/lex.hpp +++ b/src/include/lex.hpp @@ -40,3 +40,5 @@ public: std::vector collect(); }; +// when you don't want to construct the object +std::vector lex(std::string); diff --git a/src/lex.cpp b/src/lex.cpp index fd47c8a..5b6b6f2 100644 --- a/src/lex.cpp +++ b/src/lex.cpp @@ -135,5 +135,19 @@ Token Lexer::next() { } } +vector Lexer::collect() { + vector v; + while (true) { + Token t = next(); + if (t.type == TokenType::End) + break; + v.push_back(t); + } + return v; +} +std::vector lex(std::string s) { + Lexer l(s); + return l.collect(); +} diff --git a/src/main.cpp b/src/main.cpp index cc53e1a..5501e6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,17 @@ #include #include -#include +#include using namespace std; int main() { string s; - cin >> s; + getline(cin, s); + cout << s << endl; - Lexer l(s); - cout << l.next() << endl; + for (auto t : lex(s)) { + cout << t << " "; + } + cout << endl; return 0; } \ No newline at end of file