Compare commits

...
Author SHA1 Message Date
d6b9d7a407
refactor: use lib.rs in main.rs
this avoids duplicating unit tests, for instance

https://stackoverflow.com/a/77038955
2026-10-04 16:41:38 -07:00
564f95aa32
refactor: use Node::from from parsed Floats
this is a little nicer, hopefully it has no perf penalty
2026-10-04 16:35:22 -07:00
0afc7aab25
feat: allow changing max_digits for pretty_eval 2026-10-04 16:34:08 -07:00
3 changed files with 24 additions and 34 deletions

View file

@ -2,9 +2,7 @@ use rug::Float;
use crate::nodes::{self, Error}; use crate::nodes::{self, Error};
pub const MAX_DIGITS: usize = 10; pub fn pretty_eval(mut input: String, max_digits: usize) -> Result<String, Error> {
pub fn pretty_eval(mut input: String) -> Result<String, Error> {
// no need to remove the characters here, // no need to remove the characters here,
// as we ignore the them in all other logic already // as we ignore the them in all other logic already
let should_add_back_alignment = input.contains('&'); let should_add_back_alignment = input.contains('&');
@ -21,7 +19,7 @@ pub fn pretty_eval(mut input: String) -> Result<String, Error> {
let result = nodes::eval(&input)?; let result = nodes::eval(&input)?;
let display_digits = let display_digits =
get_significant_digits(&result) get_significant_digits(&result)
.min(MAX_DIGITS) .min(max_digits)
.max(if result >= 1000 || result < 1 { .max(if result >= 1000 || result < 1 {
1 1
} else if result >= 100 { } else if result >= 100 {
@ -51,20 +49,20 @@ pub fn pretty_eval(mut input: String) -> Result<String, Error> {
} }
#[test] #[test]
fn test_character_readdition() { fn test_character_readdition() {
assert_eq!(pretty_eval(" = 11".into()).unwrap(), " = 11"); assert_eq!(pretty_eval(" = 11".into(), 10).unwrap(), " = 11");
assert_eq!(pretty_eval(" &= 11".into()).unwrap(), " & = 11"); assert_eq!(pretty_eval(" &= 11".into(), 10).unwrap(), " & = 11");
assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11"); assert_eq!(pretty_eval(" & 11".into(), 10).unwrap(), " & 11");
assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\"); assert_eq!(pretty_eval(" & 11 \\".into(), 10).unwrap(), " & 11 \\");
} }
#[test] #[test]
fn test_basic_up_to_999() { fn test_basic_up_to_999() {
assert_eq!(pretty_eval("1".into()).unwrap(), "1"); assert_eq!(pretty_eval("1".into(), 10).unwrap(), "1");
assert_eq!(pretty_eval("10".into()).unwrap(), "10"); assert_eq!(pretty_eval("10".into(), 10).unwrap(), "10");
assert_eq!(pretty_eval("100".into()).unwrap(), "100"); assert_eq!(pretty_eval("100".into(), 10).unwrap(), "100");
assert_eq!(pretty_eval("999".into()).unwrap(), "999"); assert_eq!(pretty_eval("999".into(), 10).unwrap(), "999");
assert_eq!(pretty_eval("1000".into()).unwrap(), "1 * 10^3"); assert_eq!(pretty_eval("1000".into(), 10).unwrap(), "1 * 10^3");
assert_eq!(pretty_eval("11.111111".into()).unwrap(), "11.111111"); assert_eq!(pretty_eval("11.111111".into(), 10).unwrap(), "11.111111");
} }
pub fn get_significant_digits(n: &Float) -> usize { pub fn get_significant_digits(n: &Float) -> usize {

View file

@ -4,8 +4,7 @@ use std::{
process::ExitCode, process::ExitCode,
}; };
mod formatting; use tylc as lib;
mod nodes;
fn main() -> ExitCode { fn main() -> ExitCode {
let input = match env::args().skip(1).rev().reduce(|x, a| a + &x) { let input = match env::args().skip(1).rev().reduce(|x, a| a + &x) {
@ -17,7 +16,7 @@ fn main() -> ExitCode {
} }
}; };
match formatting::pretty_eval(input) { match lib::formatting::pretty_eval(input, 10) {
Ok(s) => println!("{s}"), Ok(s) => println!("{s}"),
Err(e) => { Err(e) => {

View file

@ -1,9 +1,9 @@
use core::f64; use core::f64;
use std::str::FromStr; use std::str::FromStr;
use rug::{Float, float::ParseFloatError, ops::CompleteRound}; use rug::{Float, float::ParseFloatError};
use crate::nodes::{Function, Node, Nodes, Operator, PREC}; use crate::nodes::{Function, Node, Nodes, Operator};
impl Node { impl Node {
fn from_name(s: &str) -> Result<Self, String> { fn from_name(s: &str) -> Result<Self, String> {
@ -124,15 +124,10 @@ impl FromStr for Nodes {
&& !(c == 'e' && filtered_string.chars().nth(i + 1).is_some_and(|c| c == '-')) && !(c == 'e' && filtered_string.chars().nth(i + 1).is_some_and(|c| c == '-'))
&& !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e')) && !(c == '-' && filtered_string.chars().nth(i - 1).is_some_and(|c| c == 'e'))
{ {
nodes.push(Node::Number( nodes.push(Node::from(
Float::parse(&filtered_string[start_index..i]) Float::parse(&filtered_string[start_index..i]).map_err(|e| {
.map_err(|e| { Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
Error::NumberParsing( })?,
filtered_string[start_index..i].to_owned(),
e,
)
})?
.complete(PREC),
)); ));
state = ParsingState::Start; state = ParsingState::Start;
@ -182,12 +177,10 @@ impl FromStr for Nodes {
if matches!(nodes.last(), Some(Node::ParenthesisEnd)) { if matches!(nodes.last(), Some(Node::ParenthesisEnd)) {
nodes.push(Node::Operator(Default::default())); nodes.push(Node::Operator(Default::default()));
} }
nodes.push(Node::Number( nodes.push(Node::from(
Float::parse(&filtered_string[start_index..i]) Float::parse(&filtered_string[start_index..i]).map_err(|e| {
.map_err(|e| {
Error::NumberParsing(filtered_string[start_index..i].to_owned(), e) Error::NumberParsing(filtered_string[start_index..i].to_owned(), e)
})? })?,
.complete(PREC),
)); ));
} }
} }