Added "degree" keyword with option to choose which unit that is

This commit is contained in:
Kasper 2020-01-14 13:53:06 +01:00
parent 22a5faa6be
commit c7141e87ad
2 changed files with 9 additions and 6 deletions

View File

@ -7,9 +7,10 @@ use crate::TextOperator::{Of, To};
use crate::Constant::{E, Pi}; use crate::Constant::{E, Pi};
use crate::LexerKeyword::{In, PercentChar, Per, Mercury, Hg, PoundForce, PoundWord, Force, DoubleQuotes}; 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::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round, Sin, Sqrt, Tan};
use crate::units::Unit;
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 let mut input = input.replace(",", ""); // ignore commas
@ -321,6 +322,7 @@ pub fn lex(input: &str, allow_trailing_operators: bool) -> Result<TokenVector, S
"k" | "kelvin" | "kelvins" => tokens.push(Token::Unit(Kelvin)), "k" | "kelvin" | "kelvins" => tokens.push(Token::Unit(Kelvin)),
"c" | "celcius" => tokens.push(Token::Unit(Celcius)), "c" | "celcius" => tokens.push(Token::Unit(Celcius)),
"f" | "fahrenheit" | "fahrenheits" => tokens.push(Token::Unit(Fahrenheit)), "f" | "fahrenheit" | "fahrenheits" => tokens.push(Token::Unit(Fahrenheit)),
"deg" | "degree" | "degrees" => tokens.push(Token::Unit(default_degree)),
_ => { _ => {
return Err(format!("Invalid string: {}", string)); return Err(format!("Invalid string: {}", string));

View File

@ -1,3 +1,4 @@
use crate::units::Unit;
use std::time::{Instant}; use std::time::{Instant};
use decimal::d128; use decimal::d128;
@ -91,17 +92,17 @@ fn main() {
use std::env; use std::env;
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() >= 2 { if args.len() >= 2 {
eval(&args[1], true); eval(&args[1], true, Unit::Celcius);
} else { } else {
println!("No argument supplied"); 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(); let lex_start = Instant::now();
match lexer::lex(input, allow_trailing_operators) { match lexer::lex(input, allow_trailing_operators, default_degree) {
Ok(tokens) => { Ok(tokens) => {
let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32; let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32;