[READ-ONLY] Mirror of https://github.com/probablykasper/cpc. Text calculator with support for units and conversion cpc.kasper.space
calculator cli conversion converter library math package units units-converter wasm website
0

Configure Feed

Select the types of activity you want to include in your feed.

Remove `degrees` keyword

Kasper (Jan 13, 2024, 9:53 AM +0100) be51ec4f 0dcd13f7

+15 -17
+4
CHANGELOG.md
··· 1 1 # Changelog 2 2 3 + ## Next 4 + - Remove the `degrees` keyword which referred to `celcius` by default 5 + - Remove the `default_degrees` argument from `eval()` and `lex()`. Not necessary now that the `degrees` keyword is removed 6 + 3 7 ## 1.9.3 - 2023 sep 20 4 8 - Fix negative unary `-` always having higher precedence than `^`. This resulted in `-3^2` returning `9` instead of `-9` 5 9
+6 -10
src/lexer.rs
··· 9 9 use crate::Constant::{E, Pi}; 10 10 use crate::LexerKeyword::{In, PercentChar, Per, Mercury, Hg, PoundForce, Force, DoubleQuotes, Revolution}; 11 11 use crate::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round, Sin, Sqrt, Tan}; 12 - use crate::units::Unit; 13 12 use crate::units::Unit::*; 14 13 use unicode_segmentation::{Graphemes, UnicodeSegmentation}; 15 14 ··· 595 594 "k" | "kelvin" | "kelvins" => Token::Unit(Kelvin), 596 595 "c" | "celsius" => Token::Unit(Celsius), 597 596 "f" | "fahrenheit" | "fahrenheits" => Token::Unit(Fahrenheit), 598 - "deg" | "degree" | "degrees" => Token::Unit(lexer.default_degree), 599 597 600 598 string => { 601 599 return Err(format!("Invalid string: {}", string)); ··· 610 608 right_paren_count: u16, 611 609 chars: Peekable<Graphemes<'a>>, 612 610 tokens: Vec<Token>, 613 - default_degree: Unit, 614 611 } 615 612 616 613 /// Lex an input string and returns [`Token`]s 617 - pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) -> Result<Vec<Token>, String> { 614 + pub fn lex(input: &str, remove_trailing_operator: bool) -> Result<Vec<Token>, String> { 618 615 let mut input = input.replace(',', "").to_ascii_lowercase(); 619 616 620 617 if remove_trailing_operator { ··· 631 628 right_paren_count: 0, 632 629 chars: UnicodeSegmentation::graphemes(input.as_str(), true).peekable(), 633 630 tokens: Vec::new(), 634 - default_degree, 635 631 }; 636 632 637 633 while let Some(c) = lexer.chars.next() { ··· 945 941 let nonplural_data_units = Regex::new(r"(bit|byte)s").unwrap(); 946 942 947 943 let run_lex = |input: &str, expected_tokens: Vec<Token>| { 948 - let tokens = match lex(input, false, Unit::Celsius) { 944 + let tokens = match lex(input, false) { 949 945 Ok(tokens) => tokens, 950 946 Err(e) => { 951 947 panic!("lex error: {}\nrun_lex input: {}", e, input); ··· 956 952 957 953 // Prove we can handle multiple spaces wherever we handle a single space 958 954 let input_extra_spaces = input.replace(" ", " "); 959 - let tokens_extra_spaces = lex(&input_extra_spaces, false, Unit::Celsius).unwrap(); 955 + let tokens_extra_spaces = lex(&input_extra_spaces, false).unwrap(); 960 956 assert!(tokens_extra_spaces == expected_tokens, "{info_msg}"); 961 957 962 958 // Prove we don't need spaces around operators 963 959 let input_stripped_spaces = strip_operator_spacing.replace_all(input, "$1"); 964 - let tokens_stripped_spaces = lex(&input_stripped_spaces, false, Unit::Celsius).unwrap(); 960 + let tokens_stripped_spaces = lex(&input_stripped_spaces, false).unwrap(); 965 961 assert!(tokens_stripped_spaces == expected_tokens, "{info_msg}"); 966 962 967 963 // Prove we don't need a space after a digit 968 964 let input_afterdigit_stripped_spaces = strip_afterdigit_spacing.replace_all(input, "$1"); 969 - let tokens_afterdigit_stripped_spaces = lex(&input_afterdigit_stripped_spaces, false, Unit::Celsius).unwrap(); 965 + let tokens_afterdigit_stripped_spaces = lex(&input_afterdigit_stripped_spaces, false).unwrap(); 970 966 assert!(tokens_afterdigit_stripped_spaces == expected_tokens, "{info_msg}"); 971 967 }; 972 968 ··· 975 971 976 972 // Prove plural and non-plural data units behave identically 977 973 let input_nonplural_units = nonplural_data_units.replace_all(input, "$1"); 978 - let tokens_nonplural_units = lex(&input_nonplural_units, false, Unit::Celsius).unwrap(); 974 + let tokens_nonplural_units = lex(&input_nonplural_units, false).unwrap(); 979 975 let info_msg = format!("run_datarate_lex input: {}\nexpected: {:?}\nreceived: {:?}", input, expected_tokens, tokens_nonplural_units); 980 976 assert!(tokens_nonplural_units == expected_tokens, "{info_msg}"); 981 977 };
+4 -5
src/lib.rs
··· 19 19 //! use cpc::eval; 20 20 //! use cpc::units::Unit; 21 21 //! 22 - //! match eval("3m + 1cm", true, Unit::Celsius, false) { 22 + //! match eval("3m + 1cm", true, false) { 23 23 //! Ok(answer) => { 24 24 //! // answer: Number { value: 301, unit: Unit::Centimeter } 25 25 //! println!("Evaluated value: {} {:?}", answer.value, answer.unit) ··· 230 230 /// use cpc::eval; 231 231 /// use cpc::units::Unit; 232 232 /// 233 - /// match eval("3m + 1cm", true, Unit::Celsius, false) { 233 + /// match eval("3m + 1cm", true, false) { 234 234 /// Ok(answer) => { 235 235 /// // answer: Number { value: 301, unit: Unit::Centimeter } 236 236 /// println!("Evaluated value: {} {:?}", answer.value, answer.unit) ··· 243 243 pub fn eval( 244 244 input: &str, 245 245 allow_trailing_operators: bool, 246 - default_degree: Unit, 247 246 verbose: bool, 248 247 ) -> Result<Number, String> { 249 248 let lex_start = Instant::now(); 250 249 251 - match lexer::lex(input, allow_trailing_operators, default_degree) { 250 + match lexer::lex(input, allow_trailing_operators) { 252 251 Ok(tokens) => { 253 252 let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32; 254 253 if verbose { ··· 296 295 use super::*; 297 296 298 297 fn default_eval(input: &str) -> Number { 299 - eval(input, true, units::Unit::Celsius, false).unwrap() 298 + eval(input, true, false).unwrap() 300 299 } 301 300 302 301 #[test]
+1 -2
src/main.rs
··· 1 1 use cpc::eval; 2 - use cpc::units::Unit; 3 2 use std::env; 4 3 use std::process::exit; 5 4 ··· 61 60 } 62 61 }; 63 62 64 - match eval(&expression, true, Unit::Celsius, verbose) { 63 + match eval(&expression, true, verbose) { 65 64 Ok(answer) => { 66 65 if !verbose { 67 66 println!("{answer}");