Added "degree" keyword with option to choose which unit that is
This commit is contained in:
parent
22a5faa6be
commit
c7141e87ad
@ -7,9 +7,10 @@ use crate::TextOperator::{Of, To};
|
||||
use crate::Constant::{E, Pi};
|
||||
use crate::LexerKeyword::{In, PercentChar, Per, Mercury, Hg, PoundForce, PoundWord, Force, DoubleQuotes};
|
||||
use crate::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round, Sin, Sqrt, Tan};
|
||||
use crate::units::Unit;
|
||||
use crate::units::Unit::*;
|
||||
|
||||
pub fn lex(input: &str, allow_trailing_operators: bool) -> Result<TokenVector, String> {
|
||||
pub fn lex(input: &str, allow_trailing_operators: bool, default_degree: Unit) -> Result<TokenVector, String> {
|
||||
|
||||
let mut input = input.replace(",", ""); // ignore commas
|
||||
|
||||
@ -25,7 +26,7 @@ pub fn lex(input: &str, allow_trailing_operators: bool) -> Result<TokenVector, S
|
||||
let mut chars = input.chars().peekable();
|
||||
let mut tokens: TokenVector = vec![];
|
||||
let max_word_length = 30;
|
||||
|
||||
|
||||
let mut left_paren_count = 0;
|
||||
let mut right_paren_count = 0;
|
||||
|
||||
@ -321,6 +322,7 @@ pub fn lex(input: &str, allow_trailing_operators: bool) -> Result<TokenVector, S
|
||||
"k" | "kelvin" | "kelvins" => tokens.push(Token::Unit(Kelvin)),
|
||||
"c" | "celcius" => tokens.push(Token::Unit(Celcius)),
|
||||
"f" | "fahrenheit" | "fahrenheits" => tokens.push(Token::Unit(Fahrenheit)),
|
||||
"deg" | "degree" | "degrees" => tokens.push(Token::Unit(default_degree)),
|
||||
|
||||
_ => {
|
||||
return Err(format!("Invalid string: {}", string));
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use crate::units::Unit;
|
||||
use std::time::{Instant};
|
||||
use decimal::d128;
|
||||
|
||||
@ -91,17 +92,17 @@ fn main() {
|
||||
use std::env;
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() >= 2 {
|
||||
eval(&args[1], true);
|
||||
eval(&args[1], true, Unit::Celcius);
|
||||
} else {
|
||||
println!("No argument supplied");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval(input: &str, allow_trailing_operators: bool) {
|
||||
pub fn eval(input: &str, allow_trailing_operators: bool, default_degree: Unit) {
|
||||
|
||||
let lex_start = Instant::now();
|
||||
|
||||
match lexer::lex(input, allow_trailing_operators) {
|
||||
|
||||
match lexer::lex(input, allow_trailing_operators, default_degree) {
|
||||
Ok(tokens) => {
|
||||
let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user