Added constant generation

This commit is contained in:
2023-06-13 20:15:10 -07:00
parent 86a3f506ba
commit 9fdabb80d3
10 changed files with 466 additions and 362 deletions

View File

@ -1,46 +0,0 @@
use crate::parser::Token;
use crate::parser::Constant;
use crate::quantity::Quantity;
use crate::quantity::Unit;
use super::EvalError;
pub fn eval_constant(c: &Constant) -> Result<Token, EvalError> {
Ok(match c {
// Mathematical constants
// 100 digits of each.
Constant::Pi => { Token::Quantity(Quantity::new_float_from_string(
"3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"
).unwrap())},
Constant::E => { Token::Quantity(Quantity::new_float_from_string(
"2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427"
).unwrap()) },
Constant::Phi => { Token::Quantity(Quantity::new_float_from_string(
"1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137"
).unwrap()) },
Constant::MPG => {
let mut q = Quantity::new_float_from_string("1").unwrap();
q.set_unit(
(
Unit::from_string("mile").unwrap() /
Unit::from_string("gallon").unwrap()
).unit
);
Token::Quantity(q)
},
Constant::MPH => {
let mut q = Quantity::new_float_from_string("1").unwrap();
q.set_unit(
(
Unit::from_string("mile").unwrap() /
Unit::from_string("hour").unwrap()
).unit
);
Token::Quantity(q)
},
})
}

View File

@ -2,7 +2,6 @@ use crate::parser::Token;
use crate::parser::Operator;
use super::operator::eval_operator;
use super::constant::eval_constant;
use super::function::eval_function;
use super::EvalError;
@ -13,8 +12,8 @@ pub fn evaluate(t: &Token) -> Result<Token, EvalError> {
coords.push(0);
'outer: loop {
let mut h = &mut g;
for t in coords.iter() {
let inner = h.get_args_mut();
@ -32,7 +31,7 @@ pub fn evaluate(t: &Token) -> Result<Token, EvalError> {
loop {
e = match e {
Token::Quantity(_) => { break; },
Token::Constant(c) => { eval_constant(&c)? }
Token::Constant(c) => { evaluate(&c.value()).unwrap() }
Token::Operator(Operator::Function(f), v) => { eval_function(&f, &v)? }
Token::Operator(o, v) => { eval_operator(&o, &v)? }
};
@ -50,22 +49,13 @@ pub fn evaluate(t: &Token) -> Result<Token, EvalError> {
}
match h {
Token::Operator(_,_) => {
coords.push(0);
continue 'outer;
},
Token::Constant(_) => {
coords.push(0);
continue 'outer;
},
Token::Operator(_,_) => { coords.push(0); },
Token::Constant(_) => { coords.push(0); },
Token::Quantity(_) => {
let l = coords.pop().unwrap();
coords.push(l + 1);
continue 'outer;
}
};
}

View File

@ -74,14 +74,7 @@ impl PreToken {
},
PreToken::PreWord(l, s) => {
let c = match &s[..] {
"π"|"pi" => { Some(Constant::Pi)},
"e" => { Some(Constant::E) },
"phi"|"φ" => { Some(Constant::Phi) },
"mpg" => { Some(Constant::MPG) },
"mph" => { Some(Constant::MPH) },
_ => { None }
};
let c = Constant::from_string(&s);
if c.is_some() {
return Ok(Token::Constant(c.unwrap()));

View File

@ -1,27 +0,0 @@
#[derive(Debug)]
#[derive(Clone)]
pub enum Constant {
// Fake units
MPG,
MPH,
// Mathematics
Pi,
Phi,
E,
}
impl Constant {
pub fn to_string(&self) -> String {
match self {
// Fake units
Constant::MPG => { String::from("mpg") },
Constant::MPH => { String::from("mph") },
// Mathematics
Constant::Pi => { String::from("π") },
Constant::Phi => { String::from("φ") },
Constant::E => { String::from("e") }
}
}
}

View File

@ -1,9 +1,11 @@
mod operator;
mod function;
mod token;
mod constant;
pub use self::operator::Operator;
pub use self::function::Function;
pub use self::token::Token;
pub use self::constant::Constant;
use super::parse;
include!(concat!(env!("OUT_DIR"), "/constants.rs"));