diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8cd7179 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/build/calculator-challege", + "args": [], + "cwd": "${workspaceFolder}", + "preLaunchTask": "CMake: build" + } + ] +} \ No newline at end of file diff --git a/BinaryOperatorFactoryTests.cpp b/BinaryOperatorFactoryTests.cpp index 148adfa..a2564e3 100644 --- a/BinaryOperatorFactoryTests.cpp +++ b/BinaryOperatorFactoryTests.cpp @@ -23,6 +23,7 @@ TEMPLATE_TEST_CASE("BinaryOperatorFactory", "[calculator-challenge]", std::int64 CHECK(check_factory(" + ", kLeftOperand + kRightOperand)); CHECK(check_factory(" +", kLeftOperand + kRightOperand)); CHECK(check_factory("+ ", kLeftOperand + kRightOperand)); + CHECK(check_factory("+ a", kLeftOperand + kRightOperand)); CHECK(check_factory("-", kLeftOperand - kRightOperand)); CHECK(check_factory("*", kLeftOperand * kRightOperand)); CHECK(check_factory("/", kLeftOperand / kRightOperand)); diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b22335..513593a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ set(CMAKE_CXX_COMPILER /opt/homebrew/Cellar/llvm@16/16.0.6/bin/clang++) SET(CALCULATOR_CHALLENGE_SOURCES Calculator.cpp + CalculatorCli.cpp utils.cpp ) @@ -45,7 +46,8 @@ target_link_libraries(calculator-challege PRIVATE fmt::fmt) add_executable(calculator-challege-tests CalculatorTests.cpp BinaryOperatorFactoryTests.cpp + CalculatorCliTests.cpp ${CALCULATOR_CHALLENGE_SOURCES} ) -target_link_libraries(calculator-challege-tests PRIVATE Catch2::Catch2WithMain) +target_link_libraries(calculator-challege-tests PRIVATE Catch2::Catch2WithMain fmt::fmt) catch_discover_tests(calculator-challege-tests) diff --git a/Calculator.cpp b/Calculator.cpp index 5a3e521..e20d38e 100644 --- a/Calculator.cpp +++ b/Calculator.cpp @@ -67,7 +67,7 @@ void Calculator::undoOperation(std::size_t count) noexcept _operations.erase(_operations.end() - static_cast(count), _operations.end()); } -auto Calculator::computeLast() noexcept -> std::pair +auto Calculator::computeLast() noexcept -> std::pair> { Workspace workspace; Visitor visitor{_operations, workspace}; @@ -76,11 +76,11 @@ auto Calculator::computeLast() noexcept -> std::pair auto result = compute(visitor, operation); if (result.has_value()) return std::make_pair(operation.variable, result.value()); } - return std::make_pair("na", 0.0); + return std::make_pair("na", std::nullopt); } auto Calculator::computeLastValue() noexcept -> double { auto [_, result] = computeLast(); - return result; + return result.value_or(0.0); } diff --git a/Calculator.hpp b/Calculator.hpp index 29004dd..f27e93a 100644 --- a/Calculator.hpp +++ b/Calculator.hpp @@ -33,7 +33,7 @@ public: void undoOperation(std::size_t count) noexcept; - auto computeLast() noexcept -> std::pair; + auto computeLast() noexcept -> std::pair>; auto computeLastValue() noexcept -> double; private: diff --git a/CalculatorCli.cpp b/CalculatorCli.cpp new file mode 100644 index 0000000..2a7a44c --- /dev/null +++ b/CalculatorCli.cpp @@ -0,0 +1,131 @@ +#include "CalculatorCli.hpp" + +#include + +#include +#include +#include +#include + +#include "Operand.hpp" +#include "utils.hpp" + +namespace { +constexpr std::string_view kExit{"exit"}; +constexpr std::string_view kUndo{"undo"}; +constexpr std::string_view kResult{"result"}; + +constexpr auto kSeparator{' '}; +constexpr auto kEqualOperator{'='}; + +struct OperationMembers +{ + std::string_view result; + std::string_view lhs; + std::string_view rhs; + std::string_view operation; +}; + +template +auto text_to_number(std::string_view text) noexcept +{ + if (text.empty()) return T{}; + + if constexpr (std::is_integral_v) { + return std::stoll(text.data()); + } + + if constexpr (std::is_floating_point_v) { + return T{0}; // TODO: @JC check how to do doubles + } + + return T{}; +} + +auto parse_operation_members(std::string_view text) noexcept -> std::optional +{ + OperationMembers result; + + auto result_sep = text.find_first_of(kEqualOperator); + if (result_sep == std::string::npos) return {}; + + result.result = sanitize_text(text.substr(0, result_sep)); + + auto operation_text = trim_whitespaces(text.substr(result_sep + 1)); + auto left_sep = operation_text.find_first_of(kSeparator); + if (left_sep == std::string::npos) return {}; + + result.lhs = operation_text.substr(0, left_sep); + + auto right_side = operation_text.substr(left_sep + 1); + auto right_sep = right_side.find_first_of(kSeparator); + result.operation = right_side.substr(0, right_sep); + result.rhs = sanitize_text(right_side.substr(right_sep + 1)); + return result; +} + +auto parse_operand(std::string_view text) noexcept -> Operand +{ + assert(!text.empty()); + if (!std::isdigit(text.front())) { // NOLINT + return VariableOperand{std::string{text}}; + } + auto operand{static_cast(text_to_number(text))}; + return IntegerOperand{operand}; +} +} // namespace + +auto CalculatorCli::parse(std::string_view text) noexcept -> std::string +{ + auto sanitized_text = trim_whitespaces(text); + + if (sanitized_text.empty()) return {}; + + if (sanitized_text == kUndo) { + _calculator.undoOperation(1); + return {}; + } + + if (sanitized_text.starts_with(kUndo)) { + auto left_sep = sanitized_text.find_first_of(kSeparator); + _calculator.undoOperation(text_to_number(text.substr(left_sep))); + return {}; + } + + if (sanitized_text.starts_with(kResult)) { + auto [variable, value] = _calculator.computeLast(); + if (!value.has_value()) return "N/A"; + return fmt::format("{} = {}", variable, value.value()); + } + + auto components = parse_operation_members(sanitized_text); + if (!components.has_value()) return {}; + + auto operation = _operation_factory.make(components->operation); + if (!operation.has_value()) return {}; + + _calculator.addOperation( + std::string{components->result}, + parse_operand(components->lhs), + parse_operand(components->rhs), + operation.value()); + + return fmt::format("{} = {} {} {}", components->result, components->lhs, components->operation, components->rhs); +} + +void CalculatorCli::runLoop() noexcept +{ + std::string line; + while (true) { + std::cout << "> "; + std::getline(std::cin, line); + if (std::cin.fail()) { + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + } + + if (line == kExit) return; + auto result = parse(line); + std::cout << result << std::endl; + } +} diff --git a/CalculatorCli.hpp b/CalculatorCli.hpp new file mode 100644 index 0000000..2d48ea8 --- /dev/null +++ b/CalculatorCli.hpp @@ -0,0 +1,19 @@ +#ifndef CALCULATOR_CHALLENGE_CALCULATORCLI_HPP +#define CALCULATOR_CHALLENGE_CALCULATORCLI_HPP + +#include "BinaryOperatorFactory.hpp" +#include "Calculator.hpp" + +class CalculatorCli +{ +public: + std::string parse(std::string_view text) noexcept; + + void runLoop() noexcept; + +private: + Calculator _calculator; + BinaryOperatorFactory _operation_factory; +}; + +#endif diff --git a/CalculatorCliTests.cpp b/CalculatorCliTests.cpp new file mode 100644 index 0000000..1a80d65 --- /dev/null +++ b/CalculatorCliTests.cpp @@ -0,0 +1,80 @@ +#include + +#include "CalculatorCli.hpp" + +TEST_CASE("CalculatorCli - can parse operations", "[calculator-challenge]") +{ + CalculatorCli calculator; + + calculator.parse("a = 4 + 1"); + CHECK(calculator.parse("result") == "a = 5"); + + calculator.parse(" a = 4 + 1"); + CHECK(calculator.parse("result") == "a = 5"); + + calculator.parse("a = 4 + 1 "); + CHECK(calculator.parse("result") == "a = 5"); + + calculator.parse("a = 4 + 1"); + CHECK(calculator.parse("result") == "a = 5"); +} + +TEST_CASE("CalculatorCli - edge cases", "[calculator-challenge]") +{ + CalculatorCli calculator; + + calculator.parse("a=31+2"); + CHECK_FALSE(calculator.parse("result") == "a = 33"); // TODO: @JC do not require spaces +} + +TEST_CASE("CalculatorCli - can undo operations", "[calculator-challenge]") +{ + CalculatorCli calculator; + + calculator.parse("a = 4 + 1"); + CHECK(calculator.parse("result") == "a = 5"); + + calculator.parse(" a = 6 + 1"); + CHECK(calculator.parse("result") == "a = 7"); + + SECTION("simple command") + { + calculator.parse("undo"); + CHECK(calculator.parse("result") == "a = 5"); + } + SECTION("two undos") + { + calculator.parse("undo"); + CHECK(calculator.parse("result") == "a = 5"); + + calculator.parse("undo"); + CHECK(calculator.parse("result") == "N/A"); + } + SECTION("with space") + { + calculator.parse("undo "); + CHECK(calculator.parse("result") == "a = 5"); + } + SECTION("with zero") + { + calculator.parse("undo 0"); + CHECK(calculator.parse("result") == "a = 7"); + } +} + +TEST_CASE("CalculatorCli - can undo multiple operations", "[calculator-challenge]") +{ + CalculatorCli calculator; + + calculator.parse("a = 4 + 1"); + CHECK(calculator.parse("result") == "a = 5"); + + calculator.parse(" a = 6 + 1"); + CHECK(calculator.parse("result") == "a = 7"); + + calculator.parse("b = 6 + 1"); + CHECK(calculator.parse("result") == "b = 7"); + + calculator.parse("undo 2"); + CHECK(calculator.parse("result") == "a = 5"); +} diff --git a/main.cpp b/main.cpp index 47d0996..77d35cd 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,10 @@ #include -#include "fmt/core.h" +#include "CalculatorCli.hpp" -int main() +auto main() -> int { - fmt::print("Hello, World!\n"); + CalculatorCli cli; + cli.runLoop(); return 0; } diff --git a/utils.cpp b/utils.cpp index 081f357..50a05aa 100644 --- a/utils.cpp +++ b/utils.cpp @@ -2,15 +2,29 @@ #include +namespace { +constexpr char kSpace{' '}; +} // namespace + +auto trim_whitespaces(std::string_view text) noexcept -> std::string_view +{ + auto leading_spaces = text.find_first_not_of(kSpace); + if (leading_spaces == std::string::npos) return {}; + + auto sub_view = text.substr(leading_spaces); + + auto trailling_spaces = sub_view.find_last_not_of(kSpace); + if (trailling_spaces == std::string::npos) return sub_view; + + return sub_view.substr(0, trailling_spaces + 1); +} + auto sanitize_text(std::string_view text) noexcept -> std::string_view { - constexpr char kSpace{' '}; - std::string_view view{text}; + auto leading_spaces = text.find_first_not_of(kSpace); + if (leading_spaces == std::string::npos) return {}; - auto leading_spaces = view.find_first_not_of(kSpace); - if (leading_spaces == std::string::npos) return ""; - - auto sub_view = view.substr(leading_spaces); + auto sub_view = text.substr(leading_spaces); auto trailling_spaces = sub_view.find_first_of(kSpace); if (trailling_spaces == std::string::npos) return sub_view; diff --git a/utils.hpp b/utils.hpp index 1e1e334..e8fdeb2 100644 --- a/utils.hpp +++ b/utils.hpp @@ -3,6 +3,7 @@ #include +auto trim_whitespaces(std::string_view text) noexcept -> std::string_view; auto sanitize_text(std::string_view text) noexcept -> std::string_view; #endif