feat: add terminal interface

This commit is contained in:
jfmcarreira committed 2024-03-13 01:42:40 +00:00
1 parent 604ca3f48e
commit 863c24d81a
11 files changed
+280 -14

No files matched your search

+17
View File
@@ -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"
}
]
}
+1
View File
@@ -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));
+3 -1
View File
@@ -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)
+3 -3
View File
@@ -67,7 +67,7 @@ void Calculator::undoOperation(std::size_t count) noexcept
_operations.erase(_operations.end() - static_cast<std::int64_t>(count), _operations.end());
}
auto Calculator::computeLast() noexcept -> std::pair<std::string, double>
auto Calculator::computeLast() noexcept -> std::pair<std::string, std::optional<double>>
{
Workspace workspace;
Visitor visitor{_operations, workspace};
@@ -76,11 +76,11 @@ auto Calculator::computeLast() noexcept -> std::pair<std::string, double>
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);
}
+1 -1
View File
@@ -33,7 +33,7 @@ public:
void undoOperation(std::size_t count) noexcept;
auto computeLast() noexcept -> std::pair<std::string, double>;
auto computeLast() noexcept -> std::pair<std::string, std::optional<double>>;
auto computeLastValue() noexcept -> double;
private:
+131
View File
@@ -0,0 +1,131 @@
#include "CalculatorCli.hpp"
#include <fmt/format.h>
#include <cassert>
#include <iostream>
#include <string_view>
#include <type_traits>
#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<typename T>
auto text_to_number(std::string_view text) noexcept
{
if (text.empty()) return T{};
if constexpr (std::is_integral_v<T>) {
return std::stoll(text.data());
}
if constexpr (std::is_floating_point_v<T>) {
return T{0}; // TODO: @JC check how to do doubles
}
return T{};
}
auto parse_operation_members(std::string_view text) noexcept -> std::optional<OperationMembers>
{
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<double>(text_to_number<std::int64_t>(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<std::int64_t>(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<std::streamsize>::max(), '\n');
}
if (line == kExit) return;
auto result = parse(line);
std::cout << result << std::endl;
}
}
+19
View File
@@ -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<double> _operation_factory;
};
#endif
+80
View File
@@ -0,0 +1,80 @@
#include <catch2/catch_test_macros.hpp>
#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");
}
+4 -3
View File
@@ -1,9 +1,10 @@
#include <fmt/format.h>
#include "fmt/core.h"
#include "CalculatorCli.hpp"
int main()
auto main() -> int
{
fmt::print("Hello, World!\n");
CalculatorCli cli;
cli.runLoop();
return 0;
}
+20 -6
View File
@@ -2,15 +2,29 @@
#include <string>
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;
+1
View File
@@ -3,6 +3,7 @@
#include <string_view>
auto trim_whitespaces(std::string_view text) noexcept -> std::string_view;
auto sanitize_text(std::string_view text) noexcept -> std::string_view;
#endif