Skip to content

Commit

Permalink
Added more documentation to bindings.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCG-coder committed May 19, 2024
1 parent 0da6958 commit 2005238
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 55 deletions.
24 changes: 18 additions & 6 deletions include/argParse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,25 @@ namespace steppable::__internals::utils
/// @brief This is the type of the positional arguments. It is equivalent to a vector of string_views.
using PosArgs = std::vector<std::string_view>;

/// @brief This is the correct format of a keyword argument.
// language=RegExp
[[maybe_unused]] const std::regex KEYWORD_ARG_REGEX(R"(^-([a-zA-Z]*):(-?[0-9]+)$)");
/**
* @brief This function gets the correct format of a keyword argument.
* @return The regular expression to match a keyword argument.
*/
inline std::regex getKeywordArgRegex()
{
// language=RegExp
return std::regex{ R"(^-([a-zA-Z]*):(-?[0-9]+)$)" };
}

/// @brief This is the correct format of a switch.
// language=RegExp
[[maybe_unused]] const std::regex SWITCH_REGEX(R"(^([-+])([a-zA-Z]*)$)");
/**
* @brief This function gets the correct format of a switch.
* @return The regular expression to match a switch.
*/
inline std::regex getSwitchRegex()
{
// language=RegExp
return std::regex{ R"(^([-+])([a-zA-Z]*)$)" };
}

/**
* @class ProgramArgs
Expand Down
14 changes: 14 additions & 0 deletions include/types/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ namespace steppable::types
*/
enum class Status
{
/// @brief The calculation is done and the output is simplified.
CALCULATED_SIMPLIFIED,

/// @brief The calculation is done and the output is not simplified.
CALCULATED_UNSIMPLIFIED,

/// @brief The calculation is not done, as an error has occurred.
MATH_ERROR
};

Expand All @@ -47,10 +52,19 @@ namespace steppable::types
*/
enum class StatusBool
{
/// @brief The calculation is done and the output is simplified to be true.
CALCULATED_SIMPLIFIED_YES,

/// @brief The calculation is done and the output is simplified to be false.
CALCULATED_SIMPLIFIED_NO,

/// @brief The calculation is done and the output is not simplified. The output is true.
CALCULATED_UNSIMPLIFIED_YES,

/// @brief The calculation is done and the output is not simplified. The output is false.
CALCULATED_UNSIMPLIFIED_NO,

/// @brief The calculation is not done, as an error has occurred.
MATH_ERROR
};

Expand Down
175 changes: 128 additions & 47 deletions lib/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,72 +33,153 @@
#include <nanobind/stl/vector.h>

namespace nb = nanobind;
using namespace steppable::__internals::arithmetic;
using namespace steppable::__internals::utils;
using namespace nb::literals;
using namespace steppable::__internals::arithmetic;
using namespace steppable::__internals::knowledge;
using namespace steppable::__internals::utils;

