diff --git a/Cargo.toml b/Cargo.toml index a5556bc..bfd336c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ clap-verbosity-flag = "2.0.1" env_logger = "0.10.0" humantime = "2.1.0" num = "0.4.0" +num-traits = "0.2.16" openssl = "0.10.55" openssl-sys = "0.9.90" pyo3 = "0.18.1" diff --git a/src/lib.rs b/src/lib.rs index 108426c..dd093e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,9 @@ pub mod common; pub mod logger; /// networking tools pub mod networking; +/// math tools +pub mod math; + use crate::logger::Logger; use pyo3::prelude::*; diff --git a/src/math/computer/mod.rs b/src/math/computer/mod.rs new file mode 100644 index 0000000..a0fe4da --- /dev/null +++ b/src/math/computer/mod.rs @@ -0,0 +1,98 @@ +//! # Compute expressions +//! +//! Compute computations with your computer (`ccc`) +//! +//! This modules aim is to take a term of any kind ([String]) and compute it's value, be it +//! variable based or a concrete numerical value. It implements different operators and +//! (mathematical) functions. + +//// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// +// we want docs +#![warn(missing_docs)] +#![warn(rustdoc::missing_crate_level_docs)] +// we want Debug everywhere. +#![warn(missing_debug_implementations)] +// enable clippy's extra lints, the pedantic version +#![warn(clippy::pedantic)] + +//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// +pub mod result; +use result::{Error, Result}; + +use self::result::ComputeResult; +use crate::logger::{trace, debug, info, warn, error}; + +//// TYPES ///////////////////////////////////////////////////////////////////////////////////////// + +//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// + +//// STATICS /////////////////////////////////////////////////////////////////////////////////////// + +//// MACROS //////////////////////////////////////////////////////////////////////////////////////// + +//// ENUMS ///////////////////////////////////////////////////////////////////////////////////////// +#[non_exhaustive] +pub enum Constants { + Pi +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +#[non_exhaustive] +pub enum Operations { + Addit +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +#[non_exhaustive] +pub enum Functions { + Root +} + +//// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// +struct Computer; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +#[derive(Debug)] +struct Term { + original: String, + result: Option, + parts: Vec +} + +//// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// +impl Computer { + pub fn oneshot(t: String) -> Result { + trace!(orig=t, "parsing original string to Term"); + let mut t = Term::new(t); + trace!("term has been parsed, starting computation"); + debug!("parsed term: {t:#?}"); + Self::compute(t) + } + + /// ## compute a [`Term`] + /// + /// This method makes use of the + /// [shunting yard algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm) to + /// compute the final value of any term. + /// + /// This method only processes a single term at a time, without caching. + pub fn compute(mut t: Term) -> Result { + trace!("computing term {t:?}"); + } +} + +impl Term { + pub fn new(orig: String) -> Self { + Term { + original: orig, + result: None, + parts: Vec::new() + } + } +} + +//// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// + +//// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/math/computer/result.rs b/src/math/computer/result.rs new file mode 100644 index 0000000..01b06de --- /dev/null +++ b/src/math/computer/result.rs @@ -0,0 +1,82 @@ +//! # Results and Errors for the compute module +//! +//! This module defines the errors and results that can be processed from any given term. + +//// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// +// we want docs +#![warn(missing_docs)] +#![warn(rustdoc::missing_crate_level_docs)] +// we want Debug everywhere. +#![warn(missing_debug_implementations)] +// enable clippy's extra lints, the pedantic version +#![warn(clippy::pedantic)] + +//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// +use num_traits; + +//// TYPES ///////////////////////////////////////////////////////////////////////////////////////// +pub type Result = std::result::Result; + +//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// + +//// STATICS /////////////////////////////////////////////////////////////////////////////////////// + +//// MACROS //////////////////////////////////////////////////////////////////////////////////////// + +//// ENUMS ///////////////////////////////////////////////////////////////////////////////////////// +#[non_exhaustive] +#[derive(Debug)] +pub enum Error { + SyntaxError +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +#[derive(Debug)] +pub enum ComputeResult { + Variable(VarResult), + Numerical(NumericResult), + Complex(ComplexResult), +} + +#[non_exhaustive] +#[derive(Debug)] +pub enum NumericResult { + Signed(i128), + Unsigned(u128), + Float(f64) +} + +//// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// +#[derive(Debug)] +pub struct VarResult { + +} + +#[derive(Debug)] +pub struct ComplexResult { + +} + +//// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// +impl From for NumericResult where + u128: TryFrom, + u128: TryFrom { + fn from(value: T) -> Self { + if T::min_value().is_zero() { + // unsigned data types + // `u128` is the largest unsigned datatype, any other type will fit. + NumericResult::Unsigned(value.to_u128().unwrap()) + } + else { + // signed data types + // `i128` is the largest unsigned datatype, any other type will fit. + NumericResult::Signed(value.to_i128().unwrap()) + } + } +} + +//// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// + +//// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/math/computer/term.rs b/src/math/computer/term.rs new file mode 100644 index 0000000..c6c483b --- /dev/null +++ b/src/math/computer/term.rs @@ -0,0 +1,40 @@ +//! # A term that can be the input for computation +//! +//! Short description +//! +//! Details +//! +//! ## Section 1 +//! +//! ## Section 2 + +//// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// +// we want docs +#![warn(missing_docs)] +#![warn(rustdoc::missing_crate_level_docs)] +// we want Debug everywhere. +#![warn(missing_debug_implementations)] +// enable clippy's extra lints, the pedantic version +#![warn(clippy::pedantic)] + +//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// + +//// TYPES ///////////////////////////////////////////////////////////////////////////////////////// + +//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// + +//// STATICS /////////////////////////////////////////////////////////////////////////////////////// + +//// MACROS //////////////////////////////////////////////////////////////////////////////////////// + +//// ENUMS ///////////////////////////////////////////////////////////////////////////////////////// + +//// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// + +//// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// + +//// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// + +//// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/math/mod.rs b/src/math/mod.rs index dac9d32..ac6d0e3 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -18,6 +18,7 @@ #![warn(clippy::pedantic)] //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// +pub mod computer; //// TYPES /////////////////////////////////////////////////////////////////////////////////////////