Skip to content

Commit

Permalink
rework Identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Sep 20, 2023
1 parent 30ac065 commit f281138
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 112 deletions.
5 changes: 2 additions & 3 deletions src/element/function_call.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
}
11 changes: 2 additions & 9 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use core::str;
/* Crate imports */
use crate::{
token::{Constant, Function, Operator, Token},
token::{Operator, Token},
trust_me::trust_me,
};

Expand Down Expand Up @@ -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")),
};
Expand Down
74 changes: 74 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
@@ -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,
}
32 changes: 0 additions & 32 deletions src/token/constants.rs

This file was deleted.

34 changes: 0 additions & 34 deletions src/token/functions.rs

This file was deleted.

24 changes: 0 additions & 24 deletions src/token/mod.rs

This file was deleted.

10 changes: 0 additions & 10 deletions src/token/operators.rs

This file was deleted.

0 comments on commit f281138

Please sign in to comment.