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
//!
//! 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
//! expression.
//! expression.
//// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
// we want docs
@ -15,7 +15,7 @@
#![warn(clippy::pedantic)]
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
use pt::math::computer::{*, self};
use pt::math::calculator::{*, self};
use pt::logger::*;
use clap::{Parser, Subcommand};
@ -29,9 +29,9 @@ use std::path::PathBuf;
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
/// short about section displayed in help
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.
"##;
/// 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 ///////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Clone, Parser)]
#[command(
author,
version,
about = ABOUT_ROOT,
author,
version,
about = ABOUT_ROOT,
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
help_template =
help_template =
r#"libpt: {version}{about-section}Author:
{author-with-newline}
{usage-heading} {usage}{all-args}{tab}"#
@ -128,7 +128,7 @@ fn main() {
}
debug!("exporssion: {}", expr);
let r = Computer::oneshot(expr);
let r = Calculator::oneshot(expr);
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
//! (mathematical) functions.
@ -17,8 +17,9 @@
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
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};
//// TYPES /////////////////////////////////////////////////////////////////////////////////////////
@ -53,6 +54,23 @@ pub enum Operations {
Division,
/// Mathmatical modulo, finite field arithmetic
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
Root(u16),
/// round up
@ -61,46 +79,41 @@ pub enum Operations {
Ceil,
/// round to nearest integer
/// (commercial rounding)
Round
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[non_exhaustive]
pub enum Functions {
Root
Round,
}
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
pub struct Computer;
/// ## A Calculator object
pub struct Calculator;
////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
pub struct Term {
original: String,
result: Option<ComputeResult>,
result: Option<CalculateResult>,
parts: Vec<String>
}
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
impl Computer {
pub fn oneshot(t: String) -> Result<ComputeResult> {
impl Calculator {
pub fn oneshot(t: String) -> Result<CalculateResult> {
trace!(orig=t, "parsing original string to Term");
let mut t = Term::new(t);
trace!("term has been parsed, starting computation");
trace!("term has been parsed, starting Calculation");
debug!("parsed term: {t:#?}");
Self::compute(t)
Self::calc(t)
}
/// ## compute a [`Term`]
/// ## Calculate 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.
/// Calculate 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<ComputeResult> {
trace!("computing term {t:?}");
return Ok(ComputeResult::from(0))
pub fn calc(mut t: Term) -> Result<CalculateResult> {
trace!("Calculating term {t:?}");
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.
@ -33,7 +33,7 @@ pub enum Error {
////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug)]
pub enum ComputeResult {
pub enum CalculateResult {
Variable(VarResult),
Numerical(NumericResult),
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> {
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 {
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 {
match self {
ComputeResult::Numerical(val) => {
CalculateResult::Numerical(val) => {
write!(f, "{}", val)
}
ComputeResult::Complex(val) => {
CalculateResult::Complex(val) => {
write!(f, "{}", val)
}
ComputeResult::Variable(val) => {
CalculateResult::Variable(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
//!

View File

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