NB_MODULE(steppyble, mod) // NOLINT
{
auto internals = mod.def_submodule("_internals", "Internal functions.");

nb::enum_<steppable::RoundingMode>(mod, "RoundingMode")
.value("USE_MAXIMUM_PREC", steppable::RoundingMode::USE_MAXIMUM_PREC)
.value("USE_MINIMUM_PREC", steppable::RoundingMode::USE_MINIMUM_PREC)
.value("USE_CURRENT_PREC", steppable::RoundingMode::USE_CURRENT_PREC)
.value("USE_OTHER_PREC", steppable::RoundingMode::USE_OTHER_PREC)
.value("DISCARD_ALL_DECIMALS", steppable::RoundingMode::DISCARD_ALL_DECIMALS);
nb::enum_<steppable::RoundingMode>(mod, "RoundingMode", "The mode to use when rounding the result of an operation.")
.value("USE_MAXIMUM_PREC",
steppable::RoundingMode::USE_MAXIMUM_PREC,
"Use the maximum precision of the two numbers.")
.value("USE_MINIMUM_PREC",
steppable::RoundingMode::USE_MINIMUM_PREC,
"Use the minimum precision of the two numbers.")
.value("USE_CURRENT_PREC",
steppable::RoundingMode::USE_CURRENT_PREC,
"Use the precision of the current number, ignoring the other one.")
.value("USE_OTHER_PREC",
steppable::RoundingMode::USE_OTHER_PREC,
"Use the precision of the other number, ignoring the current one.")
.value("DISCARD_ALL_DECIMALS",
steppable::RoundingMode::DISCARD_ALL_DECIMALS,
"Do not output any decimal places, no matter the current precision.");

nb::class_<steppable::Number>(mod, "Number")
.def(nb::init<std::string, size_t, steppable::RoundingMode>(),
"value"_a = "0",
"prec"_a = 5,
"rounding_mode"_a = steppable::USE_CURRENT_PREC)
.def(nb::self + nb::self, nb::rv_policy::automatic_reference)
.def(nb::self += nb::self, nb::rv_policy::automatic_reference)
.def(nb::self - nb::self, nb::rv_policy::automatic_reference)
.def(nb::self -= nb::self, nb::rv_policy::automatic_reference)
.def(nb::self * nb::self, nb::rv_policy::automatic_reference)
.def(nb::self *= nb::self, nb::rv_policy::automatic_reference)
.def(nb::self / nb::self, nb::rv_policy::automatic_reference)
.def(nb::self /= nb::self, nb::rv_policy::automatic_reference)
.def(nb::self % nb::self, nb::rv_policy::automatic_reference)
.def(nb::self %= nb::self, nb::rv_policy::automatic_reference)
.def("__pow__", &steppable::Number::operator^)
.def(nb::self == nb::self, nb::rv_policy::automatic_reference)
.def(nb::self != nb::self, nb::rv_policy::automatic_reference)
.def(nb::self < nb::self, nb::rv_policy::automatic_reference)
.def(nb::self > nb::self, nb::rv_policy::automatic_reference)
.def(nb::self <= nb::self, nb::rv_policy::automatic_reference)
.def(nb::self >= nb::self, nb::rv_policy::automatic_reference)
.def("__repr__", &steppable::Number::present);
"rounding_mode"_a = steppable::USE_CURRENT_PREC,
nb::rv_policy::automatic_reference,
"Initialize a new Steppable Number object.")
.def(nb::self + nb::self,
nb::rv_policy::automatic_reference,
"Add two numbers together, returning the resulting Number object.")
.def(nb::self += nb::self,
nb::rv_policy::automatic_reference,
"Add two numbers together, storing the result in the current one.")
.def(nb::self - nb::self,
nb::rv_policy::automatic_reference,
"Subtract two numbers, returning the resulting Number object.")
.def(nb::self -= nb::self,
nb::rv_policy::automatic_reference,
"Subtract two numbers, storing the result in the current one.")
.def(nb::self * nb::self,
nb::rv_policy::automatic_reference,
"Multiply two numbers, returning the resulting Number object.")
.def(nb::self *= nb::self,
nb::rv_policy::automatic_reference,
"Multiply two numbers, storing the result in the current one.")
.def(nb::self / nb::self,
nb::rv_policy::automatic_reference,
"Divide two numbers, returning the resulting Number object.")
.def(nb::self /= nb::self,
nb::rv_policy::automatic_reference,
"Divide two numbers, storing the result in the current one.")
.def(nb::self % nb::self,
nb::rv_policy::automatic_reference,
"Calculate the modulus of two numbers, returning the resulting Number object.")
.def(nb::self %= nb::self,
nb::rv_policy::automatic_reference,
"Calculate the modulus of two numbers, storing the result in the current one.")
.def("__pow__",
&steppable::Number::operator^,
nb::rv_policy::automatic_reference,
"Raise the current number to a power, returning the resulting Number object.")
.def(nb::self == nb::self, nb::rv_policy::automatic_reference, "Check if two numbers are equal.")
.def(nb::self != nb::self, nb::rv_policy::automatic_reference, "Check if two numbers are not equal.")
.def(nb::self < nb::self,
nb::rv_policy::automatic_reference,
"Check if the current number is less than the other.")
.def(nb::self > nb::self,
nb::rv_policy::automatic_reference,
"Check if the current number is greater than the other.")
.def(nb::self <= nb::self,
nb::rv_policy::automatic_reference,
"Check if the current number is less than or equal to the other.")
.def(nb::self >= nb::self,
nb::rv_policy::automatic_reference,
"Check if the current number is greater than or equal to the other.")
.def("__repr__",
&steppable::Number::present,
nb::rv_policy::automatic_reference,
"Get a string representation of the current number.");

