From f28113822c8537c820fa2aad5e591ff985a89c33 Mon Sep 17 00:00:00 2001 From: vic1707 <28602203+vic1707@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:31:05 +0200 Subject: [PATCH] rework Identifiers --- src/element/function_call.rs | 5 +-- src/lexer.rs | 11 +----- src/token.rs | 74 ++++++++++++++++++++++++++++++++++++ src/token/constants.rs | 32 ---------------- src/token/functions.rs | 34 ----------------- src/token/mod.rs | 24 ------------ src/token/operators.rs | 10 ----- 7 files changed, 78 insertions(+), 112 deletions(-) create mode 100644 src/token.rs delete mode 100644 src/token/constants.rs delete mode 100644 src/token/functions.rs delete mode 100644 src/token/mod.rs delete mode 100644 src/token/operators.rs diff --git a/src/element/function_call.rs b/src/element/function_call.rs index 444b689..309fc51 100644 --- a/src/element/function_call.rs +++ b/src/element/function_call.rs @@ -1,15 +1,14 @@ /* Crate imports */ use super::Element; -use crate::token::Function; #[derive(Debug, PartialEq, PartialOrd)] pub struct FunctionCall<'a> { - func: Function, + func: fn(f64) -> f64, arg: Element<'a>, } impl<'a> FunctionCall<'a> { - pub const fn new(func: Function, arg: Element<'a>) -> Self { + pub const fn new(func: fn(f64) -> f64, arg: Element<'a>) -> Self { Self { func, arg } } } diff --git a/src/lexer.rs b/src/lexer.rs index 47cfa39..f05bc0b 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -2,7 +2,7 @@ use core::str; /* Crate imports */ use crate::{ - token::{Constant, Function, Operator, Token}, + token::{Operator, Token}, trust_me::trust_me, }; @@ -84,17 +84,10 @@ impl<'a> Iterator for Lexer<'a> { let indent = trust_me! { str::from_utf8_unchecked(indent_bytes) }; - let tok = Function::try_from(indent) - .map(Token::Function) - .or_else(|_| { - Constant::try_from(indent).map(Token::Constant) - }) - // if failed return a variable in ok variant, don't care about the error - .unwrap_or(Token::Variable(indent)); // early return is necessary here so we don't // advance past the last character of the // current identifier - return Some(Ok(tok)); + return Some(Ok(Token::Identifier(indent.into()))); }, _ => return Some(Err("Illegal character")), }; diff --git a/src/token.rs b/src/token.rs new file mode 100644 index 0000000..f94920f --- /dev/null +++ b/src/token.rs @@ -0,0 +1,74 @@ +/* Clippy config */ +#![allow(clippy::pub_use)] + +#[derive(Debug, PartialEq, PartialOrd)] +#[non_exhaustive] +pub enum Token<'a> { + Number(f64), + Operator(Operator), + /// Left parenthesis. + LParen, + /// Right parenthesis. + RParen, + /// Identifiers + Identifier(Identifier<'a>), +} + +#[derive(Debug, PartialEq, PartialOrd)] +pub enum Identifier<'a> { + Function(fn(f64) -> f64), + Constant(f64), + Variable(&'a str), +} + +impl<'a> From<&'a str> for Identifier<'a> { + fn from(value: &'a str) -> Self { + match value { + /* Constants */ + "pi" => Identifier::Constant(core::f64::consts::PI), + "e" => Identifier::Constant(core::f64::consts::E), + /* Functions */ + // sin + "sin" => Identifier::Function(f64::sin), + "sinh" => Identifier::Function(f64::sinh), + "asin" => Identifier::Function(f64::asin), + "asinh" => Identifier::Function(f64::asinh), + // cos + "cos" => Identifier::Function(f64::cos), + "cosh" => Identifier::Function(f64::cosh), + "acos" => Identifier::Function(f64::acos), + "acosh" => Identifier::Function(f64::acosh), + // tan + "tan" => Identifier::Function(f64::tan), + "tanh" => Identifier::Function(f64::tanh), + "atan" => Identifier::Function(f64::atan), + "atanh" => Identifier::Function(f64::atanh), + // log + "ln" => Identifier::Function(f64::ln), + "log" => Identifier::Function(f64::log10), + // roots + "sqrt" => Identifier::Function(f64::sqrt), + "cbrt" => Identifier::Function(f64::cbrt), + // misc + "exp" => Identifier::Function(f64::exp), + "abs" => Identifier::Function(f64::abs), + "floor" => Identifier::Function(f64::floor), + "ceil" => Identifier::Function(f64::ceil), + "round" => Identifier::Function(f64::round), + "trunc" => Identifier::Function(f64::trunc), + /* Variables */ + _ => Identifier::Variable(value), + } + } +} + +#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] +pub enum Operator { + // Factorial, + Plus, + Minus, + Times, + Divide, + Power, + Modulo, +} diff --git a/src/token/constants.rs b/src/token/constants.rs deleted file mode 100644 index 4b5bfe6..0000000 --- a/src/token/constants.rs +++ /dev/null @@ -1,32 +0,0 @@ -/* Built-in imports */ -use core::f64; -/* Crate imports */ -use crate::yeet::yeet; - -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] -pub enum Constant { - Pi, - Exponential, -} - -impl TryFrom<&str> for Constant { - type Error = &'static str; - fn try_from(value: &str) -> Result { - let val = match value { - "pi" => Self::Pi, - "e" => Self::Exponential, - _ => yeet!("Invalid constant"), - }; - - Ok(val) - } -} - -impl Constant { - pub const fn value(&self) -> f64 { - match *self { - Self::Pi => f64::consts::PI, - Self::Exponential => f64::consts::E, - } - } -} diff --git a/src/token/functions.rs b/src/token/functions.rs deleted file mode 100644 index e697f7d..0000000 --- a/src/token/functions.rs +++ /dev/null @@ -1,34 +0,0 @@ -/* Crate imports */ -use crate::yeet::yeet; - -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] -pub struct Function(fn(f64) -> f64); - -impl TryFrom<&str> for Function { - type Error = &'static str; - fn try_from(value: &str) -> Result { - let func = match value { - "sin" => f64::sin, - "cos" => f64::cos, - "tan" => f64::tan, - "asin" => f64::asin, - "acos" => f64::acos, - "atan" => f64::atan, - "sinh" => f64::sinh, - "cosh" => f64::cosh, - "tanh" => f64::tanh, - "asinh" => f64::asinh, - "acosh" => f64::acosh, - "atanh" => f64::atanh, - "log" => f64::log10, - "ln" => f64::ln, - "exp" => f64::exp, - "sqrt" => f64::sqrt, - "cbrt" => f64::cbrt, - "abs" => f64::abs, - _ => yeet!("Invalid function"), - }; - - Ok(Self(func)) - } -} diff --git a/src/token/mod.rs b/src/token/mod.rs deleted file mode 100644 index 3141d95..0000000 --- a/src/token/mod.rs +++ /dev/null @@ -1,24 +0,0 @@ -/* Clippy config */ -#![allow(clippy::pub_use)] -/* Modules */ -mod constants; -mod functions; -mod operators; -/* Exports */ -pub use constants::Constant; -pub use functions::Function; -pub use operators::Operator; - -#[derive(Debug, PartialEq, PartialOrd)] -#[non_exhaustive] -pub enum Token<'a> { - Number(f64), - Constant(Constant), - Function(Function), - Operator(Operator), - Variable(&'a str), - /// Left parenthesis. - LParen, - /// Right parenthesis. - RParen, -} diff --git a/src/token/operators.rs b/src/token/operators.rs deleted file mode 100644 index db96771..0000000 --- a/src/token/operators.rs +++ /dev/null @@ -1,10 +0,0 @@ -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] -pub enum Operator { - // Factorial, - Plus, - Minus, - Times, - Divide, - Power, - Modulo, -}