Skip to content

Commit

Permalink
Move from C++20 to C++23
Browse files Browse the repository at this point in the history
C++23 features used:

* attributes on lambdas

C++23 features not used by design decision:

* std::expected

We chose to provide error handling by exceptions today. Why? Because it
was an interesting experiment for today

C++23 features not used (because of lack of compiler and tooling
support)

* modules
* import std;
* std::print
  • Loading branch information
lefticus committed Jan 16, 2024
1 parent a91eb91 commit ffd2a28
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.21)

# Only set the cxx_standard if it is not set by someone else
if (NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)
endif()

# strongly encouraged to enable this globally to avoid conflicts between
Expand Down
20 changes: 12 additions & 8 deletions src/infiz/infiz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ auto main() -> int
std::cin.getline(input.data(), max_line - 1, '\n');

while (std::cin.good()) {
const auto answer = evaluate(input.data());
std::cout << "answer: ";

if (answer.getDenominator() == 1) {
std::cout << std::format("{}\n", answer.getNumerator());
} else {
std::cout << std::format(
"{}/{} ({})\n", answer.getNumerator(), answer.getDenominator(), answer.asFloat<double>());
try {
const auto answer = evaluate(input.data());
std::cout << "answer: ";

if (answer.getDenominator() == 1) {
std::cout << std::format("{}\n", answer.getNumerator());
} else {
std::cout << std::format(
"{}/{} ({})\n", answer.getNumerator(), answer.getDenominator(), answer.asFloat<double>());
}
} catch (const std::runtime_error &err) {
std::cout << err.what() << '\n';
}

std::cin.getline(input.data(), max_line - 1, '\n');
Expand Down
61 changes: 44 additions & 17 deletions src/libinfiz/Evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include "RationalNumber.hpp"
#include "Stack.hpp"
#include "StringTokenizer.hpp"
#include <string_view>
#include <concepts>
#include <string_view>
#include <format>

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, gcc-11, Ninja Multi-Config, Release, ON, OFF)

format: No such file or directory

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, gcc-11, Ninja Multi-Config, Release, ON, OFF)

format: No such file or directory

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, gcc-11, Ninja Multi-Config, Release, OFF, OFF)

'format' file not found [clang-diagnostic-error]

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Debug, ON, OFF)

'format' file not found

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Debug, ON, OFF)

'format' file not found

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Debug, ON, OFF)

'format' file not found

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Debug, ON, OFF)

'format' file not found

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, gcc-11, Ninja Multi-Config, Debug, OFF, OFF)

'format' file not found [clang-diagnostic-error]

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Release, ON, OFF)

'format' file not found

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Release, ON, OFF)

'format' file not found

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Release, ON, OFF)

'format' file not found

Check failure on line 9 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-20.04, llvm-15.0.2, Ninja Multi-Config, Release, ON, OFF)

'format' file not found

enum struct Operators { PLUS_SIGN, CLOSE_PAREN, OPEN_PAREN, MINUS_SIGN, DIVIDE_SIGN, MULTIPLY_SIGN };