nb::class_<steppable::Fraction>(mod, "Fraction")
.def(nb::init<std::string, std::string>(), "top"_a = "1", "bottom"_a = "1")
.def(nb::self + nb::self, nb::rv_policy::automatic_reference)
.def(nb::self += nb::self, nb::rv_policy::automatic_reference)
.def(nb::self - nb::self, nb::rv_policy::automatic_reference)
.def(nb::self -= nb::self, nb::rv_policy::automatic_reference)
.def(nb::self * nb::self, nb::rv_policy::automatic_reference)
.def(nb::self *= nb::self, nb::rv_policy::automatic_reference)
.def(nb::self / nb::self, nb::rv_policy::automatic_reference)
.def(nb::self /= nb::self, nb::rv_policy::automatic_reference)
.def("__pow__", &steppable::Fraction::operator^)
.def(nb::self == nb::self, nb::rv_policy::automatic_reference)
.def(nb::self != nb::self, nb::rv_policy::automatic_reference)
.def(nb::self < nb::self, nb::rv_policy::automatic_reference)
.def(nb::self > nb::self, nb::rv_policy::automatic_reference)
.def(nb::self <= nb::self, nb::rv_policy::automatic_reference)
.def(nb::self >= nb::self, nb::rv_policy::automatic_reference)
.def("__repr__", &steppable::Fraction::present);
.def(nb::init<std::string, std::string>(),
"top"_a = "1",
"bottom"_a = "1",
nb::rv_policy::automatic_reference,
"Initialize a new Steppable Fraction object.")
.def(nb::self + nb::self,
nb::rv_policy::automatic_reference,
"Add two fractions together, returning the resulting Fraction object.")
.def(nb::self += nb::self,
nb::rv_policy::automatic_reference,
"Add two fractions together, storing the result in the current one.")
.def(nb::self - nb::self,
nb::rv_policy::automatic_reference,
"Subtract two fractions, returning the resulting Fraction object.")
.def(nb::self -= nb::self,
nb::rv_policy::automatic_reference,
"Subtract two fractions, storing the result in the current one.")
.def(nb::self * nb::self,
nb::rv_policy::automatic_reference,
"Multiply two fractions, returning the resulting Fraction object.")
.def(nb::self *= nb::self,
nb::rv_policy::automatic_reference,
"Multiply two fractions, storing the result in the current one.")
.def(nb::self / nb::self,
nb::rv_policy::automatic_reference,
"Divide two fractions, returning the resulting Fraction object.")
.def(nb::self /= nb::self,
nb::rv_policy::automatic_reference,
"Divide two fractions, storing the result in the current one.")
.def("__pow__",
&steppable::Fraction::operator^,
nb::rv_policy::automatic_reference,
"Raise the current fraction to a power, returning the resulting Fraction object.")
.def(nb::self == nb::self, nb::rv_policy::automatic_reference, "Check if two fractions are equal.")
.def(nb::self != nb::self, nb::rv_policy::automatic_reference, "Check if two fractions are not equal.")
.def(nb::self < nb::self,
nb::rv_policy::automatic_reference,
"Check if the current fraction is less than the other.")
.def(nb::self > nb::self,
nb::rv_policy::automatic_reference,
"Check if the current fraction is greater than the other.")
.def(nb::self <= nb::self,
nb::rv_policy::automatic_reference,
"Check if the current fraction is less than or equal to the other.")
.def(nb::self >= nb::self,
nb::rv_policy::automatic_reference,
"Check if the current fraction is greater than or equal to the other.")
.def("__repr__",
&steppable::Fraction::present,
"in_line"_a = true,
nb::rv_policy::automatic_reference,
"Get a string representation of the current fraction.");

nb::class_<Knowledge>(internals, "Knowledge", "Knowledge entry.")
.def(nb::init<std::string, std::vector<std::string>>(),
"output"_a,
"inputs"_a,
nb::rv_policy::automatic_reference)
.def_rw("output", &Knowledge::output, "The output of the knowledge entry.")
.def_rw("inputs", &Knowledge::inputs, "The inputs of the knowledge entry.");
.def_ro("output", &Knowledge::output, "The output of the knowledge entry.")
.def_ro("inputs", &Knowledge::inputs, "The inputs of the knowledge entry.");

mod.doc() = "The Python bindings for Steppable.";

Expand Down Expand Up @@ -165,7 +246,7 @@ NB_MODULE(steppyble, mod) // NOLINT
&writeKnowledge,
"item"_a,
"knowledge"_a,
"search"_a,
"search"_a = true,
"Internal function that writes knowledge to a file.",
nb::rv_policy::automatic_reference);
}
4 changes: 2 additions & 2 deletions src/argParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ namespace steppable::__internals::utils
for (auto _arg : argv)
{
auto arg = static_cast<std::string>(_arg);
if (std::smatch match; std::regex_match(arg, match, KEYWORD_ARG_REGEX))
if (std::smatch match; std::regex_match(arg, match, getKeywordArgRegex()))
{
std::string name = match[1];
int value = std::stoi(match[2]);
keywordArgs.insert_or_assign(name, value);
}
else if (std::regex_match(arg, match, SWITCH_REGEX))
else if (std::regex_match(arg, match, getSwitchRegex()))
{
bool enabled = match[1] == "+";
std::string name = match[2];
Expand Down

0 comments on commit 2005238

Please sign in to comment.