Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 19, 2024
1 parent ba86ae8 commit a33db67
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 36 deletions.
4 changes: 0 additions & 4 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace detail {

struct Term final {
Token name;

std::unique_ptr<IMathObject> value;

public:
Expand All @@ -40,7 +39,6 @@ struct Term final {

struct FunctionTerm final {
Term term;

std::optional<IOperator::Priority> priority;

public:
Expand All @@ -53,9 +51,7 @@ struct FunctionTerm final {
};

using TermVector = std::vector<Term>;

using FunctionTermStack = std::stack<FunctionTerm>;

using OperandStack = std::stack<std::unique_ptr<IMathObject>>;

}
Expand Down
7 changes: 5 additions & 2 deletions include/fintamath/functions/IFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
#include <string>
#include <unordered_map>

#include <fmt/core.h>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
Expand Down Expand Up @@ -68,15 +71,14 @@ class IFunction : public IMathObject {
protected:
virtual std::unique_ptr<IMathObject> callAbstract(const ArgumentRefVector &argVect) const = 0;

virtual void validateArgsSize(const ArgumentRefVector &argVect) const;

static const FunctionNameToOrderMap &getFunctionNameToOrderMap();

private:
static FunctionNameToOrderMap &getFunctionNameToOrderMutableMap();

static FunctionParser &getParser();

private:
inline static size_t maxFunctionOrder = 0;
};

Expand All @@ -85,6 +87,7 @@ template <typename Return, typename Derived, typename... Args>
class IFunctionCRTP : public IFunction {
#define I_FUNCTION_CRTP IFunctionCRTP<Return, Derived, Args...>
#include "fintamath/functions/IFunctionCRTP.hpp"

#undef I_FUNCTION_CRTP
};

Expand Down
10 changes: 10 additions & 0 deletions include/fintamath/functions/IFunctionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ class IFunctionCRTP_ : public IFunction {
});
}

void validateArgsSize(const ArgumentRefVector &argVect) const {
if (argVect.empty() && isVariadic()) {
throw InvalidInputException(fmt::format("")); // TODO!
}

if (getArgumentTypes().size() != argVect.size() && !isVariadic()) {
throw InvalidInputException(fmt::format("")); // TODO!
}
}

private:
inline static const ArgumentTypeVector argTypes = {Args::getTypeStatic()...};

Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/numbers/Complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Complex final : public INumberCRTP<Complex> {

explicit Complex(const std::string &str);

explicit Complex(const INumber &inReal, const INumber &inImag);
explicit Complex(const INumber &inRe, const INumber &inIm);

explicit Complex(int64_t inReal, int64_t inImag);
explicit Complex(int64_t inRe, int64_t inIm);

Complex(const Integer &rhs);

Expand Down
1 change: 1 addition & 0 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ranges>
#include <stack>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Expand Down
19 changes: 2 additions & 17 deletions src/fintamath/functions/IFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
#include "fintamath/functions/IFunction.hpp"

#include <ranges>
#include <string>
#include <vector>
#include <fmt/core.h>

#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/functions/FunctionArguments.hpp"

namespace fintamath {

void IFunction::validateArgsSize(const ArgumentRefVector &argVect) const {
if (!argVect.empty() && (getArgumentTypes().size() == argVect.size() || isVariadic())) {
return;
}

std::vector<std::string> argNameVect(argVect.size());

for (const auto i : stdv::iota(0U, argNameVect.size())) {
argNameVect[i] = argVect[i].get().toString();
}

throw InvalidInputFunctionException(toString(), argNameVect);
}

const IFunction::FunctionNameToOrderMap &IFunction::getFunctionNameToOrderMap() {
return getFunctionNameToOrderMutableMap();
}
Expand Down
19 changes: 11 additions & 8 deletions src/fintamath/numbers/Complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <memory>
#include <string>

#include <fmt/core.h>

#include "fintamath/core/Converter.hpp"
#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IMathObject.hpp"
Expand Down Expand Up @@ -38,21 +40,22 @@ Complex::Complex(const std::string &str) {
}

if (!re || !im) {
throw InvalidInputException(str);
throw InvalidInputException(fmt::format(R"(Invalid {} "{}")", getTypeStatic().getName(), str));
}
}

Complex::Complex(const INumber &inReal, const INumber &inImag) {
if (is<Complex>(inReal) || is<Complex>(inImag)) {
throw InvalidInputException("Nested complex numbers are not allowed");
Complex::Complex(const INumber &inRe, const INumber &inIm) {
if (is<Complex>(inRe) || is<Complex>(inIm)) {
throw InvalidInputException(
fmt::format(R"(Invalid arguments of {0} (nested {0} are not permitted))", getTypeStatic().getName()));
}

re = cast<INumber>(inReal.toMinimalObject());
im = cast<INumber>(inImag.toMinimalObject());
re = cast<INumber>(inRe.toMinimalObject());
im = cast<INumber>(inIm.toMinimalObject());
}

Complex::Complex(int64_t inReal, int64_t inImag) : re(std::make_unique<Integer>(inReal)),
im(std::make_unique<Integer>(inImag)) {
Complex::Complex(int64_t inRe, int64_t inIm) : re(std::make_unique<Integer>(inRe)),
im(std::make_unique<Integer>(inIm)) {
}

Complex::Complex(const Integer &rhs) : re(cast<INumber>(rhs.toMinimalObject())) {
Expand Down
6 changes: 3 additions & 3 deletions tests/src/numbers/ComplexTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ TEST(ComplexTests, stringConstructorTest) {
EXPECT_EQ(Complex("1.I").toString(), "I");

EXPECT_THAT(
[] { Complex("--10"); },
[] { Complex(""); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[] { Complex("test"); },
[] { Complex("--10"); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
[] { Complex(""); },
[] { Complex("test"); },
testing::ThrowsMessage<InvalidInputException>(
testing::StrEq("")));
EXPECT_THAT(
Expand Down

0 comments on commit a33db67

Please sign in to comment.