From 10ecf4d95194a273cc6aab89c3a1bcf739a194cd Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 22 Jun 2024 11:01:41 -0600 Subject: Add tokenizer --- src/include/tokenizer.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/include/tokenizer.hpp (limited to 'src/include/tokenizer.hpp') diff --git a/src/include/tokenizer.hpp b/src/include/tokenizer.hpp new file mode 100644 index 0000000..a3f9998 --- /dev/null +++ b/src/include/tokenizer.hpp @@ -0,0 +1,47 @@ +#pragma once +#include +#include +#include + +enum Type { + operand, + sum, + substraction, + multiplication, + division, + left_parens, + right_parens, + equals +}; + +struct Token { + Type type; + int value; +}; + +class Tokenizer { +private: + // Private attributes + std::array tokens; + const std::string *to_tokenize; + size_t current_char_index; + short tokens_head; + // Regexes + std::regex operand_regex = std::regex("^0{1}|\\d+"); + std::regex operation_regex = std::regex("\\+|-|/|\\*"); + std::regex parens_regex = std::regex("(|)"); + // End of private attributes + // Private methods + void clearTokens(); + void insertToken(Type type, int value); + void matchOperation(const std::string &operation); + void matchParens(const std::string &operation); + void matchOperand(const std::string &operation); + std::string buildNumber(char *current); + // End of private methods +public: + Tokenizer(); + ~Tokenizer(); + const std::array &tokenize(const std::string &operation); + const std::array &getTokens(); +}; -- cgit v1.2.3