Skip to content

Commit

Permalink
move built_in_functions mod
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Oct 31, 2023
1 parent 8367784 commit bfc29dd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 52 deletions.
3 changes: 2 additions & 1 deletion src/tests/parser/valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use core::f64;
/* Crate imports */
use crate::{
element::{BinOp, Element, FunctionCall, UnOp},
token::{built_in_functions, Operator},
token::Operator,
utils::built_in_functions,
Parser, Xprs,
};

Expand Down
50 changes: 0 additions & 50 deletions src/token/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,3 @@ macro_rules! xprs_fn {
)
};
}

pub mod built_in_functions {
/* Crate imports */
use super::Function;

// sin
pub const SIN: Function = xprs_fn!("sin", f64::sin, 1);
pub const SINH: Function = xprs_fn!("sinh", f64::sinh, 1);
pub const ASIN: Function = xprs_fn!("asin", f64::asin, 1);
pub const ASINH: Function = xprs_fn!("asinh", f64::asinh, 1);
// cos
pub const COS: Function = xprs_fn!("cos", f64::cos, 1);
pub const COSH: Function = xprs_fn!("cosh", f64::cosh, 1);
pub const ACOS: Function = xprs_fn!("acos", f64::acos, 1);
pub const ACOSH: Function = xprs_fn!("acosh", f64::acosh, 1);
// tan
pub const TAN: Function = xprs_fn!("tan", f64::tan, 1);
pub const TANH: Function = xprs_fn!("tanh", f64::tanh, 1);
pub const ATAN: Function = xprs_fn!("atan", f64::atan, 1);
pub const ATAN2: Function = xprs_fn!("atan2", f64::atan2, 2);
pub const ATANH: Function = xprs_fn!("atanh", f64::atanh, 1);
// log
pub const LN: Function = xprs_fn!("ln", f64::ln, 1);
pub const LOG: Function = xprs_fn!("log", f64::log10, 1);
pub const LOGN: Function = xprs_fn!("logn", f64::log, 2);
// roots
pub const SQRT: Function = xprs_fn!("sqrt", f64::sqrt, 1);
pub const CBRT: Function = xprs_fn!("cbrt", f64::cbrt, 1);
// misc
pub const EXP: Function = xprs_fn!("exp", f64::exp, 1);
pub const ABS: Function = xprs_fn!("abs", f64::abs, 1);
pub const FLOOR: Function = xprs_fn!("floor", f64::floor, 1);
pub const CEIL: Function = xprs_fn!("ceil", f64::ceil, 1);
pub const ROUND: Function = xprs_fn!("round", f64::round, 1);
pub const TRUNC: Function = xprs_fn!("trunc", f64::trunc, 1);
pub const SUM: Function = xprs_fn!("sum", |args| args.iter().sum());
#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
pub const MEAN: Function = xprs_fn!("mean", |args| {
args.iter().sum::<f64>() / args.len() as f64
});
pub const INVERT: Function = xprs_fn!("invert", f64::recip, 1);
pub const MIN: Function = xprs_fn!("min", |args| {
args.iter().fold(f64::INFINITY, |acc, &x| acc.min(x))
});
pub const MAX: Function = xprs_fn!("max", |args| {
args.iter().fold(f64::NEG_INFINITY, |acc, &x| acc.max(x))
});
pub const HYPOT: Function = xprs_fn!("hypot", f64::hypot, 2);
pub const FRACT: Function = xprs_fn!("fract", f64::fract, 1);
}
3 changes: 2 additions & 1 deletion src/token/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* Built-in imports */
use core::f64;
/* Crate imports */
use super::{function::built_in_functions, Function};
use super::Function;
use crate::utils::built_in_functions;

#[derive(Debug, PartialEq, PartialOrd)]
pub enum Identifier<'a> {
Expand Down
47 changes: 47 additions & 0 deletions src/utils/built_in_functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Crate imports */
use crate::{token::Function, xprs_fn};

// sin
pub const SIN: Function = xprs_fn!("sin", f64::sin, 1);
pub const SINH: Function = xprs_fn!("sinh", f64::sinh, 1);
pub const ASIN: Function = xprs_fn!("asin", f64::asin, 1);
pub const ASINH: Function = xprs_fn!("asinh", f64::asinh, 1);
// cos
pub const COS: Function = xprs_fn!("cos", f64::cos, 1);
pub const COSH: Function = xprs_fn!("cosh", f64::cosh, 1);
pub const ACOS: Function = xprs_fn!("acos", f64::acos, 1);
pub const ACOSH: Function = xprs_fn!("acosh", f64::acosh, 1);
// tan
pub const TAN: Function = xprs_fn!("tan", f64::tan, 1);
pub const TANH: Function = xprs_fn!("tanh", f64::tanh, 1);
pub const ATAN: Function = xprs_fn!("atan", f64::atan, 1);
pub const ATAN2: Function = xprs_fn!("atan2", f64::atan2, 2);
pub const ATANH: Function = xprs_fn!("atanh", f64::atanh, 1);
// log
pub const LN: Function = xprs_fn!("ln", f64::ln, 1);
pub const LOG: Function = xprs_fn!("log", f64::log10, 1);
pub const LOGN: Function = xprs_fn!("logn", f64::log, 2);
// roots
pub const SQRT: Function = xprs_fn!("sqrt", f64::sqrt, 1);
pub const CBRT: Function = xprs_fn!("cbrt", f64::cbrt, 1);
// misc
pub const EXP: Function = xprs_fn!("exp", f64::exp, 1);
pub const ABS: Function = xprs_fn!("abs", f64::abs, 1);
pub const FLOOR: Function = xprs_fn!("floor", f64::floor, 1);
pub const CEIL: Function = xprs_fn!("ceil", f64::ceil, 1);
pub const ROUND: Function = xprs_fn!("round", f64::round, 1);
pub const TRUNC: Function = xprs_fn!("trunc", f64::trunc, 1);
pub const SUM: Function = xprs_fn!("sum", |args| args.iter().sum());
#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
pub const MEAN: Function = xprs_fn!("mean", |args| {
args.iter().sum::<f64>() / args.len() as f64
});
pub const INVERT: Function = xprs_fn!("invert", f64::recip, 1);
pub const MIN: Function = xprs_fn!("min", |args| {
args.iter().fold(f64::INFINITY, |acc, &x| acc.min(x))
});
pub const MAX: Function = xprs_fn!("max", |args| {
args.iter().fold(f64::NEG_INFINITY, |acc, &x| acc.max(x))
});
pub const HYPOT: Function = xprs_fn!("hypot", f64::hypot, 2);
pub const FRACT: Function = xprs_fn!("fract", f64::fract, 1);
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Modules */
pub mod built_in_functions;
#[doc(hidden)]
pub mod macros;
pub mod precedence;

0 comments on commit bfc29dd

Please sign in to comment.