use std::{
fmt::{Debug, Display},
str::FromStr,
};
mod errors;
mod index_set;
mod node;
mod parse;
pub use errors::{ArithIndexSetTryFromError, Error, LexerErrorKind, ParseErrorKind};
pub use index_set::{ArithIndexSet, ArithRangeFrom, ArithRangeIter, IndexSet, IndexSetIter};
pub use node::{CompiledExpr, CompiledExprMulti, InvariantGroup, Node};
pub use parse::{parse, parse_with_ext, validate, validate_with_ext};
pub trait VarMap {
fn ndim(&self) -> usize;
fn resolve_alias(&self, name: &str) -> Option<usize>;
fn primary_prefix(&self) -> &str;
}
macro_rules! define_f0 {
($($variant:ident => $str:literal = $body:expr),* $(,)?) => {
#[derive(Debug, Clone, Copy)]
pub enum F0 {
$($variant,)*
}
impl F0 {
pub fn to_num(self) -> f64 {
match self {
$(Self::$variant => $body,)*
}
}
pub const NAMES: &'static [&'static str] = &[$($str,)*];
}
impl FromStr for F0 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($str => Ok(Self::$variant),)*
_ => Err(format!("unknown const '{s}'")),
}
}
}
};
}
macro_rules! define_f1 {
($($variant:ident => $str:literal = $body:expr),* $(,)?) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum F1 {
$($variant,)*
}
impl F1 {
#[inline]
pub fn to_fn(self) -> fn(f64) -> f64 {
match self {
$(Self::$variant => $body,)*
}
}
pub const NAMES: &'static [&'static str] = &[$($str,)*];
}
impl FromStr for F1 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($str => Ok(Self::$variant),)*
_ => Err(format!("unknown function '{s}'")),
}
}
}
};
}
macro_rules! define_f2 {
($($variant:ident => $str:literal = $body:expr),* $(,)?) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum F2 {
$($variant,)*
}
impl F2 {
#[inline]
pub fn to_fn(self) -> fn(f64, f64) -> f64 {
match self {
$(Self::$variant => $body,)*
}
}
pub const NAMES: &'static [&'static str] = &[$($str,)*];
}
impl FromStr for F2 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($str => Ok(Self::$variant),)*
_ => Err(format!("unknown function '{s}'")),
}
}
}
};
}
define_f0! {
PI => "PI" = std::f64::consts::PI,
E => "E" = std::f64::consts::E,
}
define_f1! {
Sin => "sin" = f64::sin,
Cos => "cos" = f64::cos,
Tan => "tan" = f64::tan,
Asin => "asin" = f64::asin,
Acos => "acos" = f64::acos,
Atan => "atan" = f64::atan,
Sinh => "sinh" = f64::sinh,
Cosh => "cosh" = f64::cosh,
Tanh => "tanh" = f64::tanh,
Sqrt => "sqrt" = f64::sqrt,
Cbrt => "cbrt" = f64::cbrt,
Exp => "exp" = f64::exp,
Ln => "ln" = f64::ln,
Log10 => "log10" = f64::log10,
Log2 => "log2" = f64::log2,
Floor => "floor" = f64::floor,
Ceil => "ceil" = f64::ceil,
Round => "round" = f64::round,
Trunc => "trunc" = f64::trunc,
Abs => "abs" = f64::abs,
}
define_f2! {
Atan2 => "atan2" = f64::atan2,
Pow => "pow" = |a, b| {
if a == 0.0 && b == 0.0 {
1.0
} else {
let exp_int = b as i32;
if (exp_int as f64) == b {
a.powi(exp_int)
} else {
a.powf(b)
}
}
},
}
pub fn f0_list() -> String {
F0::NAMES.join(", ")
}
pub fn f1_list() -> String {
F1::NAMES.join(", ")
}
pub fn f2_list() -> String {
F2::NAMES.join(", ")
}
pub trait ExtF0: Debug + Clone + PartialEq {
fn to_num(&self) -> f64;
fn names(&self) -> &[&str];
fn list(&self) -> String {
self.names().join(", ")
}
}
pub trait ExtF1: Debug + Clone + PartialEq {
fn to_fn(&self) -> fn(f64) -> f64;
fn names(&self) -> &[&str];
fn list(&self) -> String {
self.names().join(", ")
}
}
pub trait ExtF2: Debug + Clone + PartialEq {
fn to_fn(&self) -> fn(f64, f64) -> f64;
fn names(&self) -> &[&str];
fn list(&self) -> String {
self.names().join(", ")
}
}
#[macro_export]
macro_rules! define_ext_f0 {
($enum_name:ident) => {
define_ext_f0!($enum_name,);
};
($enum_name:ident, $($variant:ident => $str:literal = $body:expr),* $(,)?) => {
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum $enum_name {
$($variant,)*
}
impl hypervox_expr::ExtF0 for $enum_name {
fn to_num(&self) -> f64 {
match *self {
$(Self::$variant => $body,)*
}
}
fn names(&self) -> &[&str] {
&[$($str,)*]
}
}
impl std::str::FromStr for $enum_name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($str => Ok(Self::$variant),)*
_ => Err(format!("unknown const '{s}'")),
}
}
}
};
}
#[macro_export]
macro_rules! define_ext_f1 {
($enum_name:ident) => {
define_ext_f1!($enum_name,);
};
($enum_name:ident, $($variant:ident => $str:literal = $body:expr),* $(,)?) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum $enum_name {
$($variant,)*
}
impl hypervox_expr::ExtF1 for $enum_name {
#[inline]
fn to_fn(&self) -> fn(f64) -> f64 {
match *self {
$(Self::$variant => $body,)*
}
}
fn names(&self) -> &[&str] {
&[$($str,)*]
}
}
impl std::str::FromStr for $enum_name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($str => Ok(Self::$variant),)*
_ => Err(format!("unknown function '{s}'")),
}
}
}
};
}
#[macro_export]
macro_rules! define_ext_f2 {
($enum_name:ident) => {
define_ext_f2!($enum_name,);
};
($enum_name:ident, $($variant:ident => $str:literal = $body:expr),* $(,)?) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum $enum_name {
$($variant,)*
}
impl hypervox_expr::ExtF2 for $enum_name {
#[inline]
fn to_fn(&self) -> fn(f64, f64) -> f64 {
match *self {
$(Self::$variant => $body,)*
}
}
fn names(&self) -> &[&str] {
&[$($str,)*]
}
}
impl std::str::FromStr for $enum_name {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
$($str => Ok(Self::$variant),)*
_ => Err(format!("unknown function '{s}'")),
}
}
}
};
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NoExtF {}
impl ExtF0 for NoExtF {
fn to_num(&self) -> f64 {
unreachable!("NoExtF should never be called")
}
fn names(&self) -> &[&str] {
unreachable!("NoExtF should never be called")
}
}
impl ExtF1 for NoExtF {
fn to_fn(&self) -> fn(f64) -> f64 {
unreachable!("NoExtF should never be called")
}
fn names(&self) -> &[&str] {
unreachable!("NoExtF should never be called")
}
}
impl ExtF2 for NoExtF {
fn to_fn(&self) -> fn(f64, f64) -> f64 {
unreachable!("NoExtF should never be called")
}
fn names(&self) -> &[&str] {
unreachable!("NoExtF should never be called")
}
}
impl FromStr for NoExtF {
type Err = String;
fn from_str(_s: &str) -> Result<Self, Self::Err> {
Err(String::from("no external constants or functions defined"))
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum LexerToken {
Num(f64),
Ident(String),
Plus,
Minus,
Star,
Slash,
Caret,
Percent,
Pipe,
LParen,
RParen,
Comma,
Eof,
}
impl Display for LexerToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Num(v) => write!(f, "{v}"),
Self::Ident(s) => write!(f, "'{s}'"),
Self::Plus => write!(f, "'+'"),
Self::Minus => write!(f, "'-'"),
Self::Star => write!(f, "'*'"),
Self::Slash => write!(f, "'/'"),
Self::Caret => write!(f, "'^'"),
Self::Percent => write!(f, "'%'"),
Self::Pipe => write!(f, "'|'"),
Self::LParen => write!(f, "'('"),
Self::RParen => write!(f, "')'"),
Self::Comma => write!(f, "','"),
Self::Eof => write!(f, "end of expression"),
}
}
}