···11# Changelog
2233+## Next
44+- Remove the `degrees` keyword which referred to `celcius` by default
55+- Remove the `default_degrees` argument from `eval()` and `lex()`. Not necessary now that the `degrees` keyword is removed
66+37## 1.9.3 - 2023 sep 20
48- Fix negative unary `-` always having higher precedence than `^`. This resulted in `-3^2` returning `9` instead of `-9`
59
+6-10
src/lexer.rs
···99use crate::Constant::{E, Pi};
1010use crate::LexerKeyword::{In, PercentChar, Per, Mercury, Hg, PoundForce, Force, DoubleQuotes, Revolution};
1111use crate::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round, Sin, Sqrt, Tan};
1212-use crate::units::Unit;
1312use crate::units::Unit::*;
1413use unicode_segmentation::{Graphemes, UnicodeSegmentation};
1514···595594 "k" | "kelvin" | "kelvins" => Token::Unit(Kelvin),
596595 "c" | "celsius" => Token::Unit(Celsius),
597596 "f" | "fahrenheit" | "fahrenheits" => Token::Unit(Fahrenheit),
598598- "deg" | "degree" | "degrees" => Token::Unit(lexer.default_degree),
599597600598 string => {
601599 return Err(format!("Invalid string: {}", string));
···610608 right_paren_count: u16,
611609 chars: Peekable<Graphemes<'a>>,
612610 tokens: Vec<Token>,
613613- default_degree: Unit,
614611}
615612616613/// Lex an input string and returns [`Token`]s
617617-pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) -> Result<Vec<Token>, String> {
614614+pub fn lex(input: &str, remove_trailing_operator: bool) -> Result<Vec<Token>, String> {
618615 let mut input = input.replace(',', "").to_ascii_lowercase();
619616620617 if remove_trailing_operator {
···631628 right_paren_count: 0,
632629 chars: UnicodeSegmentation::graphemes(input.as_str(), true).peekable(),
633630 tokens: Vec::new(),
634634- default_degree,
635631 };
636632637633 while let Some(c) = lexer.chars.next() {
···945941 let nonplural_data_units = Regex::new(r"(bit|byte)s").unwrap();
946942947943 let run_lex = |input: &str, expected_tokens: Vec<Token>| {
948948- let tokens = match lex(input, false, Unit::Celsius) {
944944+ let tokens = match lex(input, false) {
949945 Ok(tokens) => tokens,
950946 Err(e) => {
951947 panic!("lex error: {}\nrun_lex input: {}", e, input);
···956952957953 // Prove we can handle multiple spaces wherever we handle a single space
958954 let input_extra_spaces = input.replace(" ", " ");
959959- let tokens_extra_spaces = lex(&input_extra_spaces, false, Unit::Celsius).unwrap();
955955+ let tokens_extra_spaces = lex(&input_extra_spaces, false).unwrap();
960956 assert!(tokens_extra_spaces == expected_tokens, "{info_msg}");
961957962958 // Prove we don't need spaces around operators
963959 let input_stripped_spaces = strip_operator_spacing.replace_all(input, "$1");
964964- let tokens_stripped_spaces = lex(&input_stripped_spaces, false, Unit::Celsius).unwrap();
960960+ let tokens_stripped_spaces = lex(&input_stripped_spaces, false).unwrap();
965961 assert!(tokens_stripped_spaces == expected_tokens, "{info_msg}");
966962967963 // Prove we don't need a space after a digit
968964 let input_afterdigit_stripped_spaces = strip_afterdigit_spacing.replace_all(input, "$1");
969969- let tokens_afterdigit_stripped_spaces = lex(&input_afterdigit_stripped_spaces, false, Unit::Celsius).unwrap();
965965+ let tokens_afterdigit_stripped_spaces = lex(&input_afterdigit_stripped_spaces, false).unwrap();
970966 assert!(tokens_afterdigit_stripped_spaces == expected_tokens, "{info_msg}");
971967 };
972968···975971976972 // Prove plural and non-plural data units behave identically
977973 let input_nonplural_units = nonplural_data_units.replace_all(input, "$1");
978978- let tokens_nonplural_units = lex(&input_nonplural_units, false, Unit::Celsius).unwrap();
974974+ let tokens_nonplural_units = lex(&input_nonplural_units, false).unwrap();
979975 let info_msg = format!("run_datarate_lex input: {}\nexpected: {:?}\nreceived: {:?}", input, expected_tokens, tokens_nonplural_units);
980976 assert!(tokens_nonplural_units == expected_tokens, "{info_msg}");
981977 };
+4-5
src/lib.rs
···1919//! use cpc::eval;
2020//! use cpc::units::Unit;
2121//!
2222-//! match eval("3m + 1cm", true, Unit::Celsius, false) {
2222+//! match eval("3m + 1cm", true, false) {
2323//! Ok(answer) => {
2424//! // answer: Number { value: 301, unit: Unit::Centimeter }
2525//! println!("Evaluated value: {} {:?}", answer.value, answer.unit)
···230230/// use cpc::eval;
231231/// use cpc::units::Unit;
232232///
233233-/// match eval("3m + 1cm", true, Unit::Celsius, false) {
233233+/// match eval("3m + 1cm", true, false) {
234234/// Ok(answer) => {
235235/// // answer: Number { value: 301, unit: Unit::Centimeter }
236236/// println!("Evaluated value: {} {:?}", answer.value, answer.unit)
···243243pub fn eval(
244244 input: &str,
245245 allow_trailing_operators: bool,
246246- default_degree: Unit,
247246 verbose: bool,
248247) -> Result<Number, String> {
249248 let lex_start = Instant::now();
250249251251- match lexer::lex(input, allow_trailing_operators, default_degree) {
250250+ match lexer::lex(input, allow_trailing_operators) {
252251 Ok(tokens) => {
253252 let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32;
254253 if verbose {
···296295 use super::*;
297296298297 fn default_eval(input: &str) -> Number {
299299- eval(input, true, units::Unit::Celsius, false).unwrap()
298298+ eval(input, true, false).unwrap()
300299 }
301300302301 #[test]