rename computer to calculator

This commit is contained in:
Christoph J. Scherr 2023-09-12 20:39:37 +02:00
parent b9b0a40cd6
commit 1e176814e9
5 changed files with 56 additions and 43 deletions

View File

@ -1,9 +1,9 @@
//! # Executable for the math/compute submodule //! # Executable for the math/compute submodule
//! //!
//! Compute computations with your computer! //! Calculate Calculations with your Computer!
//! //!
//! This command line tool allows you to input a mathematical expression. It will then process the //! This command line tool allows you to input a mathematical expression. It will then process the
//! expression. //! expression.
//// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// //// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
// we want docs // we want docs
@ -15,7 +15,7 @@
#![warn(clippy::pedantic)] #![warn(clippy::pedantic)]
//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// //// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
use pt::math::computer::{*, self}; use pt::math::calculator::{*, self};
use pt::logger::*; use pt::logger::*;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
@ -29,9 +29,9 @@ use std::path::PathBuf;
//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// //// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
/// short about section displayed in help /// short about section displayed in help
const ABOUT_ROOT: &'static str = r##" const ABOUT_ROOT: &'static str = r##"
Compute computations with your computer Calculate Calculations with your Computer
This commandline tool allows you to calculate complex mathematical expressions right in your This commandline tool allows you to calculate complex mathematical expressions right in your
shell. shell.
"##; "##;
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT) /// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
@ -44,11 +44,11 @@ static LONG_ABOUT_ROOT: &'static str = r##"
//// STATICS /////////////////////////////////////////////////////////////////////////////////////// //// STATICS ///////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
#[command( #[command(
author, author,
version, version,
about = ABOUT_ROOT, about = ABOUT_ROOT,
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT), long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
help_template = help_template =
r#"libpt: {version}{about-section}Author: r#"libpt: {version}{about-section}Author:
{author-with-newline} {author-with-newline}
{usage-heading} {usage}{all-args}{tab}"# {usage-heading} {usage}{all-args}{tab}"#
@ -128,7 +128,7 @@ fn main() {
} }
debug!("exporssion: {}", expr); debug!("exporssion: {}", expr);
let r = Computer::oneshot(expr); let r = Calculator::oneshot(expr);
println!("{}", r.unwrap()); println!("{}", r.unwrap());
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,8 +1,8 @@
//! # Compute expressions //! # Calculate expressions
//! //!
//! Compute computations with your computer (`ccc`) //! Calculate Calculations with your Calculator (`ccc`)
//! //!
//! This modules aim is to take a term of any kind ([String]) and compute it's value, be it //! This modules aim is to take a term of any kind ([String]) and calculate it's value, be it
//! variable based or a concrete numerical value. It implements different operators and //! variable based or a concrete numerical value. It implements different operators and
//! (mathematical) functions. //! (mathematical) functions.
@ -17,8 +17,9 @@
//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// //// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
pub mod result; pub mod result;
pub use result::{Error, Result, ComputeResult}; pub use result::{Error, Result, CalculateResult};
#[allow(unused_imports)] // we possibly want to use all log levels
use crate::logger::{trace, debug, info, warn, error}; use crate::logger::{trace, debug, info, warn, error};
//// TYPES ///////////////////////////////////////////////////////////////////////////////////////// //// TYPES /////////////////////////////////////////////////////////////////////////////////////////
@ -53,6 +54,23 @@ pub enum Operations {
Division, Division,
/// Mathmatical modulo, finite field arithmetic /// Mathmatical modulo, finite field arithmetic
Modulo, Modulo,
/// Any function, seel [`Function`]
Function(Function)
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// ## Supported Functions
///
/// This `enum` contains all functions supported in this module.
///
/// A function has a name followed by braces directly afterwards.
/// A function may have 0 to 31 Arguments.
///
/// Example: `sqrt(19)`, `floor(19.9)`
#[non_exhaustive]
#[derive(Debug)]
pub enum Function {
/// Draw the mathmatical root, attribute n is the nth root /// Draw the mathmatical root, attribute n is the nth root
Root(u16), Root(u16),
/// round up /// round up
@ -61,46 +79,41 @@ pub enum Operations {
Ceil, Ceil,
/// round to nearest integer /// round to nearest integer
/// (commercial rounding) /// (commercial rounding)
Round Round,
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[non_exhaustive]
pub enum Functions {
Root
} }
//// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// //// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
pub struct Computer; /// ## A Calculator object
pub struct Calculator;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)] #[derive(Debug)]
pub struct Term { pub struct Term {
original: String, original: String,
result: Option<ComputeResult>, result: Option<CalculateResult>,
parts: Vec<String> parts: Vec<String>
} }
//// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// //// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
impl Computer { impl Calculator {
pub fn oneshot(t: String) -> Result<ComputeResult> { pub fn oneshot(t: String) -> Result<CalculateResult> {
trace!(orig=t, "parsing original string to Term"); trace!(orig=t, "parsing original string to Term");
let mut t = Term::new(t); let mut t = Term::new(t);
trace!("term has been parsed, starting computation"); trace!("term has been parsed, starting Calculation");
debug!("parsed term: {t:#?}"); debug!("parsed term: {t:#?}");
Self::compute(t) Self::calc(t)
} }
/// ## compute a [`Term`] /// ## Calculate a [`Term`]
/// ///
/// This method makes use of the /// This method makes use of the
/// [shunting yard algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm) to /// [shunting yard algorithm](https://en.wikipedia.org/wiki/Shunting_yard_algorithm) to
/// compute the final value of any term. /// Calculate the final value of any term.
/// ///
/// This method only processes a single term at a time, without caching. /// This method only processes a single term at a time, without caching.
pub fn compute(mut t: Term) -> Result<ComputeResult> { pub fn calc(mut t: Term) -> Result<CalculateResult> {
trace!("computing term {t:?}"); trace!("Calculating term {t:?}");
return Ok(ComputeResult::from(0)) return Ok(CalculateResult::from(0))
} }
} }

