Compare commits
2 Commits
ec59b49c32
...
561c76b6d7
Author | SHA1 | Date | |
---|---|---|---|
561c76b6d7 | |||
00fdc84d22 |
@@ -11,7 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(HEADER_FILES src/include/lex.hpp)
|
||||
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,
|
||||
# this is so we don't have to compile twice for main and tests.
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <deque>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
@@ -40,8 +41,8 @@ public:
|
||||
void feed(std::string);
|
||||
|
||||
Token next();
|
||||
std::vector<Token> collect();
|
||||
std::deque<Token> collect();
|
||||
};
|
||||
|
||||
// when you don't want to construct the object
|
||||
std::vector<Token> lex(std::string);
|
||||
std::deque<Token> lex(std::string);
|
||||
|
22
src/lex.cpp
22
src/lex.cpp
@@ -9,14 +9,14 @@ using namespace std;
|
||||
std::ostream &operator<<(std::ostream &os, Token const &t) {
|
||||
os << "Token(";
|
||||
switch (t.type) {
|
||||
case TokenType::OpenParen: os << "OpenParen)"; break;
|
||||
case TokenType::CloseParen: os << "CloseParen)"; break;
|
||||
case TokenType::Dollar: os << "Dollar)"; break;
|
||||
case TokenType::Symbol: os << "Symbol, " << get<string>(*t.value) << ")"; break;
|
||||
case TokenType::String: os << "String, \"" << get<string>(*t.value) << "\")"; break;
|
||||
case TokenType::Int: os << "Int, " << get<int64_t>(*t.value) << ")"; break;
|
||||
case TokenType::Double: os << "Double, " << get<double>(*t.value) << ")"; break;
|
||||
case TokenType::End: os << "END)"; break;
|
||||
case OpenParen: os << "OpenParen)"; break;
|
||||
case CloseParen: os << "CloseParen)"; break;
|
||||
case Dollar: os << "Dollar)"; break;
|
||||
case Symbol: os << "Symbol, " << get<string>(*t.value) << ")"; break;
|
||||
case String: os << "String, \"" << get<string>(*t.value) << "\")"; break;
|
||||
case Int: os << "Int, " << get<int64_t>(*t.value) << ")"; break;
|
||||
case Double: os << "Double, " << get<double>(*t.value) << ")"; break;
|
||||
case End: os << "END)"; break;
|
||||
default:
|
||||
os << ")";
|
||||
}
|
||||
@@ -146,8 +146,8 @@ Token Lexer::next() {
|
||||
}
|
||||
}
|
||||
|
||||
vector<Token> Lexer::collect() {
|
||||
vector<Token> v;
|
||||
deque<Token> Lexer::collect() {
|
||||
deque<Token> v;
|
||||
while (true) {
|
||||
Token t = next();
|
||||
if (t.type == TokenType::End)
|
||||
@@ -158,7 +158,7 @@ vector<Token> Lexer::collect() {
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<Token> lex(std::string s) {
|
||||
std::deque<Token> lex(std::string s) {
|
||||
Lexer l(s);
|
||||
return l.collect();
|
||||
}
|
||||
|
@@ -8,7 +8,6 @@ int main() {
|
||||
string s;
|
||||
getline(cin, s);
|
||||
cout << s << endl;
|
||||
|
||||
for (auto t : lex(s)) {
|
||||
cout << t << " ";
|
||||
}
|
||||
|
@@ -6,16 +6,16 @@ TEST_CASE("Lexer lexes doubles correctly", "[Lexer]") {
|
||||
|
||||
SECTION("double and negative syntax") {
|
||||
Lexer l("(1.0 0.1 -.1 -1. . - -. .-)");
|
||||
REQUIRE(l.next() == Token({OpenParen, nullopt}));
|
||||
REQUIRE(l.next() == Token({Double, 1.0}));
|
||||
REQUIRE(l.next() == Token({Double, 0.1}));
|
||||
REQUIRE(l.next() == Token({Double, -0.1}));
|
||||
REQUIRE(l.next() == Token({Double, -1.0}));
|
||||
REQUIRE(l.next() == Token({Symbol, "."}));
|
||||
REQUIRE(l.next() == Token({Symbol, "-"}));
|
||||
REQUIRE(l.next() == Token({Symbol, "-."}));
|
||||
REQUIRE(l.next() == Token({Symbol, ".-"}));
|
||||
REQUIRE(l.next() == Token({CloseParen, nullopt}));
|
||||
REQUIRE(l.next() == Token({TokenType::OpenParen, nullopt}));
|
||||
REQUIRE(l.next() == Token({TokenType::Double, 1.0}));
|
||||
REQUIRE(l.next() == Token({TokenType::Double, 0.1}));
|
||||
REQUIRE(l.next() == Token({TokenType::Double, -0.1}));
|
||||
REQUIRE(l.next() == Token({TokenType::Double, -1.0}));
|
||||
REQUIRE(l.next() == Token({TokenType::Symbol, "."}));
|
||||
REQUIRE(l.next() == Token({TokenType::Symbol, "-"}));
|
||||
REQUIRE(l.next() == Token({TokenType::Symbol, "-."}));
|
||||
REQUIRE(l.next() == Token({TokenType::Symbol, ".-"}));
|
||||
REQUIRE(l.next() == Token({TokenType::CloseParen, nullopt}));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user