Skip to content

Commit

Permalink
Make use of concepts replacing template magic
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Dec 6, 2023
1 parent 39c192f commit b39f7e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
6 changes: 4 additions & 2 deletions include/helper/ConsolePrinter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ class ConsolePrinter {
bool last_was_endl = true;

public:
template <class T, std::enable_if_t<!ConsolePrinter::is_printer<T>, bool> = true>
template <class T>
requires(!ConsolePrinter::is_printer<T>)
ConsolePrinter &operator<<(const T &output);
ConsolePrinter &operator<<(const NestedPrinter &nested_printer);
ConsolePrinter &operator<<(const EndlPrinter &endl_printer);

friend void endl(ConsolePrinter &printer);
};

template <class T, std::enable_if_t<!ConsolePrinter::is_printer<T>, bool>>
template <class T>
requires(!ConsolePrinter::is_printer<T>)
ConsolePrinter &ConsolePrinter::operator<<(const T &output) {
if (last_was_endl) {
std::cout << std::string(2 * indent, ' ');
Expand Down
30 changes: 16 additions & 14 deletions include/lexer/Lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include <string>
#include <type_traits>

template <class InputIterator>
template <class I>
concept CharIterator = std::same_as<typename std::iterator_traits<I>::value_type, char>;

template <CharIterator InputIterator>
class Lexer {

public:
Expand All @@ -21,9 +24,6 @@ class Lexer {
using reference = Token &;

private:
static_assert(std::is_same<typename std::iterator_traits<InputIterator>::value_type, char>(),
"Expecting iterator over 'char' type.");

InputIterator current_character;
InputIterator characters_end;

Expand All @@ -42,13 +42,14 @@ class Lexer {
private:
std::optional<Token> next();
template <class TokenMatcher>
requires std::is_invocable_r_v<bool, TokenMatcher, char>
std::string get_while_matching(const TokenMatcher &matcher);

static bool is_operator(char c);
static bool is_identifier(char c);
};

template <class InputIterator>
template <CharIterator InputIterator>
Lexer<InputIterator>::Lexer(InputIterator begin, InputIterator end)
: current_character(begin)
, characters_end(end) {
Expand All @@ -57,34 +58,34 @@ Lexer<InputIterator>::Lexer(InputIterator begin, InputIterator end)
}
}

template <class InputIterator>
template <CharIterator InputIterator>
Token Lexer<InputIterator>::operator*() const {
return *current_token;
}

template <class InputIterator>
template <CharIterator InputIterator>
Token &Lexer<InputIterator>::operator++() {
return *(current_token = next());
}

template <class InputIterator>
template <CharIterator InputIterator>
Token Lexer<InputIterator>::operator++(int) {
auto tmp_token = std::move(current_token);
++(*this);
return *tmp_token;
}

template <class InputIterator>
template <CharIterator InputIterator>
bool Lexer<InputIterator>::operator==(const Lexer &other) const {
return current_token.has_value() == other.current_token.has_value();
}

template <class InputIterator>
template <CharIterator InputIterator>
bool Lexer<InputIterator>::operator!=(const Lexer &other) const {
return !(*this == other);
}

template <class InputIterator>
template <CharIterator InputIterator>
std::optional<Token> Lexer<InputIterator>::next() {
while (current_character != characters_end) {
if (isspace(*current_character) != 0) {
Expand Down Expand Up @@ -123,8 +124,9 @@ std::optional<Token> Lexer<InputIterator>::next() {
return {};
}

template <class InputIterator>
template <CharIterator InputIterator>
template <class TokenMatcher>
requires std::is_invocable_r_v<bool, TokenMatcher, char>
std::string Lexer<InputIterator>::get_while_matching(const TokenMatcher &matcher) {
std::string value;
do {
Expand All @@ -133,12 +135,12 @@ std::string Lexer<InputIterator>::get_while_matching(const TokenMatcher &matcher
return value;
}

template <class InputIterator>
template <CharIterator InputIterator>
bool Lexer<InputIterator>::is_operator(const char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '%';
}

template <class InputIterator>
template <CharIterator InputIterator>
bool Lexer<InputIterator>::is_identifier(const char c) {
return (isalnum(c) != 0) || c == '_';
}
Expand Down

0 comments on commit b39f7e9

Please sign in to comment.