View File

@ -1,4 +1,4 @@
//! # Results and Errors for the compute module //! # Results and Errors for the calculate module
//! //!
//! This module defines the errors and results that can be processed from any given term. //! This module defines the errors and results that can be processed from any given term.
@ -33,7 +33,7 @@ pub enum Error {
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)] #[derive(Debug)]
pub enum ComputeResult { pub enum CalculateResult {
Variable(VarResult), Variable(VarResult),
Numerical(NumericResult), Numerical(NumericResult),
Complex(ComplexResult), Complex(ComplexResult),
@ -78,7 +78,7 @@ impl<T: num_traits::PrimInt> From<T> for NumericResult where
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
impl<T: num_traits::PrimInt> From<T> for ComputeResult where impl<T: num_traits::PrimInt> From<T> for CalculateResult where
u128: TryFrom<T>, u128: TryFrom<T>,
u128: TryFrom<T> { u128: TryFrom<T> {
fn from(value: T) -> Self { fn from(value: T) -> Self {
@ -87,23 +87,23 @@ impl<T: num_traits::PrimInt> From<T> for ComputeResult where
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
impl From<NumericResult> for ComputeResult { impl From<NumericResult> for CalculateResult {
fn from(value: NumericResult) -> Self { fn from(value: NumericResult) -> Self {
ComputeResult::Numerical(value) CalculateResult::Numerical(value)
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
impl Display for ComputeResult { impl Display for CalculateResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
ComputeResult::Numerical(val) => { CalculateResult::Numerical(val) => {
write!(f, "{}", val) write!(f, "{}", val)
} }
ComputeResult::Complex(val) => { CalculateResult::Complex(val) => {
write!(f, "{}", val) write!(f, "{}", val)
} }
ComputeResult::Variable(val) => { CalculateResult::Variable(val) => {
write!(f, "{}", val) write!(f, "{}", val)
} }
} }

View File

@ -1,4 +1,4 @@
//! # A term that can be the input for computation //! # A term that can be the input for calculation
//! //!
//! Short description //! Short description
//! //!

View File

@ -18,7 +18,7 @@
#![warn(clippy::pedantic)] #![warn(clippy::pedantic)]
//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// //// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
pub mod computer; pub mod calculator;
//// TYPES ///////////////////////////////////////////////////////////////////////////////////////// //// TYPES /////////////////////////////////////////////////////////////////////////////////////////