Minor changes - change vector to deque in lexer as this needs to behave like a queue

This commit is contained in:
2025-10-03 21:08:43 +03:00
committed by Emin Arslan
parent ec59b49c32
commit 00fdc84d22
4 changed files with 15 additions and 15 deletions

View File

@@ -11,7 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(HEADER_FILES src/include/lex.hpp) set(HEADER_FILES src/include/lex.hpp)
set(SOURCE_FILES src/lex.cpp) set(SOURCE_FILES src/lex.cpp)
set(CXX_WARNING_FLAGS -Wall -Wextra -Wpedantic -pedantic -Werror) set(CXX_WARNING_FLAGS -Wall -Wextra -Wpedantic -pedantic)
# we're not actually shipping a library yet, # we're not actually shipping a library yet,
# this is so we don't have to compile twice for main and tests. # this is so we don't have to compile twice for main and tests.

View File

@@ -1,4 +1,5 @@
#pragma once #pragma once
#include <deque>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <stdint.h> #include <stdint.h>
@@ -40,8 +41,8 @@ public:
void feed(std::string); void feed(std::string);
Token next(); Token next();
std::vector<Token> collect(); std::deque<Token> collect();
}; };
// when you don't want to construct the object // when you don't want to construct the object
std::vector<Token> lex(std::string); std::deque<Token> lex(std::string);

View File

@@ -9,14 +9,14 @@ using namespace std;
std::ostream &operator<<(std::ostream &os, Token const &t) { std::ostream &operator<<(std::ostream &os, Token const &t) {
os << "Token("; os << "Token(";
switch (t.type) { switch (t.type) {
case TokenType::OpenParen: os << "OpenParen)"; break; case OpenParen: os << "OpenParen)"; break;
case TokenType::CloseParen: os << "CloseParen)"; break; case CloseParen: os << "CloseParen)"; break;
case TokenType::Dollar: os << "Dollar)"; break; case Dollar: os << "Dollar)"; break;
case TokenType::Symbol: os << "Symbol, " << get<string>(*t.value) << ")"; break; case Symbol: os << "Symbol, " << get<string>(*t.value) << ")"; break;
case TokenType::String: os << "String, \"" << get<string>(*t.value) << "\")"; break; case String: os << "String, \"" << get<string>(*t.value) << "\")"; break;
case TokenType::Int: os << "Int, " << get<int64_t>(*t.value) << ")"; break; case Int: os << "Int, " << get<int64_t>(*t.value) << ")"; break;
case TokenType::Double: os << "Double, " << get<double>(*t.value) << ")"; break; case Double: os << "Double, " << get<double>(*t.value) << ")"; break;
case TokenType::End: os << "END)"; break; case End: os << "END)"; break;
default: default:
os << ")"; os << ")";
} }
@@ -146,8 +146,8 @@ Token Lexer::next() {
} }
} }
vector<Token> Lexer::collect() { deque<Token> Lexer::collect() {
vector<Token> v; deque<Token> v;
while (true) { while (true) {
Token t = next(); Token t = next();
if (t.type == TokenType::End) if (t.type == TokenType::End)
@@ -158,7 +158,7 @@ vector<Token> Lexer::collect() {
return v; return v;
} }
std::vector<Token> lex(std::string s) { std::deque<Token> lex(std::string s) {
Lexer l(s); Lexer l(s);
return l.collect(); return l.collect();
} }

View File

@@ -8,7 +8,6 @@ int main() {
string s; string s;
getline(cin, s); getline(cin, s);
cout << s << endl; cout << s << endl;
for (auto t : lex(s)) { for (auto t : lex(s)) {
cout << t << " "; cout << t << " ";
} }