Expand Down Expand Up @@ -61,7 +62,9 @@ constexpr void evaluateStacks(Stack<RationalNumber> &numbers, Stack<Operators> &

case Operators::MINUS_SIGN: {
operators.pop();
numbers.push(-numbers.pop());
const auto operand2 = numbers.pop();
const auto operand1 = numbers.pop();
numbers.push(operand1 - operand2);
break;
}

Expand All @@ -88,16 +91,15 @@ constexpr void evaluateStacks(Stack<RationalNumber> &numbers, Stack<Operators> &
}


template<std::integral Type>
[[nodiscard]] constexpr auto from_chars(std::string_view input) -> Type
template<std::integral Type> [[nodiscard]] constexpr auto from_chars(std::string_view input) -> Type
{
Type result{0};
Type result{ 0 };

for (const char digit : input) {
result *= 10; // NOLINT
result *= 10;// NOLINT

if (digit >= '0' && digit <= '9') {
result += static_cast<Type>(digit - '0');
if (digit >= '0' && digit <= '9') { result += static_cast<Type>(digit - '0'); } else {
throw std::range_error("not a number");
}
}

Expand All @@ -109,10 +111,29 @@ template<std::integral Type>
Stack<Operators> operators;
Stack<RationalNumber> numbers;

const auto throw_error = [&tokenizer] [[noreturn]] () {

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

syntax error: '[' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

syntax error: ']' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

syntax error: ')' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

syntax error: missing ';' before '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

syntax error: '[' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

syntax error: ']' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

syntax error: ')' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

syntax error: missing ';' before '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

syntax error: '[' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

syntax error: ']' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

syntax error: ')' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

syntax error: missing ';' before '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

syntax error: '[' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

syntax error: ']' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

syntax error: ')' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

syntax error: missing ';' before '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

syntax error: '[' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

syntax error: ']' was unexpected here; expected '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

syntax error: ')' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 114 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

syntax error: missing ';' before '{' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]
throw std::runtime_error(std::format(
R"(Unable to evaluate expression
{}
{}^ unevaluated)",
tokenizer.input(),
std::string(tokenizer.offset(), ' ')));
};

const auto evalStacks = [&]() {
try {
evaluateStacks(numbers, operators);
} catch (const std::runtime_error &) {
throw_error();

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

'throw_error': cannot be used before it is initialized [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

'throw_error': cannot be used before it is initialized [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

'throw_error': cannot be used before it is initialized [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

'throw_error': cannot be used before it is initialized [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

'throw_error': cannot be used before it is initialized [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 127 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]
}
};

Check failure on line 129 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 129 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 129 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 129 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 129 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

'auto evaluateExpression::<lambda_1>::operator ()(void) const': function cannot access 'throw_error' [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

while (tokenizer.hasMoreTokens()) {

auto next = tokenizer.nextToken();

if (next.empty()) { throw_error(); }

Check failure on line 135 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 135 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 135 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 135 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 135 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

auto value = Operators::PLUS_SIGN;

if (!next.empty()) {
Expand Down Expand Up @@ -145,8 +166,12 @@ template<std::integral Type>

default:
operation = false;
const std::integral auto parsed = from_chars<int>(next);
numbers.emplace(parsed, 1);
try {
const std::integral auto parsed = from_chars<int>(next);
numbers.emplace(parsed, 1);
} catch (const std::range_error &) {
throw_error();

Check failure on line 173 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 173 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 173 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, On, On, On)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 173 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Debug, Off)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]

Check failure on line 173 in src/libinfiz/Evaluator.hpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, msvc, Visual Studio 17 2022, Release, Off, ZIP)

term does not evaluate to a function taking 0 arguments [D:\a\infiz\infiz\build\test\constexpr_tests.vcxproj]
}
break;
}

Expand All @@ -157,26 +182,28 @@ template<std::integral Type>
break;
case Operators::CLOSE_PAREN:
operators.push(value);
evaluateStacks(numbers, operators);
evalStacks();
break;
default:
if (operators.peek() != nullptr && precedence(value) <= precedence(*operators.peek())) {
evaluateStacks(numbers, operators);
}
if (operators.peek() != nullptr && precedence(value) <= precedence(*operators.peek())) { evalStacks(); }
operators.push(value);
break;
}
}
}
}

if (operators.peek() != nullptr) { evaluateStacks(numbers, operators); }
if (operators.peek() != nullptr) { evalStacks(); }

if (!operators.empty() || tokenizer.hasUnparsedInput()) {
throw_error();
}

if (numbers.peek() != nullptr) {
return *numbers.peek();
} else {
return { 0, 0 };
}

throw_error();
}

[[nodiscard]] constexpr auto evaluate(std::string_view input) -> RationalNumber
Expand Down
1 change: 1 addition & 0 deletions src/libinfiz/RationalNumber.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef INFIZ_RATIONAL_NUMBER_H
#define INFIZ_RATIONAL_NUMBER_H

#include <compare>
#include <numeric>

/**
Expand Down
16 changes: 14 additions & 2 deletions src/libinfiz/StringTokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

[[nodiscard]] constexpr auto isWhiteSpace(char input) noexcept -> bool
{
return !isNumber(input) && !isOperator(input);
return input == ' ' || input == '\t' || input == '\n' || input == '\r';
}


Expand Down Expand Up @@ -85,10 +85,22 @@ class StringTokenizer
return returnValue;
}

[[nodiscard]] constexpr auto hasMoreTokens() const {
[[nodiscard]] constexpr auto hasUnparsedInput() const noexcept {
return currentOffset < string.size();
}

[[nodiscard]] constexpr auto hasMoreTokens() const noexcept {
return moreTokens;
}

[[nodiscard]] constexpr auto input() const noexcept {
return string;
}

[[nodiscard]] constexpr auto offset() const noexcept {
return currentOffset;
}

private:
std::string_view string;
std::size_t currentOffset{0};
Expand Down
6 changes: 6 additions & 0 deletions test/constexpr_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ TEST_CASE("Addition")
STATIC_REQUIRE(evaluate("(3 + (2 + 4))") == RationalNumber(9, 1));// NOLINT
}

TEST_CASE("Subtraction")
{
STATIC_REQUIRE(evaluate("(3 - 2)") == RationalNumber(1, 1));// NOLINT
STATIC_REQUIRE(evaluate("(3 + (2 - 4))") == RationalNumber(1, 1));// NOLINT
}

TEST_CASE("Division")
{
STATIC_REQUIRE(evaluate("(3 / 2)") == RationalNumber(3, 2));// NOLINT
Expand Down

0 comments on commit ffd2a28

Please sign in to comment.