From a6bcbd5f6669d525ccefbee84d0607ed41d096af Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 13 Jun 2023 09:13:24 -0700 Subject: [PATCH] Added no_space parameter for units --- buildscript/main.rs | 29 +++++++++++++++++++++++++---- src/parser/token/operator.rs | 16 ++++++++++++---- src/quantity/quantity.rs | 12 ++++++++++-- src/quantity/unit/unit.rs | 6 ++++++ 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/buildscript/main.rs b/buildscript/main.rs index 7278490..579cb84 100644 --- a/buildscript/main.rs +++ b/buildscript/main.rs @@ -31,6 +31,7 @@ fn write_wholeunit_main(mut file: &File, units: &Vec) { writeln!(file, "}}\n").unwrap(); + // ToString writeln!(file, concat!( "impl ToString for WholeUnit {{\n", @@ -47,7 +48,30 @@ fn write_wholeunit_main(mut file: &File, units: &Vec) { ).unwrap(); } - writeln!(file, "\t\t}})\n\t}}\n}}").unwrap(); + writeln!(file, "\t\t}})\n\t}}\n}}\n").unwrap(); + + + // Properties + writeln!(file, + concat!( + "impl WholeUnit {{\n", + "\tfn no_space(&self) -> bool {{\n", + "\t\tmatch self {{" + ) + ).unwrap(); + + for u in units { + if u.as_table().unwrap().contains_key("no_space") { + if u.as_table().unwrap()["no_space"].as_bool().unwrap() { + writeln!(file, + "\t\t\tWholeUnit::{} => true,", + u["enum_name"].as_str().unwrap() + ).unwrap(); + } + } + } + + writeln!(file, "\t\t\t_ => false\n\t\t}}\n\t}}\n}}").unwrap(); } @@ -223,9 +247,6 @@ fn write_freeunit_from_string(mut file: &File, units: &Vec) { - - - fn main() -> Result<(), ()>{ let out_dir = env::var_os("OUT_DIR").unwrap(); diff --git a/src/parser/token/operator.rs b/src/parser/token/operator.rs index bfbcbe3..e55acd0 100644 --- a/src/parser/token/operator.rs +++ b/src/parser/token/operator.rs @@ -318,10 +318,18 @@ impl Operator { }; if no_times { - return format!("{} {}", - self.add_parens_to_arg_strict(a), - self.add_parens_to_arg_strict(b) - ); + let Token::Quantity(u) = b else {panic!()}; + if u.unit.no_space() { + return format!("{}{}", + self.add_parens_to_arg_strict(a), + self.add_parens_to_arg_strict(b) + ); + } else { + return format!("{} {}", + self.add_parens_to_arg_strict(a), + self.add_parens_to_arg_strict(b) + ); + } } else { return format!("{} × {}", self.add_parens_to_arg_strict(a), diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index 147ec68..b31dc3f 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -29,7 +29,11 @@ impl ToString for Quantity { let u = self.unit.to_string(); if self.is_one() { return u; }; - return format!("{n} {u}"); + if self.unit.no_space() { + return format!("{n}{u}"); + } else { + return format!("{n} {u}"); + } } } @@ -39,7 +43,11 @@ impl Quantity { if self.unitless() { return n; } let u = self.unit.to_string(); - return format!("{n} {u}"); + if self.unit.no_space() { + return format!("{n}{u}"); + } else { + return format!("{n} {u}"); + } } pub fn new_float(f: f64) -> Option { diff --git a/src/quantity/unit/unit.rs b/src/quantity/unit/unit.rs index 3a0e7e0..0bc77b9 100644 --- a/src/quantity/unit/unit.rs +++ b/src/quantity/unit/unit.rs @@ -86,6 +86,12 @@ impl Unit { pub fn get_val_mut(&mut self) -> &mut HashMap { &mut self.val } pub fn unitless(&self) -> bool { self.get_val().len() == 0 } + pub fn no_space(&self) -> bool { + if self.get_val().len() == 1 { + return self.get_val().keys().next().unwrap().whole.no_space(); + } else { return false; } + } + pub fn from_array(a: &[(FreeUnit, Scalar)]) -> Unit { let mut n = Unit::new(); for (u, p) in a.iter() {