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/calculator.hpp | 32 +++++++++++++++++++++++++++++++ src/include/tokenizer.hpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/include/calculator.hpp create mode 100644 src/include/tokenizer.hpp (limited to 'src/include') diff --git a/src/include/calculator.hpp b/src/include/calculator.hpp new file mode 100644 index 0000000..8c5e97a --- /dev/null +++ b/src/include/calculator.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "LCD_I2C.hpp" +#include "keypad.hpp" +#include "tokenizer.hpp" +#define LCD_COLUMNS 16 +#define LCD_ROWS 2 +#define I2C_ADDRESS 0x27 + +class Calculator { +private: + LCD_I2C *display; + Keypad *default_keypad; + Tokenizer tokenizer = Tokenizer(); + char default_keypad_chars[4][4] = { + {'1', '2', '3', '+'}, + {'4', '5', '6', '-'}, + {'7', '8', '9', '*'}, + {'(', '0', ')', '='} + }; + char secondary_keypad[4][4] = { + {'1', '2', '3', '+'}, + {'4', '5', '6', '-'}, + {'7', '8', '9', '/'}, + {'(', '0', ')', '='} + }; + uint keypad_col_pins[4] = {6, 7, 8, 9}; + uint keypad_row_pins[4] = {16, 17, 18, 19}; +public: + Calculator(); + ~Calculator(); + void run(); +}; 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