From 10ecf4d95194a273cc6aab89c3a1bcf739a194cd Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 22 Jun 2024 11:01:41 -0600 Subject: Add tokenizer --- CMakeLists.txt | 20 ++---- src/CMakeLists.txt | 17 +++++ src/calculator.cpp | 54 ++++++++++++++ src/exceptions/tokenizer_exception.hpp | 13 ++++ src/include/calculator.hpp | 32 +++++++++ src/include/tokenizer.hpp | 47 +++++++++++++ src/pico-calc.cpp | 6 +- src/tokenizer.cpp | 125 +++++++++++++++++++++++++++++++++ 8 files changed, 296 insertions(+), 18 deletions(-) create mode 100644 src/CMakeLists.txt create mode 100644 src/calculator.cpp create mode 100644 src/exceptions/tokenizer_exception.hpp create mode 100644 src/include/calculator.hpp create mode 100644 src/include/tokenizer.hpp create mode 100644 src/tokenizer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0734ba4..1f58270 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,26 +1,14 @@ cmake_minimum_required(VERSION 3.12) include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake) -set(CMAKE_CXX_FLAGS_DEBUG "-pipe -g -O0 -Wfatal-errors -Wpedantic -Wall -Wextra -Wconversion -Wshadow=local -Wdouble-promotion -Wformat=2 -Wformat-overflow=2 -Wformat-nonliteral -Wformat-security -Wformat-truncation=2 -Wnull-dereference -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wswitch-default -Wunused-parameter -Wunused-const-variable=2 -Wstrict-overflow=4 -Wstringop-overflow=3 -Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wmissing-noreturn -Wsuggest-attribute=malloc -Wsuggest-attribute=format -Wmissing-format-attribute -Wsuggest-attribute=cold -Walloc-zero -Walloca -Wattribute-alias=2 -Wduplicated-branches -Wcast-qual") -set(CMAKE_C_FLAGS_DEBUG "-pipe -g -O0 -Wfatal-errors -Wpedantic -Wall -Wextra -Wconversion -Wshadow=local -Wdouble-promotion -Wformat=2 -Wformat-overflow=2 -Wformat-nonliteral -Wformat-security -Wformat-truncation=2 -Wnull-dereference -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wswitch-default -Wunused-parameter -Wunused-const-variable=2 -Wstrict-overflow=4 -Wstringop-overflow=3 -Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wmissing-noreturn -Wsuggest-attribute=malloc -Wsuggest-attribute=format -Wmissing-format-attribute -Wsuggest-attribute=cold -Walloc-zero -Walloca -Wattribute-alias=2 -Wduplicated-branches -Wcast-qual") +set(CMAKE_CXX_FLAGS_DEBUG "-pipe -g -O0 -Wfatal-errors") +set(CMAKE_C_FLAGS_DEBUG "-pipe -g -O0 -Wfatal-errors") project(pico-calc C CXX ASM) set(CMAKE_CXX_FLAGS_RELEASE "-pipe -Os -fno-builtin") set(CMAKE_C_FLAGS_RELEASE "-pipe -Os -fno-builtin") +set(PICO_CXX_ENABLE_EXCEPTIONS 1) pico_sdk_init() -# Dependencies -add_library(lcd-i2c ./libs/Pico-I2C-LCD/LCD_I2C.cpp ./libs/Pico-I2C-LCD/LCD_I2C.hpp) -add_library(keypad ./libs/pico-keypad/src/keypad.cpp ./libs/pico-keypad/src/keypad.hpp) - -add_executable(pico-calc ./src/pico-calc.cpp) - -target_include_directories(pico-calc PUBLIC ./libs) - -# Dependencies compilation -target_link_libraries(keypad pico_stdlib hardware_gpio) -target_link_libraries(lcd-i2c pico_stdlib hardware_i2c) - -# Link libraries -target_link_libraries(pico-calc keypad lcd-i2c) +add_subdirectory(src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..89b60e6 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,17 @@ +# Dependencies +# Keypad +add_library(pico-keypad ../libs/pico-keypad/src/keypad.cpp) +target_link_libraries(pico-keypad PUBLIC pico_stdlib hardware_gpio) +target_include_directories(pico-keypad INTERFACE ../libs/pico-keypad/src) +target_include_directories(pico-keypad PUBLIC ../libs/pico-keypad/src) +# I2C LCD +add_library(Pico-I2C-LCD ../libs/Pico-I2C-LCD/LCD_I2C.cpp) +target_link_libraries(Pico-I2C-LCD PUBLIC pico_stdlib hardware_i2c) +target_include_directories(Pico-I2C-LCD INTERFACE ../libs/Pico-I2C-LCD/) +target_include_directories(Pico-I2C-LCD PUBLIC ../libs/Pico-I2C-LCD/) + +# Main executable +add_executable(pico-calc pico-calc.cpp calculator.cpp tokenizer.cpp) +target_link_libraries(pico-calc + hardware_i2c Pico-I2C-LCD pico-keypad hardware_timer) +target_include_directories(pico-calc PUBLIC include exceptions) diff --git a/src/calculator.cpp b/src/calculator.cpp new file mode 100644 index 0000000..5de706b --- /dev/null +++ b/src/calculator.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include "hardware/i2c.h" +#include "pico/time.h" +#include "LCD_I2C.hpp" +#include "keypad.hpp" +#include "include/calculator.hpp" +#include "include/tokenizer.hpp" + + +Calculator::Calculator() { + display = new LCD_I2C(I2C_ADDRESS, LCD_COLUMNS, LCD_ROWS, + PICO_DEFAULT_I2C_INSTANCE, + PICO_DEFAULT_I2C_SDA_PIN, + PICO_DEFAULT_I2C_SCL_PIN); + default_keypad = new Keypad(keypad_row_pins, keypad_col_pins, + default_keypad_chars); + display->BacklightOn(); +} + +Calculator::~Calculator() { + delete display; + delete default_keypad; +} + +void Calculator::run() { + while(true) { + display->Clear(); + std::string input; + short str_len = 0; + char pressed_key = default_keypad->getKey(); + + if(pressed_key == '\0') + continue; + + while(str_len <= 15) { + pressed_key = default_keypad->getKey(); + + if(pressed_key != '\0' && pressed_key != '=') { + display->PrintChar(pressed_key); + input += pressed_key; + str_len += 1; + } + + if(pressed_key == '=') { + std::array tokens = tokenizer.tokenize(input); + break; + } + + busy_wait_ms(170); + } + } +} diff --git a/src/exceptions/tokenizer_exception.hpp b/src/exceptions/tokenizer_exception.hpp new file mode 100644 index 0000000..2366bb1 --- /dev/null +++ b/src/exceptions/tokenizer_exception.hpp @@ -0,0 +1,13 @@ +#pragma once +#include + +class TokenizerException { +private: + std::string message; +public: + TokenizerException(std::string error) : message{error} {} + const std::string &getError() const { + return message; + } +}; + 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(); +}; diff --git a/src/pico-calc.cpp b/src/pico-calc.cpp index 14ab8b3..91228e5 100644 --- a/src/pico-calc.cpp +++ b/src/pico-calc.cpp @@ -1,6 +1,8 @@ -#include "pico-keypad/src/keypad.hpp" -#include "Pico-I2C-LCD/LCD_I2C.hpp" +#include "include/calculator.hpp" int main() { + Calculator calc = Calculator(); + calc.run(); + return 0; } diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp new file mode 100644 index 0000000..a156000 --- /dev/null +++ b/src/tokenizer.cpp @@ -0,0 +1,125 @@ +#include +#include +#include "include/tokenizer.hpp" +#include "exceptions/tokenizer_exception.hpp" + +Tokenizer::Tokenizer() { + tokens.fill(nullptr); + tokens_head = 0; +} + +Tokenizer::~Tokenizer() { + clearTokens(); +} + +void Tokenizer::clearTokens() { + for(auto token{ tokens.begin() }; *token != nullptr; ++token) + delete token; + + tokens.fill(nullptr); +} + +void Tokenizer::insertToken(Type type, int value = 0) { + Token *new_token = new Token { .type = type, .value = value}; + + tokens[tokens_head] = new_token; + tokens_head += 1; +} + + +const std::array +&Tokenizer::tokenize(const std::string &operation) { + to_tokenize = &operation; + current_char_index = 0; + + while(current_char_index < operation.length()) { + size_t unchanged_index = current_char_index; + + matchOperand(operation); + matchParens(operation); + matchOperation(operation); + + // No matches. + if(unchanged_index == current_char_index) + throw TokenizerException("Invalid character detected."); + } + + return getTokens(); +} + +void Tokenizer::matchOperand(const std::string &operation) { + std::smatch match; + + if(current_char_index > operation.length()) + return; + + std::string remaining(operation.substr(current_char_index)); + bool result = std::regex_search(remaining, match, + operand_regex); + + if(!result) + return; + + insertToken(operand, std::stoi(match.str(0))); + current_char_index += match.length(0); +} + +void Tokenizer::matchParens(const std::string &operation) { + std::smatch match; + + if(current_char_index > operation.length()) + return; + + std::string remaining(operation.substr(current_char_index)); + bool result = std::regex_search(remaining, match, + operand_regex); + + if(!result) + return; + + switch(match.str(0)[0]) { + case '(': + insertToken(left_parens); + break; + case ')': + insertToken(right_parens); + break; + } + + current_char_index += 1; +} + +void Tokenizer::matchOperation(const std::string &operation) { + std::smatch match; + + if(current_char_index > operation.length()) + return; + + std::string remaining(operation.substr(current_char_index)); + bool result = std::regex_search(remaining, match, + operand_regex); + + if(!result) + return; + + switch(match.str(0)[0]) { + case '+': + insertToken(sum); + break; + case '-': + insertToken(substraction); + break; + case '*': + insertToken(multiplication); + break; + case '/': + insertToken(division); + break; + } + + current_char_index += 1; +} + +const std::array &Tokenizer::getTokens() { + return tokens; +} -- cgit v1.2.3