39 Commits

Author SHA1 Message Date
315be575ee Fixed small function 2023-09-20 11:09:44 -07:00
31c368f7d8 Removed dependencies for astrofloat 2023-09-20 11:09:28 -07:00
8a026fc2ea Updated scalar print routine 2023-09-20 11:07:47 -07:00
8497c125ef Minor cleanup 2023-09-20 11:06:37 -07:00
682205f5e1 Fixed function evaluator 2023-09-04 13:49:20 -07:00
e1ba2a9c1f Whitespace 2023-09-03 16:03:34 -07:00
b9cfe719a6 Cleaned up to_string functions 2023-09-03 16:03:24 -07:00
47abd9d18e Migrated floatbase to bigdecimal 2023-09-03 15:57:46 -07:00
4353548900 Fixed float printing 2023-09-03 15:56:33 -07:00
84ebf89d6f Updated TODO 2023-09-03 14:36:20 -07:00
2391606ae1 Fully removed RUG dependency 2023-09-03 14:36:00 -07:00
38c982bb00 Updated TODO 2023-09-03 13:12:37 -07:00
80b1c76c59 Added shell.nix 2023-09-03 13:12:22 -07:00
11b5cd877a Replaced RUG rational with num rational 2023-08-27 22:28:17 -07:00
3a08cfb2d3 Updated TODO 2023-08-21 13:26:53 -07:00
b136353d36 Comments 2023-08-21 13:24:49 -07:00
a125e867c4 Help text typo 2023-08-21 13:24:28 -07:00
c477302c88 Fixed ans evaluation 2023-08-21 13:17:58 -07:00
edc859dc01 Updated TODO 2023-08-20 17:24:39 -07:00
77c357c2f3 All style definitions in one place 2023-08-20 16:49:35 -07:00
3ae5383eed Minor cleanup 2023-08-20 16:49:05 -07:00
4055c08217 Fixed incompatible messages when one unit is a scalar 2023-08-20 16:33:34 -07:00
6969d17cce Added substitution config switch 2023-08-20 16:19:57 -07:00
6e3609d85e Updated TODO 2023-08-17 17:33:54 -07:00
f9ec4d82fe Added a test 2023-08-17 17:31:43 -07:00
86b5356d4f Updated TODO 2023-08-17 16:38:17 -07:00
cc81c3979c Fixed mod bug 2023-08-17 16:38:02 -07:00
b846a7c144 Fixed help art 2023-08-17 12:49:29 -07:00
09996801d8 Updated TODO 2023-08-17 10:32:05 -07:00
da1507e1fc Version bump 2023-08-17 10:11:28 -07:00
7c54c1eb91 Updated TODO 2023-08-17 10:10:43 -07:00
e0ca8be79f Added terminal color detection
Added configuration
Cleaned up context args
2023-08-17 10:10:38 -07:00
fc4c28543f Updated README 2023-08-16 22:44:54 -07:00
36540f37b8 Merge branch 'master' of ssh://git.betalupi.com:33/Mark/daisy 2023-08-16 22:33:40 -07:00
d31fd0b08c Cleaned up context argument 2023-08-16 22:33:38 -07:00
9ea1990f3c Update README.md 2023-08-05 15:11:32 -07:00
8d39b290fa Update README.md 2023-08-05 14:01:04 -07:00
22935957e2 Updated README 2023-08-05 10:14:20 -07:00
2e39bbd524 Updated TODO 2023-08-05 10:10:40 -07:00
29 changed files with 1018 additions and 670 deletions

168
Cargo.lock generated
View File

@ -8,12 +8,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "az"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973"
[[package]]
name = "bitflags"
version = "1.3.2"
@ -28,24 +22,14 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "daisycalc"
version = "1.0.0"
version = "1.0.1"
dependencies = [
"cfg-if",
"rug",
"num",
"termion",
"toml",
]
[[package]]
name = "gmp-mpfr-sys"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b560063e2ffa8ce9c2ef9bf487f2944a97deca5b8de0b5bcd0ae6437ef8b75f"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
@ -64,9 +48,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.140"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "memchr"
@ -74,6 +58,82 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
dependencies = [
"autocfg",
]
[[package]]
name = "numtoa"
version = "0.1.0"
@ -98,17 +158,6 @@ dependencies = [
"redox_syscall",
]
[[package]]
name = "rug"
version = "1.19.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "555e8b44763d034526db899c88cd56ccc4486cd38b444c8aa0e79d4e70ae5a34"
dependencies = [
"az",
"gmp-mpfr-sys",
"libc",
]
[[package]]
name = "serde"
version = "1.0.164"
@ -170,63 +219,6 @@ dependencies = [
"winnow",
]
[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "winnow"
version = "0.4.6"

View File

@ -1,6 +1,6 @@
[package]
name = "daisycalc"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
build = "buildscript/main.rs"
license = "GPL-3.0-only"
@ -28,7 +28,8 @@ cfg-if = "1.0.0"
[target.'cfg(target_family = "unix")'.dependencies]
termion = "2.0.1"
rug = "1.19.2"
num = "0.4.1"
#astro-float = "0.7.1"
[build-dependencies]
toml = "0.7.4"

View File

@ -5,6 +5,7 @@ A high-precision scientific calculator with support for units, derivatives, and
Many features are missing, this is still under development.
# 📦 Installation
- **Cargo:** `cargo install daisycalc`
- **Arch:** `yay -S daisy`
- **Debian:** coming soon
@ -14,7 +15,7 @@ Binary will be in `target/release/daisy`
# 📹 Screenshot
![](https://betalupi.com/static/git/daisy.png)
![Screenshot](https://github.com/rm-dr/daisy/assets/96270320/cc71887a-0fde-46b2-a13b-96b05098b158)
# 🛠️ Features
- Open-source

33
TODO.md
View File

@ -1,50 +1,40 @@
## Version Bump checklist
- TODO: build and publish script
- update Cargo.toml
- run cargo test
- commit
- git tag -a v1.0.0 -m "Version 1.0.0"
- push
- merge
- git tag -a v1.0.0 -m "Version 1.0.0" on merge commit
- cargo publish
- Update packages
## Pre-release
- Fix linelocation (consistent, what does an operator's linelocation mean?)
- Tuple operations
- we don't need vectors as arguments to operators
- Assignment tests
- Fix linelocation when evaluating functions
## Parser
- Better error when `sin = 2`
- Should functions be operators?
- Binary, hex, octal numbers
## General
- Better tests (assignment, many expressions in one context)
- Optional config file
- Optional history file
- daisyrc file
- Compile to WASM, publish a webapp
- Options:
- disable replacement
- disable special characters
- 1/ as -1 power toggle
- powers as superscripts toggle
- evaluate straight from command line
- Auto-push to crates.io
- Package for debian
- Package for debian, nix
## Internals
- Non-recursive treeify
- Faster factorial function. Maybe use gamma instead?
- Arbitrary precision float (rug doesn't offer arbitrary exponents)
- Remove rug dependency (too big, incompatible)
## Math Features
- Dice
- Mean, Median, Min
- Arbitrary base logarithm
- Derivatives
@ -52,21 +42,16 @@
- Complex numbers
- acot/acoth functions
- Sums and products with functional arguments
- Add functions: gcd, inverse mod, dice
## Prompt
- Fix terminal color detection
- Live syntax/output (like firefox js terminal)
- Syntax highlight input and output
- Syntax highlighting
- fish-style tab completion
- Numbered expressions, history recall
- Color configuration
- Enable/disable unit sets (defaults?)
- Consistent unit ordering
- Better linelocation
- we shouldn't need to re-print user input on evaluation errors, red arrows should adjust themselves to the prettyprinted string
- Backend-independent colorful printing
- Better colors in error texts
- Better substitution. Consistent: when ascii, when unicode?
- Command to list substitutions
## Units
- long prefixes (megatonne, etc)

11
shell.nix Normal file
View File

@ -0,0 +1,11 @@
let
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/f155f0cf4ea43c4e3c8918d2d327d44777b6cad4.tar.gz") {};
in pkgs.mkShell {
buildInputs = with pkgs; [
cargo
rustc
rustfmt
m4
];
}

View File

@ -44,8 +44,8 @@ fn greeter() -> FormattedText {
#[inline(always)]
pub fn do_command(
context: &mut Context,
s: &String,
context: &mut Context
) -> FormattedText {
let args: Vec<&str> = s.split(" ").collect();
let first = args[0];
@ -170,7 +170,7 @@ pub fn do_command(
t.push(&format!(
" {key}{padding} = [c]{v}[n]\n",
v = value.to_string(),
v = value.display(context),
));
}
}
@ -184,7 +184,7 @@ pub fn do_command(
t.push(&format!(
" {s}{padding} = [c]{v}[n]\n",
v = exp.to_string(),
v = exp.display(context),
));
}
}
@ -224,13 +224,13 @@ pub fn do_command(
if args.len() != 2 {
return FormattedText::new(
format!(
"[c]{first}[n] [t]takes exactly two arguments.[n]\n\n",
"[c]{first}[n] [t]takes exactly one argument.[n]\n\n",
)
);
}
let v = args[1].to_string();
let v = substitute(&v, context);
let v = substitute(context, &v);
let r = context.delete(&v);
return match r {

View File

@ -3,7 +3,68 @@ use crate::quantity::freeunit_from_string;
use std::collections::HashMap;
#[derive(Debug)]
#[derive(Clone)]
pub struct Config {
// How to color terminal text.
// 0: No colors
// 1: ANSI-compatible, 8 colors
// 2: Full 256 color and special styles
pub term_color_type: u8,
// Should we accept input and print in unicode?
//pub enable_unicode: bool,
// Should we replace certain strings (like "pi")
// with prettier unicode alternatives?
//
// Automatically disabled if enable_unicode is off.
pub enable_substituion: bool,
// Should we print simple powers
// as unicode superscript chars?
//
// Automatically disables if enable_unicode is off.
pub enable_super_powers: bool,
// Should we write "one-over" fractions
// as -1 powers?
//
// Automatically disabled if enable_super_powers is off.
pub enable_one_over_power: bool,
}
impl Config {
pub fn new() -> Config {
Config{
term_color_type: 2,
enable_substituion: true,
//enable_unicode: true,
enable_super_powers: true,
enable_one_over_power: true
}
}
pub fn check(&mut self) {
//if !self.enable_unicode {
// self.enable_substituion = false;
// self.enable_super_powers = false;
//}
if !self.enable_super_powers {
self.enable_one_over_power = false
}
}
}
#[derive(Debug)]
//#[derive(Clone)]
pub struct Context {
pub config: Config,
history: Vec<Expression>,
variables: HashMap<String, Expression>,
functions: HashMap<String, (Vec<String>, Expression)>,
@ -15,11 +76,12 @@ pub struct Context {
// General functions
impl Context {
pub fn new() -> Context {
Context{
Context {
config: Config::new(),
history: Vec::new(),
variables: HashMap::new(),
functions: HashMap::new(),
shadow: HashMap::new(),
shadow: HashMap::new()
}
}
@ -64,6 +126,7 @@ impl Context {
} else { panic!() }
}
// Can we define a new variable with this name?
pub fn valid_varible(&self, s: &str) -> bool {
if {
Function::from_string(s).is_some() ||
@ -83,11 +146,18 @@ impl Context {
}
}
// Can we get a value fro mthis variable name?
pub fn is_varible(&self, s: &str) -> bool {
return self.valid_varible(s) && (
self.variables.contains_key(s) ||
self.shadow.contains_key(s)
);
return {
(
s == "ans" &&
self.history.len() != 0
) ||
(
self.valid_varible(s) &&
(self.variables.contains_key(s) || self.shadow.contains_key(s))
)
};
}
pub fn get_variables(&self) -> &HashMap<String, Expression> {

View File

@ -1,11 +1,12 @@
use std::collections::VecDeque;
use std::io::Write;
use termion::raw::RawTerminal;
use termion::color;
use termion::style;
use crate::formattedtext;
use crate::parser::substitute_cursor;
use crate::context::Context;
const PROMPT_STR: &str = "==> ";
#[derive(Debug)]
pub struct PromptBuffer {
// History
@ -37,70 +38,43 @@ impl PromptBuffer {
}
// Same as write_primpt, but pretends there is no cursor
pub fn write_prompt_nocursor(&mut self, stdout: &mut RawTerminal<std::io::Stdout>, context: &Context) -> Result<(), std::io::Error> {
// Draw prettyprinted expression
let (_, s) = substitute_cursor(&self.get_contents(), self.buffer.chars().count(), context);
write!(
stdout, "\r{}{}==>{}{} {}",
style::Bold,
color::Fg(color::Blue),
color::Fg(color::Reset),
style::Reset,
s
)?;
// If this string is shorter, clear the remaining old one.
if s.chars().count() < self.last_print_len {
write!(
stdout, "{}{}",
" ".repeat(self.last_print_len - s.chars().count()),
termion::cursor::Left((self.last_print_len - s.chars().count()) as u16)
)?;
pub fn write_prompt_nocursor(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
let tmp = self.cursor;
self.cursor = 0;
let r = self.write_prompt(context, stdout);
self.cursor = tmp;
return r;
}
self.last_print_len = s.chars().count();
stdout.flush()?;
return Ok(());
}
pub fn write_prompt(&mut self, stdout: &mut RawTerminal<std::io::Stdout>, context: &Context) -> Result<(), std::io::Error> {
pub fn write_prompt(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
let l = self.buffer.chars().count();
let i = if l == 0 {0} else {l - self.cursor};
// Draw prettyprinted expression
let (display_cursor, s) = substitute_cursor(&self.get_contents(), i, context);
let (display_c, s) = substitute_cursor(context, &self.get_contents(), i);
write!(
stdout, "\r{}{}==>{}{} {}",
style::Bold,
color::Fg(color::Blue),
color::Fg(color::Reset),
style::Reset,
stdout, "\r{}{PROMPT_STR}{}{}",
formattedtext::format_map('p', context).unwrap(),
formattedtext::format_map('n', context).unwrap(),
s
)?;
// If this string is shorter, clear the remaining old one.
if s.chars().count() < self.last_print_len {
write!(
stdout, "{}{}",
" ".repeat(self.last_print_len - s.chars().count()),
termion::cursor::Left((self.last_print_len - s.chars().count()) as u16)
)?;
}
// Move cursor to correct position
if display_cursor != 0 {
write!(
stdout, "{}",
termion::cursor::Left(display_cursor as u16)
" ".repeat(self.last_print_len - s.chars().count()),
)?;
stdout.flush()?;
}
self.last_print_len = s.chars().count();
write!(
stdout, "\r{}",
termion::cursor::Right((display_c + PROMPT_STR.chars().count()) as u16)
)?;
stdout.flush()?;
self.last_print_len = s.chars().count();
return Ok(());
}

View File

@ -1,4 +1,3 @@
use std::io::Write;
use std::io::stdout;
use std::io::stdin;
use std::env;
@ -6,35 +5,63 @@ use std::env;
use termion::{
event::Key,
input::TermRead,
raw::IntoRawMode
raw::IntoRawMode,
color::DetectColors
};
use super::promptbuffer::PromptBuffer;
use crate::command;
use crate::context::Context;
use crate::FormattedText;
#[inline(always)]
pub fn main() -> Result<(), std::io::Error> {
let mut stdout = stdout().into_raw_mode().unwrap();
let mut pb: PromptBuffer = PromptBuffer::new(64);
let mut context: Context = Context::new();
let mut context = Context::new();
// Set color compatibilty
let term_colors = stdout.available_colors().unwrap_or(0);
if term_colors >= 256 {
context.config.term_color_type = 2;
} else if term_colors >= 8 {
context.config.term_color_type = 1;
} else {
context.config.term_color_type = 0;
}
context.config.check();
// Handle command-line arguments
let args: Vec<String> = env::args().collect();
if args.iter().any(|s| s == "--help") {
let t = command::do_command(&String::from("help"), &mut context);
t.write(&mut stdout)?;
let t = command::do_command(&mut context, &String::from("help"));
t.write(&context, &mut stdout)?;
return Ok(());
} else if args.iter().any(|s| s == "--version") {
write!(stdout, "Daisy v{}\r\n", env!("CARGO_PKG_VERSION"))?;
let t = FormattedText::new(format!(
"Daisy v{}\n", env!("CARGO_PKG_VERSION")
));
t.write(&context, &mut stdout)?;
return Ok(());
} else if args.iter().any(|s| s == "--debug") {
let t = FormattedText::new(format!(
concat!(
"Daisy v{}\n",
"Your terminal supports {} colors.\n"
),
env!("CARGO_PKG_VERSION"),
term_colors
));
t.write(&context, &mut stdout)?;
return Ok(());
}
'outer: loop {
pb.write_prompt(&mut stdout, &context)?;
pb.write_prompt(&mut context, &mut stdout)?;
let stdin = stdin();
for c in stdin.keys() {
@ -43,19 +70,19 @@ pub fn main() -> Result<(), std::io::Error> {
'\n' => {
// Print again without cursor, in case we pressed enter
// while inside a substitution
pb.write_prompt_nocursor(&mut stdout, &context)?;
pb.write_prompt_nocursor(&mut context, &mut stdout)?;
let in_str = pb.enter();
write!(stdout, "\r\n")?;
FormattedText::newline(&mut stdout)?;
if in_str == "" { break; }
if in_str.trim() == "quit" {
break 'outer;
} else {
let r = crate::do_string(&in_str, &mut context);
let r = crate::do_string(&mut context, &in_str);
match r {
Ok(t) | Err(t) => {
t.write(&mut stdout).unwrap();
t.write(&context, &mut stdout).unwrap();
}
}
}
@ -79,10 +106,10 @@ pub fn main() -> Result<(), std::io::Error> {
};
};
pb.write_prompt(&mut stdout, &context)?;
pb.write_prompt(&mut context, &mut stdout)?;
}
}
write!(stdout, "\r\n")?;
FormattedText::newline(&mut stdout)?;
return Ok(());
}

View File

@ -9,8 +9,8 @@ use super::function::eval_function;
pub fn evaluate(
t: &Expression,
context: &mut Context
context: &mut Context,
t: &Expression
) -> Result<
Expression,
(LineLocation, DaisyError)
@ -51,7 +51,7 @@ pub fn evaluate(
let new = match g {
Expression::Quantity(_, _) => None,
Expression::Tuple(_, _) => None,
Expression::Constant(_, c) => { Some(evaluate(&c.value(), context).unwrap()) },
Expression::Constant(_, c) => { Some(evaluate(context, &c.value()).unwrap()) },
Expression::Variable(l, s) => {
// Don't move up, re-evaluate
// This makes variables containing floating variables work properly
@ -63,8 +63,8 @@ pub fn evaluate(
context.get_variable(&s)
},
Expression::Operator(_, Operator::Function(_), _) => { Some(eval_function(g)?) },
Expression::Operator(_, _, _) => { eval_operator(g, context)? },
Expression::Operator(_, Operator::Function(_), _) => { eval_function(g)? },
Expression::Operator(_, _, _) => { eval_operator(context, g)? },
};
if let Some(mut new) = new {

View File

@ -26,7 +26,7 @@ fn to_radians(q: Quantity) -> Result<Quantity, ()> {
pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyError)> {
pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
let Expression::Operator(loc, Operator::Function(f), args) = g else {unreachable!()};
@ -41,113 +41,112 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
))
};
let Expression::Quantity(l, q) = a else {panic!()};
let Expression::Quantity(l, q) = a else { return Ok(None); };
match f {
Function::NoUnit => { return Ok(Expression::Quantity(*loc + *l, q.without_unit())); }
Function::ToBase => { return Ok(Expression::Quantity(*loc + *l, q.convert_to_base())); }
Function::NoUnit => { return Ok(Some(Expression::Quantity(*loc + *l, q.without_unit()))); }
Function::ToBase => { return Ok(Some(Expression::Quantity(*loc + *l, q.convert_to_base()))); }
Function::Abs => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.abs()));
return Ok(Some(Expression::Quantity(*loc + *l, q.abs())));
},
Function::Floor => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.floor()));
return Ok(Some(Expression::Quantity(*loc + *l, q.floor())));
},
Function::Ceil => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.ceil()));
return Ok(Some(Expression::Quantity(*loc + *l, q.ceil())));
},
Function::Round => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.round()));
return Ok(Some(Expression::Quantity(*loc + *l, q.round())));
},
Function::NaturalLog => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.ln()));
return Ok(Some(Expression::Quantity(*loc + *l, q.ln())));
},
Function::TenLog => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.log10()));
return Ok(Some(Expression::Quantity(*loc + *l, q.log10())));
},
Function::Sin => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sin()));
return Ok(Some(Expression::Quantity(*loc + *l, q.sin())));
},
Function::Cos => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.cos()));
return Ok(Some(Expression::Quantity(*loc + *l, q.cos())));
},
Function::Tan => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.tan()));
return Ok(Some(Expression::Quantity(*loc + *l, q.tan())));
},
Function::Csc => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.csc()));
return Ok(Some(Expression::Quantity(*loc + *l, q.csc())));
},
Function::Sec => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sec()));
return Ok(Some(Expression::Quantity(*loc + *l, q.sec())));
},
Function::Cot => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.cot()));
return Ok(Some(Expression::Quantity(*loc + *l, q.cot())));
},
Function::Sinh => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sinh()));
return Ok(Some(Expression::Quantity(*loc + *l, q.sinh())));
},
Function::Cosh => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.cosh()));
return Ok(Some(Expression::Quantity(*loc + *l, q.cosh())));
},
Function::Tanh => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.tanh()));
return Ok(Some(Expression::Quantity(*loc + *l, q.tanh())));
},
Function::Csch => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.csch()));
return Ok(Some(Expression::Quantity(*loc + *l, q.csch())));
},
Function::Sech => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sech()));
return Ok(Some(Expression::Quantity(*loc + *l, q.sech())));
},
Function::Coth => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.coth()));
return Ok(Some(Expression::Quantity(*loc + *l, q.coth())));
},
Function::Asin => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.asin()));
return Ok(Some(Expression::Quantity(*loc + *l, q.asin())));
},
Function::Acos => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.acos()));
return Ok(Some(Expression::Quantity(*loc + *l, q.acos())));
},
Function::Atan => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.atan()));
return Ok(Some(Expression::Quantity(*loc + *l, q.atan())));
},
Function::Asinh => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.asinh()));
return Ok(Some(Expression::Quantity(*loc + *l, q.asinh())));
},
Function::Acosh => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.acosh()));
return Ok(Some(Expression::Quantity(*loc + *l, q.acosh())));
},
Function::Atanh => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.atanh()));
return Ok(Some(Expression::Quantity(*loc + *l, q.atanh())));
},
@ -160,7 +159,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
let mut r = q.without_unit();
r += Quantity::new_rational(-273.15f64).unwrap();
return Ok(Expression::Quantity(*loc + *l, r));
return Ok(Some(Expression::Quantity(*loc + *l, r)));
},
Function::ToFahrenheit => {
let mut k = Quantity::new_rational(1f64).unwrap();
@ -172,7 +171,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
r += Quantity::new_rational(-459.67).unwrap();
return Ok(Expression::Quantity(*loc + *l, r));
return Ok(Some(Expression::Quantity(*loc + *l, r)));
},
Function::FromCelsius => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
@ -181,7 +180,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
r += q.clone();
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
return Ok(Expression::Quantity(*loc + *l, r));
return Ok(Some(Expression::Quantity(*loc + *l, r)));
},
Function::FromFahrenheit => {
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
@ -191,7 +190,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyE
r *= Quantity::new_rational_from_frac(5i64, 9i64).unwrap();
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
return Ok(Expression::Quantity(*loc + *l, r));
return Ok(Some(Expression::Quantity(*loc + *l, r)));
}
}
}

View File

@ -7,7 +7,7 @@ use crate::errors::DaisyError;
use super::evaluate;
pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
let Expression::Operator(op_loc, op, args) = g else {panic!()};
@ -52,7 +52,7 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
}
let r = evaluate(&exp, context)?;
let r = evaluate(context, &exp)?;
context.clear_shadow();
return Ok(Some(r));
@ -75,12 +75,26 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
if !a.unit.compatible_with(&b.unit) {
let a = a.convert_to_base().unit;
let b = b.convert_to_base().unit;
let a_s: String;
let b_s: String;
if a.unitless() {
a_s = String::from("scalar");
} else {
a_s = a.display(context);
}
if b.unitless() {
b_s = String::from("scalar");
} else {
b_s = b.display(context);
}
return Err((
*la + *lb + *op_loc,
DaisyError::IncompatibleUnits(
a.convert_to_base().unit.to_string(),
b.convert_to_base().unit.to_string()
)
DaisyError::IncompatibleUnits(a_s, b_s)
));
}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone())));
@ -98,12 +112,24 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
if !a.unit.compatible_with(&b.unit) {
let a_s: String;
let b_s: String;
if a.unitless() {
a_s = String::from("scalar");
} else {
a_s = a.display(context);
}
if b.unitless() {
b_s = String::from("scalar");
} else {
b_s = b.display(context);
}
return Err((
*la + *lb + *op_loc,
DaisyError::IncompatibleUnits(
a.convert_to_base().unit.to_string(),
b.convert_to_base().unit.to_string()
)
DaisyError::IncompatibleUnits(a_s, b_s)
));
}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() - b.clone())));
@ -138,7 +164,8 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() * b.clone())));
let o = a.clone() * b.clone();
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, o)));
}
}
@ -162,6 +189,11 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, DaisyError::BadMath)); }
if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, DaisyError::BadMath)); }
let o = va.clone() % vb.clone();
if o.is_nan() {return Err((*la + *lb + *op_loc, DaisyError::BadMath));}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, va.clone() % vb.clone())));
} else { return Ok(None); }
} else { return Ok(None); }
@ -176,12 +208,26 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
if let Expression::Quantity(lb, vb) = b {
let n = va.clone().convert_to(vb.clone());
if n.is_none() {
let va = va.convert_to_base().unit;
let vb = vb.convert_to_base().unit;
let a_s: String;
let b_s: String;
if va.unitless() {
a_s = String::from("scalar");
} else {
a_s = a.display(context);
}
if vb.unitless() {
b_s = String::from("scalar");
} else {
b_s = b.display(context);
}
return Err((
*la + *lb + *op_loc,
DaisyError::IncompatibleUnits(
va.convert_to_base().unit.to_string(),
vb.convert_to_base().unit.to_string()
)
DaisyError::IncompatibleUnits(a_s, b_s)
));
}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, n.unwrap())));

View File

@ -5,6 +5,7 @@ use termion::style;
use termion::clear;
use termion::cursor;
use std::ops::Add;
use crate::context::Context;
#[derive(Debug)]
@ -18,8 +19,108 @@ impl ToString for FormattedText {
}
impl FormattedText {
fn format_map_none(c: char) -> Option<String> {
Some(match c {
'n'|'i'|'t'|'a'|
'e'|'c'|'s'|'r'|
'p'
=> { "".to_string() },
_ => { return None }
})
}
fn format_map_ansi(c: char) -> Option<String> {
Some(match c {
'n' => { // Normal text
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
},
'i' => { // Normal italic text
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
},
't' => { // Title text (should be cyan)
format!("{}{}", color::Fg(color::AnsiValue(6)), color::Bg(color::Reset))
},
'a' => { // Colored text (should be pink)
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
},
'e' => { // Error titles (should be red)
format!("{}{}", color::Fg(color::AnsiValue(1)), color::Bg(color::Reset))
},
'c' => { // Console text (inverted black on white)
format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7)))
},
'p' => { // Input prompt (how ==> is styled) (should be blue)
format!("{}{}", color::Fg(color::AnsiValue(4)), color::Bg(color::Reset))
},
's' => { // Repeat prompt (how => is styled) (should be pink)
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
},
'r' => { // Result prompt (how = is styled) (should be green)
format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset))
},
_ => { return None }
})
}
// style::reset also resets color.
// Make sure color comes AFTER style reset.
fn format_map_full(c: char) -> Option<String> {
Some(match c {
'n' => { // Normal text
format!("{}{}", style::Reset, color::Fg(color::Reset))
},
'i' => { // Normal italic text
format!("{}{}", color::Fg(color::Reset), style::Italic)
},
't' => { // Title text
format!("{}{}", color::Fg(color::Magenta), style::Bold)
},
'a' => { // Colored text
format!("{}{}", style::Reset, color::Fg(color::Magenta))
},
'e' => { // Error titles
format!("{}{}", color::Fg(color::Red), style::Bold)
},
'c' => { // Console text
format!("{}{}", color::Fg(color::LightBlack), style::Italic)
},
'p' => { // Input prompt (how ==> is styled)
format!("{}{}", color::Fg(color::Blue), style::Bold)
},
's' => { // Repeat prompt (how => is styled)
format!("{}{}", color::Fg(color::Magenta), style::Bold)
},
'r' => { // Result prompt (how = is styled)
format!("{}{}", color::Fg(color::Green), style::Bold)
},
_ => { return None }
})
}
pub fn format_map(c: char, context: &Context) -> Option<String> {
match context.config.term_color_type {
0 => format_map_none(c),
1 => format_map_ansi(c),
2 => format_map_full(c),
_ => unreachable!("Invalid term_color_type")
}
}
impl FormattedText {
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
write!(stdout, "\n")?;
return Ok(());
}
}
impl FormattedText {
pub fn new(s: String) -> FormattedText {
return FormattedText {
text: s
@ -31,7 +132,7 @@ impl FormattedText {
}
pub fn write(&self, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
pub fn write(&self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
if self.text == "[clear]" {
write!(
@ -58,30 +159,17 @@ impl FormattedText {
let b = chars.next().unwrap();
match (a, b) {
('n', ']') => { // Normal text
s.push_str(&format!("{}{}", color::Fg(color::Reset), style::Reset));
},
('i', ']') => { // Normal italic text
s.push_str(&format!("{}{}", color::Fg(color::Reset), style::Italic));
},
('t', ']') => { // Title text
s.push_str(&format!("{}{}", color::Fg(color::Magenta), style::Bold));
},
('a', ']') => { // Colored text
s.push_str(&format!("{}{}", color::Fg(color::Magenta), style::Reset));
},
('e', ']') => { // Error titles
s.push_str(&format!("{}{}", color::Fg(color::Red), style::Bold));
},
('c', ']') => { // Console text
s.push_str(&format!("{}{}", color::Fg(color::LightBlack), style::Italic));
},
(c, ']') => { // Normal text
('s', ']') => { // Repeat prompt (how => is styled)
s.push_str(&format!("{}{}", color::Fg(color::Magenta), style::Bold));
},
('r', ']') => { // Result prompt (how = is styled)
s.push_str(&format!("{}{}", color::Fg(color::Green), style::Bold));
let q = format_map(c, context);
if q.is_some() {
s.push_str(&q.unwrap());
} else {
s.push('[');
s.push(a);
s.push(b);
}
},
_ => {
@ -96,7 +184,7 @@ impl FormattedText {
}
}
write!(stdout, "{}", s)?;
write!(stdout, "\r{}", s)?;
return Ok(());
}
}

View File

@ -26,21 +26,21 @@ fn main() -> Result<(), std::io::Error> {
#[inline(always)]
pub fn do_string(
s: &String,
mut context: &mut Context
context: &mut Context,
s: &String
) -> Result<FormattedText, FormattedText> {
let r: (LineLocation, DaisyError);
if command::is_command(s) {
return Ok(command::do_command(s, &mut context));
return Ok(command::do_command(context, s));
} else if s.contains("=") {
let x = do_assignment(s, &mut context);
let x = do_assignment(context, s);
match x {
Ok(t) => { return Ok(t) },
Err(t) => { r = t }
};
} else {
let x = do_expression(s, &mut context);
let x = do_expression(context, s);
match x {
Ok((t, e)) => { context.push_hist(e); return Ok(t) },
Err(t) => { r = t }
@ -73,25 +73,25 @@ pub fn do_string(
// Returns a FormattedText with output that should be printed.
#[inline(always)]
fn do_expression(
s: &String,
context: &mut Context
context: &mut Context,
s: &String
) -> Result<(FormattedText, parser::Expression), (LineLocation, DaisyError)> {
let mut output = FormattedText::new("".to_string());
let g = parser::parse(&s, context)?;
let g_evaluated = evaluate::evaluate(&g, context)?;
let g = parser::parse(context, &s)?;
let g_evaluated = evaluate::evaluate(context, &g)?;
// Display parsed string
output.push(&format!(
" [s]=>[n] {}\n\n",
g.to_string()
g.display(context)
));
// Display result
output.push(&format!(
" [r]=[n] {}\n\n",
g_evaluated.to_string_outer(),
g_evaluated.display_outer(context),
));
return Ok((output, g_evaluated));
@ -102,8 +102,8 @@ fn do_expression(
// Returns a FormattedText with output that should be printed.
#[inline(always)]
fn do_assignment(
s: &String,
context: &mut Context
context: &mut Context,
s: &String
) -> Result<FormattedText, (LineLocation, DaisyError)> {
let mut output = FormattedText::new("".to_string());
@ -135,8 +135,8 @@ fn do_assignment(
.unwrap_or_else(|| parts[0].len());
let left = substitute(&parts[0].trim().to_string(), &context);
let right = substitute(&parts[1].trim().to_string(), &context);
let left = substitute(context, &parts[0].trim().to_string());
let right = substitute(context, &parts[1].trim().to_string());
let is_function = left.contains("(");
// The order of methods below is a bit odd.
@ -212,7 +212,7 @@ fn do_assignment(
}
// Parse right hand side
let g = parser::parse(&right, context);
let g = parser::parse(context, &right);
let Ok(g) = g else {
let Err((l, e)) = g else { unreachable!() };
return Err((
@ -224,12 +224,12 @@ fn do_assignment(
// Display parsed string
output.push(&format!(
" [s]=>[n] {left} = {}\n\n",
g.to_string()
g.display(context)
));
// Evaluate expression with shadow variables
for a in &args { context.add_shadow(a.to_string(), None);}
let g_evaluated = evaluate::evaluate(&g, context);
let g_evaluated = evaluate::evaluate(context, &g);
context.clear_shadow();
let Ok(_g_evaluated) = g_evaluated else {
let Err((l, e)) = g_evaluated else { unreachable!() };
@ -254,7 +254,7 @@ fn do_assignment(
}
// Parse right hand side
let g = parser::parse(&right, context);
let g = parser::parse(context, &right);
let Ok(g) = g else {
let Err((l, e)) = g else { unreachable!() };
return Err((
@ -266,11 +266,11 @@ fn do_assignment(
// Display parsed string
output.push(&format!(
" [t]=>[n] {left} = {}\n\n",
g.to_string()
g.display(context)
));
// Evaluate expression
let g_evaluated = evaluate::evaluate(&g, context);
let g_evaluated = evaluate::evaluate(context, &g);
let Ok(g_evaluated) = g_evaluated else {
let Err((l, e)) = g_evaluated else { unreachable!() };
return Err((

View File

@ -1,5 +1,6 @@
use std::collections::VecDeque;
use crate::quantity::Quantity;
use crate::context::Context;
use super::Operator;
use super::Constant;
@ -10,6 +11,23 @@ use super::super::LineLocation;
#[derive(Debug)]
#[derive(Clone)]
pub enum Expression {
// Meaning of `LineLocation`:
//
// For Variables, Constants, Quantities, Tuples:
// If this expression was parsed, LineLocation is what part of the prompt was parsed to get this expression
// If this expression is the result of a calculation, LineLocaion is the sum of the LineLocations of
// all expressions used to make it. In other words, it points to the part of the prompt that was evaluated
// to get this new expression.
//
// For Operators:
// Linelocation points to the operator's position in the prompt.
// If this is a function, it points to the function name.
// If this is `+`, `!`, or etc, it points to that character.
// Operator arguments are NOT included in this linelocation.
//
//
// All the above rules are implemented when parsing and evaluating expressions.
Variable(LineLocation, String),
Quantity(LineLocation, Quantity),
Constant(LineLocation, Constant),
@ -17,17 +35,17 @@ pub enum Expression {
Tuple(LineLocation, VecDeque<Expression>),
}
impl ToString for Expression {
fn to_string(&self) -> String {
impl Expression {
pub fn display(&self, context: &Context) -> String {
match self {
Expression::Quantity(_, v) => v.to_string(),
Expression::Quantity(_, v) => v.display(context),
Expression::Constant(_, c) => c.to_string(),
Expression::Variable(_, s) => s.clone(),
Expression::Operator(_, o,a) => o.print(a),
Expression::Operator(_, o,a) => o.display(context, a),
Expression::Tuple(_, v) => {
format!("({})",
v.iter()
.map(|x| x.to_string())
.map(|x| x.display(context))
.collect::<Vec<String>>()
.join(", ")
)
@ -39,16 +57,16 @@ impl ToString for Expression {
impl Expression {
// This is called only when this is the outermost Expression.
// This sometimes leads to different--usually more verbose--behavior.
pub fn to_string_outer(&self) -> String {
pub fn display_outer(&self, context: &Context) -> String {
match self {
Expression::Quantity(_, v) => v.to_string_outer(),
Expression::Quantity(_, v) => v.display_outer(context),
Expression::Constant(_, c) => c.to_string(),
Expression::Variable(_, s) => s.clone(),
Expression::Operator(_, o,a) => o.print(a),
Expression::Operator(_, o,a) => o.display(context, a),
Expression::Tuple(_, v) => {
format!("({})",
v.iter()
.map(|x| x.to_string())
.map(|x| x.display(context))
.collect::<Vec<String>>()
.join(", ")
)

View File

@ -62,7 +62,7 @@ impl Operator {
}
#[inline(always)]
pub fn from_string(s: &str, context: &Context) -> Option<Operator> {
pub fn from_string(context: &Context, s: &str) -> Option<Operator> {
let f = Function::from_string(s);
if let Some(f) = f {
@ -128,8 +128,8 @@ impl Operator {
}
#[inline(always)]
fn add_parens_to_arg(&self, arg: &Expression) -> String {
let mut astr: String = arg.to_string();
fn add_parens_to_arg(&self, context: &Context, arg: &Expression) -> String {
let mut astr: String = arg.display(context);
if let Expression::Operator(_, o,_) = arg {
if o.print_map() < self.print_map() {
astr = format!("({})", astr);
@ -139,8 +139,8 @@ impl Operator {
}
#[inline(always)]
fn add_parens_to_arg_strict(&self, arg: &Expression) -> String {
let mut astr: String = arg.to_string();
fn add_parens_to_arg_strict(&self, context: &Context, arg: &Expression) -> String {
let mut astr: String = arg.display(context);
if let Expression::Operator(_, o,_) = arg {
if o.print_map() <= self.print_map() {
astr = format!("({})", astr);
@ -150,56 +150,56 @@ impl Operator {
}
pub fn print(&self, args: &VecDeque<Expression>) -> String {
pub fn display(&self, context: &Context, args: &VecDeque<Expression>) -> String {
match self {
Operator::Negative => {
return format!("-{}", self.add_parens_to_arg(&args[0]));
return format!("-{}", self.add_parens_to_arg(context, &args[0]));
},
Operator::Sqrt => {
return format!(
"{}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(context, &args[0]),
);
},
Operator::ModuloLong => {
return format!(
"{} mod {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
self.add_parens_to_arg(context, &args[0]),
self.add_parens_to_arg(context, &args[1])
);
},
Operator::DivideLong => {
return format!(
"{} per {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
self.add_parens_to_arg(context, &args[0]),
self.add_parens_to_arg(context, &args[1])
);
},
Operator::UnitConvert => {
return format!(
"{} to {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
self.add_parens_to_arg(context, &args[0]),
self.add_parens_to_arg(context, &args[1])
);
},
Operator::Modulo => {
return format!(
"{} % {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
self.add_parens_to_arg(context, &args[0]),
self.add_parens_to_arg(context, &args[1])
);
},
Operator::Subtract => {
return format!(
"{} - {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
self.add_parens_to_arg(context, &args[0]),
self.add_parens_to_arg(context, &args[1])
);
},
@ -207,10 +207,14 @@ impl Operator {
let q = &args[1];
if q.is_unitless_integer() && !q.to_string().contains("e") {
if {
context.config.enable_super_powers &&
q.is_unitless_integer() &&
!q.display(context).contains("e")
} {
// Write integer powers as a superscript
let mut b = String::new();
for c in q.to_string().chars() {
for c in q.display(context).chars() {
b.push(match c {
'-' => '⁻',
'0' => '⁰',
@ -229,27 +233,27 @@ impl Operator {
return format!(
"{}{}",
self.add_parens_to_arg_strict(&args[0]),
self.add_parens_to_arg_strict(context, &args[0]),
b
);
} else {
return format!(
"{}^{}",
self.add_parens_to_arg_strict(&args[0]),
self.add_parens_to_arg_strict(&args[1])
self.add_parens_to_arg_strict(context, &args[0]),
self.add_parens_to_arg_strict(context, &args[1])
);
}
},
Operator::Factorial => {
return format!("{}!", self.add_parens_to_arg(&args[0]));
return format!("{}!", self.add_parens_to_arg(context, &args[0]));
},
Operator::Add => {
return format!(
"{} + {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
self.add_parens_to_arg(context, &args[0]),
self.add_parens_to_arg(context, &args[1])
);
},
@ -279,26 +283,26 @@ impl Operator {
if let Expression::Quantity(_, u) = b {
if u.unit.no_space() {
return format!("{}{}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
self.add_parens_to_arg_strict(context, a),
self.add_parens_to_arg_strict(context, b)
);
} else {
return format!("{} {}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
self.add_parens_to_arg_strict(context, a),
self.add_parens_to_arg_strict(context, b)
);
}
} else {
return format!("{}{}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
self.add_parens_to_arg_strict(context, a),
self.add_parens_to_arg_strict(context, b)
);
};
} else {
return format!("{} × {}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
self.add_parens_to_arg_strict(context, a),
self.add_parens_to_arg_strict(context, b)
);
}
},
@ -309,25 +313,25 @@ impl Operator {
if let Expression::Quantity(_, q) = a {
if q.is_one() {
if q.is_one() && context.config.enable_one_over_power {
return format!("{}⁻¹",
self.add_parens_to_arg_strict(b)
self.add_parens_to_arg_strict(context, b)
);
}
}
return format!("{} ÷ {}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
self.add_parens_to_arg_strict(context, a),
self.add_parens_to_arg_strict(context, b)
);
},
Operator::Function(s) => {
return format!("{}({})", s.to_string(), args[0].to_string());
return format!("{}({})", s.to_string(), args[0].display(context));
},
Operator::UserFunction(s) => {
return format!("{}({})", s.to_string(), args[0].to_string());
return format!("{}({})", s, args[0].display(context));
}
};
}

View File

@ -18,59 +18,79 @@ use crate::context::Context;
use crate::errors::DaisyError;
pub fn parse(
s: &String, context: &Context
context: &Context, s: &String
) -> Result<Expression, (LineLocation, DaisyError)> {
let expressions = stage::tokenize(s, context);
let (_, expressions) = stage::find_subs(expressions);
let g = stage::groupify(expressions, context)?;
let g = stage::treeify(g, context)?;
let mut expressions = stage::tokenize(context, s);
if context.config.enable_substituion {
(_, expressions) = stage::find_subs(expressions);
}
let g = stage::groupify(context, expressions)?;
let g = stage::treeify(context, g)?;
return Ok(g);
}
pub fn parse_no_context(s: &String) -> Result<Expression, (LineLocation, DaisyError)> {
parse(s, &Context::new())
parse(&Context::new(), s)
}
pub fn substitute(s: &String, context: &Context) -> String {
let (_, s) = substitute_cursor(s, s.chars().count(), context);
// Substitiution replaces certain string with pretty unicode characters.
// When it is enabled, ALL input strings are substituted. Variable and
// operator tokens use the replaced string value. Make sure both the
// original and the replaced strings are handled correctly by the parser.
pub fn substitute(context: &Context, s: &String) -> String {
if !context.config.enable_substituion { return s.clone(); }
let (_, s) = substitute_cursor(context, s, s.chars().count());
return s;
}
pub fn substitute_cursor(
context: &Context,
s: &String, // The string to substitute
c: usize, // Location of the cursor right now
context: &Context
c: usize // Location of the cursor right now
) -> (
usize, // Location of cursor in substituted string
usize, // New cursor
String // String with substitutions
) {
if !context.config.enable_substituion { return (c, s.clone()); }
if s == "" { return (c, s.clone()) }
let mut new_s = s.clone();
let l = s.chars().count();
let expressions = stage::tokenize(s, context);
let expressions = stage::tokenize(context, s);
let (mut subs, _) = stage::find_subs(expressions);
let mut new_c = l - c;
let mut new_c = c.clone();
while subs.len() > 0 {
let r = subs.pop_back().unwrap();
// Apply substitutions in reverse order
// r is the current substitution: (linelocation, string)
let r = subs.pop_back().unwrap();
if { // Don't substitute if our cursor is inside the substitution
c >= r.0.pos &&
c < r.0.pos+r.0.len
} { continue; }
if c < r.0.pos {
let ct = r.1.chars().count();
if ct >= r.0.len {
if new_c >= ct - r.0.len {
new_c += ct - r.0.len
}
} else {
new_c -= r.0.len - ct
// If this substitution is before our cursor,
// we need to adjust our cursor's position.
if c > r.0.pos {
let c_o = r.0.len; // Old length
let c_n = r.1.chars().count(); // New length
if c_n > c_o {
// Move cursor right by difference
new_c += c_n - c_o;
} else if c_n < c_o {
// Move cursor left by difference
if new_c >= c_o - c_n {
new_c -= c_o - c_n;
} else { new_c = 0; }
}
}

View File

@ -6,11 +6,69 @@ use super::super::{
};
fn sub_string(s: &str) -> Option<&'static str> {
let r = match s {
/* Only found in operator tokens */
"*" => "×",
"/" => "÷",
"sqrt" => "",
"rt" => "",
/* Only found in word tokens */
// Greek letters
"alpha" => "α",
"beta" => "β",
"gamma" => "γ",
"delta" => "δ",
"epsilon" => "ε",
"zeta" => "ζ",
"eta" => "η",
"theta" => "θ",
//"iota" => {Some("ι")}, // looks just like i
//"kappa" => {Some("κ")}, // looks just like k
"lambda" => "λ",
"mu" => "μ",
//"nu" => {Some("ν")}, // looks just like v
"xi" => "ξ",
//"omicron" => {Some("ο")}, // looks exactly like o
"pi" => "π",
"rho" => "ρ",
"sigma" => "σ",
"tau" => "τ",
//"upsilon" => {Some("υ")}, // looks just like u
"phi" => "φ",
"chi" => "χ",
//"psi" => {Some("ψ")}, Conflict with pound / square inch
"omega" => "ω",
// Constants
"epsilon_zero" => "ε₀",
"eps_zero" => "ε₀",
"g_zero" => "g₀",
"mu_zero" => "μ₀",
"h_bar" => "",
// Misc
"deg" => "°",
_ => { return None; }
};
return Some(r);
}
// Finds substitutions in an array of tokens.
// Returns new token array and substitution list.
pub fn find_subs(
mut g: VecDeque<Token>,
) -> (
VecDeque<(LineLocation, String)>,
VecDeque<Token>
VecDeque<(LineLocation, String)>, // List of substrings to replace (in order)
VecDeque<Token> // New token array, with updated strings and linelocations
) {
// Array of replacements
@ -24,62 +82,19 @@ pub fn find_subs(
while g.len() > 0 {
let mut t = g.pop_front().unwrap();
let target: Option<&str> = match &mut t {
Token::Operator(_, s) => {
let target = match &s[..] {
"*" => {Some("×")},
"/" => {Some("÷")},
"sqrt" => {Some("")},
"rt" => {Some("")},
_ => {None}
};
let target = sub_string(s);
// Update token contents too.
// This makes sure that errors also contain the updated text.
// This makes errors and printouts use the updated string.
if target.is_some() { *s = String::from(target.unwrap()); }
target
},
Token::Word(_, s) => {
let target = match &s[..] {
// Greek letters
"alpha" => {Some("α")},
"beta" => {Some("β")},
"gamma" => {Some("γ")},
"delta" => {Some("δ")},
"epsilon" => {Some("ε")},
"zeta" => {Some("ζ")},
"eta" => {Some("η")},
"theta" => {Some("θ")},
//"iota" => {Some("ι")},
//"kappa" => {Some("κ")},
"lambda" => {Some("λ")},
"mu" => {Some("μ")},
//"nu" => {Some("ν")},
"xi" => {Some("ξ")},
//"omicron" => {Some("ο")},
"pi" => {Some("π")},
"rho" => {Some("ρ")},
"sigma" => {Some("σ")},
"tau" => {Some("τ")},
//"upsilon" => {Some("υ")},
"phi" => {Some("φ")},
"chi" => {Some("χ")},
//"psi" => {Some("ψ")}, Conflict with pound / square inch
"omega" => {Some("ω")},
// Constants
"epsilon_zero" => {Some("ε₀")},
"eps_zero" => {Some("ε₀")},
"g_zero" => {Some("g₀")},
"mu_zero" => {Some("μ₀")},
"h_bar" => {Some("")},
// Misc
"deg" => {Some("°")}
_ => {None}
};
let target = sub_string(s);
if target.is_some() { *s = String::from(target.unwrap()); }
target
},
@ -88,7 +103,7 @@ pub fn find_subs(
};
if target.is_none() {
// Even if nothing changed, we need to update token location
// Even if nothing changed, we need to update the new token's linelocation
let l = t.get_mut_linelocation();
*l = LineLocation{pos: l.pos - offset, len: l.len};
} else {

View File

@ -11,8 +11,8 @@ use crate::context::Context;
fn lookback_signs(
g: &mut VecDeque<Token>,
context: &Context
context: &Context,
g: &mut VecDeque<Token>
) -> Result<(), (LineLocation, DaisyError)> {
// Convert `-` operators to `neg` operators
@ -44,7 +44,7 @@ fn lookback_signs(
(Token::Operator(_, sa), Token::Operator(l,sb))
=> {
if {
let o = Operator::from_string(sa, context);
let o = Operator::from_string(context, sa);
o.is_some() &&
(
@ -101,11 +101,11 @@ fn lookback_signs(
// Inserts implicit operators
fn lookback(
g: &mut VecDeque<Token>,
context: &Context
context: &Context,
g: &mut VecDeque<Token>
) -> Result<(), (LineLocation, DaisyError)> {
lookback_signs(g, context)?;
lookback_signs(context, g)?;
let mut i: usize = 0;
while i < g.len() {
@ -142,7 +142,7 @@ fn lookback(
=> {
let la = la.clone();
let lb = lb.clone();
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
g.insert(i-1, b);
if o.is_some() {
@ -164,7 +164,7 @@ fn lookback(
=> {
let la = la.clone();
let lb = lb.clone();
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
g.insert(i-1, b);
if o.is_some() {
@ -196,8 +196,8 @@ fn lookback(
pub fn groupify(
mut g: VecDeque<Token>,
context: &Context
context: &Context,
mut g: VecDeque<Token>
) -> Result<
Token,
(LineLocation, DaisyError)
@ -240,7 +240,7 @@ pub fn groupify(
let (_, mut v) = levels.pop().unwrap();
let (_, v_now) = levels.last_mut().unwrap();
lookback(&mut v, context)?;
lookback(context, &mut v)?;
let q = is_tuple.pop().unwrap();
if q {
@ -275,7 +275,7 @@ pub fn groupify(
let (_, v_now) = levels.last_mut().unwrap();
if v.len() == 0 { return Err((l, DaisyError::EmptyGroup)) }
lookback(&mut v, context)?;
lookback(context, &mut v)?;
let q = is_tuple.pop().unwrap();
if q {
@ -294,7 +294,7 @@ pub fn groupify(
return Err((l, DaisyError::BadTuple));
}
lookback(&mut v, context)?;
lookback(context, &mut v)?;
return Ok(Token::Group(
LineLocation{pos:0, len:last_linelocation.pos + last_linelocation.len},

View File

@ -10,10 +10,10 @@ use super::super::{
// Called whenever a token is finished.
#[inline(always)]
fn push_token(
context: &Context,
g: &mut VecDeque<Token>,
t: Option<Token>,
stop_i: usize,
context: &Context
stop_i: usize
) {
if t.is_none() { return }
@ -60,7 +60,7 @@ fn push_token(
// Some operators are written as words.
if let Token::Word(l, s) = &t {
if Operator::from_string(s, context).is_some() {
if Operator::from_string(context, s).is_some() {
t = Token::Operator(*l, s.clone());
}
}
@ -69,7 +69,7 @@ fn push_token(
}
/// Turns a string into Tokens. First stage of parsing.
pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
pub fn tokenize(context: &Context, input: &String) -> VecDeque<Token> {
let mut t: Option<Token> = None; // The current token we're reading
let mut g: VecDeque<Token> = VecDeque::with_capacity(32);
@ -88,7 +88,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
// If we're not building a number, finalize
// previous token and start one.
_ => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::Quantity(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
@ -102,7 +102,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
Some(Token::Quantity(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::Word(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
@ -122,7 +122,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
} else {
// Otherwise, end the number.
// We probably have a subtraction.
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::Operator(
LineLocation{pos: i, len: 1},
String::from(c)
@ -134,7 +134,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
// Multi-character operators with - and + are NOT supported!
// (for example, we can't use -> for unit conversion)
_ => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::Operator(
LineLocation{pos: i, len: 1},
String::from(c)
@ -144,7 +144,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
},
',' => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::TupleDelim(LineLocation{pos: i, len: 1}));
},
@ -157,7 +157,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
match &mut t {
Some(Token::Operator(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::Operator(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
@ -165,17 +165,17 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
// Group
'(' => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::GroupStart(LineLocation{pos: i, len: 0}));
},
')' => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::GroupEnd(LineLocation{pos: i, len: 0}));
},
// Space. Basic seperator.
' ' => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = None;
}
@ -185,7 +185,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
Some(Token::Word(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i, context);
push_token(context, &mut g, t, i);
t = Some(Token::Word(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
@ -193,7 +193,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
};
}
push_token(&mut g, t, input.chars().count(), context);
push_token(context, &mut g, t, input.chars().count());
return g;
}

View File

@ -10,9 +10,9 @@ use super::super::{
};
fn treeify_binary(
context: &Context,
i: usize,
g_inner: &mut VecDeque<Token>,
context: &Context
g_inner: &mut VecDeque<Token>
) -> Result<bool, (LineLocation, DaisyError)> {
let this: &Token = &g_inner[i];
@ -56,7 +56,7 @@ fn treeify_binary(
if let Token::Operator(l, s) = left {
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
let o = o.unwrap();
@ -72,7 +72,7 @@ fn treeify_binary(
}
if let Token::Operator(l, s) = right {
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
let o = o.unwrap();
@ -91,7 +91,7 @@ fn treeify_binary(
// This operator
let this_op = {
let Token::Operator(l, s) = this else {panic!()};
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // bad operator string
o.unwrap()
};
@ -99,14 +99,14 @@ fn treeify_binary(
// The operators contesting our arguments
let left_op = if i > 1 {
let Token::Operator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad operator string
Some(o.unwrap())
} else { None };
let right_op = if i < g_inner.len()-2 {
let Token::Operator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad operator string
Some(o.unwrap())
} else { None };
@ -122,20 +122,20 @@ fn treeify_binary(
let right_pre = g_inner.remove(i-1).unwrap();
let mut left: Expression; let mut right: Expression;
if let Token::Group(l, _) = right_pre {
right = treeify(right_pre, context)?;
right = treeify(context, right_pre)?;
right.set_linelocation(&(right.get_linelocation() + l));
} else if let Token::Tuple(l, _) = right_pre {
right = treeify(right_pre, context)?;
right = treeify(context, right_pre)?;
right.set_linelocation(&(right.get_linelocation() + l));
} else {
right = right_pre.to_expression(context)?;
}
if let Token::Group(l, _) = left_pre {
left = treeify(left_pre, context)?;
left = treeify(context, left_pre)?;
left.set_linelocation(&(left.get_linelocation() + l));
} else if let Token::Tuple(l, _) = left_pre {
left = treeify(left_pre, context)?;
left = treeify(context, left_pre)?;
left.set_linelocation(&(left.get_linelocation() + l));
} else {
left = left_pre.to_expression(context)?;
@ -143,7 +143,7 @@ fn treeify_binary(
let (l, o) = {
let Token::Operator(l, s) = this_pre else {panic!()};
let o = Operator::from_string(&s, context);
let o = Operator::from_string(context, &s);
if o.is_none() { panic!() }
(l, o.unwrap())
};
@ -161,10 +161,10 @@ fn treeify_binary(
}
fn treeify_unary(
context: &Context,
i: usize,
g_inner: &mut VecDeque<Token>,
left_associative: bool,
context: &Context
left_associative: bool
) -> Result<bool, (LineLocation, DaisyError)> {
let this: &Token = &g_inner[i];
@ -224,7 +224,7 @@ fn treeify_unary(
// This operator
let this_op = {
let Token::Operator(l, s) = this else {panic!()};
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
o.unwrap()
};
@ -233,14 +233,14 @@ fn treeify_unary(
let next_op = if left_associative {
if i > 1 {
let Token::Operator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
Some(o.unwrap())
} else { None }
} else {
if i < g_inner.len()-2 {
let Token::Operator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s, context);
let o = Operator::from_string(context, s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
Some(o.unwrap())
} else { None }
@ -255,10 +255,10 @@ fn treeify_unary(
next_pre = g_inner.remove(i).unwrap();
}
if let Token::Group(l, _) = next_pre {
next = treeify(next_pre, context)?;
next = treeify(context, next_pre)?;
next.set_linelocation(&(next.get_linelocation() + l));
} else if let Token::Tuple(l, _) = next_pre {
next = treeify(next_pre, context)?;
next = treeify(context, next_pre)?;
next.set_linelocation(&(next.get_linelocation() + l));
} else {
next = next_pre.to_expression(context)?;
@ -267,7 +267,7 @@ fn treeify_unary(
let (l, o) = {
let Token::Operator(l, s) = this_pre else {panic!()};
let o = Operator::from_string(&s, context);
let o = Operator::from_string(context, &s);
if o.is_none() { panic!() }
(l, o.unwrap())
};
@ -292,8 +292,8 @@ fn treeify_unary(
pub fn treeify(
mut g: Token,
context: &Context
context: &Context,
mut g: Token
) -> Result<Expression, (LineLocation, DaisyError)> {
let (l, g_inner): (LineLocation, &mut VecDeque<Token>) = match g {
@ -301,7 +301,7 @@ pub fn treeify(
Token::Tuple(l, parts) => {
let mut t: VecDeque<Expression> = VecDeque::new();
for p in parts {
t.push_back(treeify(p, context)?);
t.push_back(treeify(context, p)?);
};
return Ok(Expression::Tuple(l, t));
@ -332,7 +332,7 @@ pub fn treeify(
// If not an operator, move on.
let this_op = match &g_inner[i] {
Token::Operator(l, s) => {
let o = Operator::from_string(&s, context);
let o = Operator::from_string(context, &s);
if o.is_none() { return Err((*l, DaisyError::Syntax)); }
o.unwrap()
},
@ -346,9 +346,9 @@ pub fn treeify(
let mut changed = false;
if this_op.is_left_associative() {
if this_op.is_binary() {
changed = treeify_binary(i, g_inner, context)?;
changed = treeify_binary(context, i, g_inner)?;
} else {
changed = treeify_unary(i, g_inner, left_associative, context)?;
changed = treeify_unary(context, i, g_inner, left_associative)?;
}
}
@ -360,9 +360,9 @@ pub fn treeify(
} else {
if !this_op.is_left_associative() {
if this_op.is_binary() {
treeify_binary(i, g_inner, context)?;
treeify_binary(context, i, g_inner)?;
} else {
treeify_unary(i, g_inner, left_associative, context)?;
treeify_unary(context, i, g_inner, left_associative)?;
}
}
j -= 1
@ -378,7 +378,7 @@ pub fn treeify(
},
Token::Tuple(_, _) |
Token::Group(_,_) => {
treeify(g, context)
treeify(context, g)
},

View File

@ -7,6 +7,7 @@ use std::ops::{
};
use std::cmp::Ordering;
use crate::context::Context;
use crate::quantity::Unit;
use crate::quantity::FreeUnit;
@ -21,12 +22,12 @@ pub struct Quantity {
impl ToString for Quantity {
fn to_string(&self) -> String {
impl Quantity {
pub fn display(&self, context: &Context) -> String {
let n = self.scalar.to_string();
if self.unitless() { return n; }
let u = self.unit.to_string();
let u = self.unit.display(context);
if self.is_one() { return u; };
if self.unit.no_space() {
@ -38,11 +39,11 @@ impl ToString for Quantity {
}
impl Quantity {
pub fn to_string_outer(&self) -> String {
pub fn display_outer(&self, context: &Context) -> String {
let n = self.scalar.to_string();
if self.unitless() { return n; }
let u = self.unit.to_string();
let u = self.unit.display(context);
if self.unit.no_space() {
return format!("{n}{u}");
} else {

View File

@ -8,6 +8,7 @@ use std::ops::{
use std::cmp::Ordering;
use super::ScalarBase;
use super::dec_to_sci;
macro_rules! foward {
@ -25,16 +26,39 @@ pub struct F64Base where {
}
impl ToString for F64Base {
fn to_string(&self) -> String { self.val.to_string() }
fn to_string(&self) -> String {
// Remove negative sign from string
let mut s = self.val.to_string();
let neg = s.starts_with("-");
if neg { s = String::from(&s[1..]); }
// Power of ten
let mut p: i64 = {
if let Some(x) = s.find(".") {
x as i64
} else {
s.len() as i64
}
};
p -= 1;
// We no longer need a decimal point in our string.
// also, trim off leading zeros and adjust power.
let mut s: &str = &s.replace(".", "");
s = &s[0..];
s = s.trim_end_matches('0');
while s.starts_with('0') {
s = &s[1..];
p -= 1;
}
return dec_to_sci(neg, s.to_string(), p);
}
}
impl ScalarBase for F64Base {
fn from_f64(f: f64) -> Option<F64Base> {
return Some(F64Base{ val: f });
}
fn from_string(s: &str) -> Option<F64Base> {
let v = s.parse::<f64>();
let v = match v {
@ -51,6 +75,7 @@ impl ScalarBase for F64Base {
fn is_one(&self) -> bool {self.val == 1f64}
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
fn is_int(&self) -> bool { self.val.floor() == self.val }
foward!(abs);
foward!(floor);
@ -60,9 +85,11 @@ impl ScalarBase for F64Base {
foward!(sin);
foward!(cos);
foward!(tan);
foward!(csc);
foward!(sec);
foward!(cot);
fn csc(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.sin() }) }
fn sec(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cos() }) }
fn cot(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.tan() }) }
foward!(asin);
foward!(acos);
foward!(atan);
@ -70,9 +97,11 @@ impl ScalarBase for F64Base {
foward!(sinh);
foward!(cosh);
foward!(tanh);
foward!(csch);
foward!(sech);
foward!(coth);
fn csch(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.sinh() }) }
fn sech(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cosh() }) }
fn coth(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.tanh() }) }
foward!(asinh);
foward!(acosh);
foward!(atanh);
@ -161,11 +190,11 @@ impl Rem<F64Base> for F64Base {
fn rem(self, modulus: F64Base) -> Self::Output {
if {
(!self.fract().unwrap().is_zero()) ||
(!modulus.fract().unwrap().is_zero())
(!self.is_int()) ||
(!modulus.is_int())
} { panic!() }
F64Base{val : self.val.fract() % modulus.val.fract()}
F64Base{val : self.val.round() % modulus.val.round()}
}
}

View File

@ -1,7 +1,7 @@
use rug::Float;
use rug::Assign;
use rug::ops::AssignRound;
use rug::ops::Pow;
use bigdecimal::BigDecimal;
use bigdecimal::Zero;
use bigdecimal::RoundingMode;
use std::str::FromStr;
use std::ops::{
Add, Sub, Mul, Div,
@ -14,158 +14,151 @@ use std::ops::{
use std::cmp::Ordering;
use super::ScalarBase;
use super::PRINT_LEN;
use super::FLOAT_PRECISION;
use super::dec_to_sci;
#[derive(Debug)]
#[derive(Clone)]
pub struct FloatBase where {
pub val: Float
pub val: BigDecimal
}
impl FloatBase {
pub fn from<T>(a: T) -> Option<FloatBase> where
Float: Assign<T> + AssignRound<T>
{
let v = Float::with_val(FLOAT_PRECISION, a);
return Some(FloatBase{ val: v });
pub fn new(s: &str) -> FloatBase {
return FloatBase {
val: s.parse().unwrap()
};
}
}
impl ToString for FloatBase {
fn to_string(&self) -> String {
let (sign, mut string, exp) = self.val.to_sign_string_exp(10, Some(PRINT_LEN));
// zero, nan, or inf.
let sign = if sign {"-"} else {""};
if exp.is_none() { return format!("{sign}{string}"); }
let exp = exp.unwrap();
// Remove trailing zeros.
// At this point, string is guaranteed to be nonzero.
while string.chars().last().unwrap() == '0' {
string.remove(string.len() - 1);
if self.val.is_nan() {
return "NaN".to_string();
} else if self.val.is_inf_neg() {
return "-Inf".to_string();
} else if self.val.is_inf_pos() {
return "+Inf".to_string();
}
let exp_u: usize;
if exp < 0 {
exp_u = (-exp).try_into().unwrap()
} else {
exp_u = exp.try_into().unwrap()
}
// Already in scientific notation,we just need to trim significant digits.
let mut _a = self.val.round(32, astro_float::RoundingMode::Up).to_string();
let mut _b = _a.split('e');
if exp_u >= PRINT_LEN {
// Exponential notation
let pre = &string[0..1];
let post = &string[1..];
let mut s = String::from(_b.next().unwrap()); // Decimal
let p: i64 = _b.next().unwrap().parse().unwrap(); // Exponent
format!(
"{pre}{}{post}e{}",
if post.len() != 0 {"."} else {""},
//if exp > 0 {"+"} else {""},
exp - 1
)
} else {
if exp <= 0 { // Decimal, needs `0.` and leading zeros
format!(
"{sign}0.{}{string}",
"0".repeat(exp_u)
)
} else if exp_u < string.len() { // Decimal, needs only `.`
format!(
"{sign}{}.{}",
&string[0..exp_u],
&string[exp_u..]
)
} else { // Integer, needs trailing zeros
format!(
"{sign}{string}{}",
"0".repeat(exp_u - string.len())
)
}
}
// Remove negative sign from string
let neg = s.starts_with("-");
if neg { s = String::from(&s[1..]); }
// We no longer need a decimal point in our string.
// also, trim off leading zeros and adjust power.
let mut s: &str = &s.replace(".", "");
s = &s[0..];
s = s.trim_end_matches('0');
s = s.trim_start_matches('0');
return dec_to_sci(neg, s.to_string(), p);
}
}
macro_rules! foward {
( $x:ident ) => {
fn $x(&self) -> Option<FloatBase> {
Some(FloatBase{ val: self.val.clone().$x()})
}
}
}
impl ScalarBase for FloatBase {
fn from_f64(f: f64) -> Option<FloatBase> {
let v = Float::with_val(FLOAT_PRECISION, f);
return Some(FloatBase{ val: v });
}
fn from_string(s: &str) -> Option<FloatBase> {
let v = Float::parse(s);
let v = BigDecimal::from_str(s);
let v = match v {
Ok(x) => x,
Err(_) => return None
};
return Some(
FloatBase{ val:
Float::with_val(FLOAT_PRECISION, v)
}
);
return Some(FloatBase{ val: v });
}
foward!(fract);
//foward!(fract);
fn is_zero(&self) -> bool {self.val.is_zero()}
fn is_one(&self) -> bool {self.val == Float::with_val(FLOAT_PRECISION, 1)}
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
fn is_one(&self) -> bool {self.val == BigDecimal::from_str("1").unwrap()}
fn is_negative(&self) -> bool { self.val.sign() == num::bigint::Sign::Minus }
fn is_positive(&self) -> bool { self.val.sign() == num::bigint::Sign::Plus }
fn is_int(&self) -> bool {
self.fract() == FloatBase::from_f64(0f64)
fn is_int(&self) -> bool { self.val.is_integer() }
fn abs(&self) -> Option<FloatBase> { Some(FloatBase{ val: self.val.abs() }) }
fn round(&self) -> Option<FloatBase> { Some(FloatBase{ val: self.val.round(0) }) }
fn floor(&self) -> Option<FloatBase> {
let (_, scale) = self.val.as_bigint_and_exponent();
Some(FloatBase{ val: self.val.with_scale_round(scale, RoundingMode::Down) })
}
foward!(abs);
foward!(floor);
foward!(ceil);
foward!(round);
foward!(sin);
foward!(cos);
foward!(tan);
foward!(csc);
foward!(sec);
foward!(cot);
foward!(asin);
foward!(acos);
foward!(atan);
foward!(sinh);
foward!(cosh);
foward!(tanh);
foward!(csch);
foward!(sech);
foward!(coth);
foward!(asinh);
foward!(acosh);
foward!(atanh);
foward!(exp);
foward!(ln);
foward!(log10);
foward!(log2);
fn log(&self, base: FloatBase) -> Option<FloatBase> {
Some(FloatBase{ val: self.val.clone().log10() } / base.log10().unwrap())
fn ceil(&self) -> Option<FloatBase> {
let (_, scale) = self.val.as_bigint_and_exponent();
Some(FloatBase{ val: self.val.with_scale_round(scale, RoundingMode::Up) })
}
fn pow(&self, base: FloatBase) -> Option<FloatBase> {
Some(FloatBase{ val: self.val.clone().pow(base.val)})
fn fract(&self) -> Option<FloatBase> { Some(self.clone() - self.floor().unwrap()) }
fn sin(&self) -> Option<FloatBase> {
let c0: BigDecimal = "1.276278962".parse().unwrap();
let c1: BigDecimal = "-.285261569".parse().unwrap();
let c2: BigDecimal = "0.009118016".parse().unwrap();
let c3: BigDecimal = "-.000136587".parse().unwrap();
let c4: BigDecimal = "0.000001185".parse().unwrap();
let c5: BigDecimal = "-.000000007".parse().unwrap();
// z should be between -0.25 to 0.25 (percent of a full circle)
let z: BigDecimal = self.val.clone() / 360f64;
let w = BigDecimal::from(4) * z;
let x: BigDecimal = 2 * w.clone() * w.clone() - 1;
let p = (
c0 * 1 +
c1 * x.clone() +
c2 * (2 * x.clone()*x.clone() - 1) +
c3 * (4 * x.clone()*x.clone()*x.clone() - 3 * x.clone()) +
c4 * (8 * x.clone()*x.clone()*x.clone()*x.clone() - 8 * x.clone()*x.clone() + 1) +
c5 * (16 * x.clone()*x.clone()*x.clone()*x.clone()*x.clone() - 20 * x.clone()*x.clone()*x.clone() + 5 * x.clone())
) * w;
return Some(FloatBase{ val: p })
}
fn cos(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn tan(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn csc(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn sec(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn cot(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn asin(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn acos(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn atan(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn sinh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn cosh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn tanh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn csch(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn sech(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn coth(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn asinh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn acosh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn atanh(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn exp(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn ln(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn log10(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn log2(&self) -> Option<FloatBase> { Some(FloatBase{ val: "1".parse().unwrap() }) }
fn log(&self, _base: FloatBase) -> Option<FloatBase> {
Some(FloatBase{ val: "1".parse().unwrap() })
}
fn pow(&self, _base: FloatBase) -> Option<FloatBase> {
Some(FloatBase{ val: "1".parse().unwrap() })
}
}
@ -223,7 +216,7 @@ impl Div for FloatBase {
impl DivAssign for FloatBase where {
fn div_assign(&mut self, other: Self) {
self.val /= other.val;
self.val = self.val.clone() / other.val;
}
}
@ -240,11 +233,11 @@ impl Rem<FloatBase> for FloatBase {
fn rem(self, modulus: FloatBase) -> Self::Output {
if {
(!self.fract().unwrap().is_zero()) ||
(!modulus.fract().unwrap().is_zero())
(!self.is_int()) ||
(!modulus.is_int())
} { panic!() }
FloatBase{val : self.val.fract() % modulus.val.fract()}
FloatBase{val : self.val.round(0) % modulus.val.round(0)}
}
}

View File

@ -1,10 +1,92 @@
const FLOAT_PRECISION: u32 = 1024;
const PRINT_LEN: usize = 5; // How many significant digits we will show in output
//const FLOAT_PRECISION: u32 = 1024;
const SHOW_SIG: usize = 5; // How many significant digits we will show in output
const MAX_LEN: usize = 5; // If a scientific exponent is >= this value, do not use scientific notation.
pub(in self) mod rationalbase;
pub(in self) mod floatbase;
//mod f64base;
// Pick a float implementation.
// floatbase is high-precision, f64base is for testing.
//pub(in self) mod floatbase;
//pub use floatbase::FloatBase;
pub(in self) mod f64base;
pub use f64base::F64Base as FloatBase;
mod scalar;
pub use self::scalar::Scalar;
pub use self::scalar::ScalarBase;
// Convert a string to scientific notation,
// with parameters SHOW_SIG and MAX_LEN.
//
// input:
// neg: true if negative
// s: decimal portion. Must contain only digits and a single decimal point.
// zeros must be stripped from both ends.
// p: power of ten to multiply by.
//
// So, (-1)^(neg) + (s * 10^p) should give us our number.
#[allow(dead_code)]
pub(in self) fn dec_to_sci(neg: bool, mut s: String, p: i64) -> String {
// Pick significant digits and round
if s.len() > SHOW_SIG {
let round;
if s.len() != SHOW_SIG + 1 {
round = s[SHOW_SIG..SHOW_SIG+1].parse().unwrap();
} else { round = 0; }
s = String::from(&s[0..SHOW_SIG]);
if round >= 5 {
let new = s[s.len()-1..s.len()].parse::<u8>().unwrap() + 1u8;
if new != 10 {
s = format!("{}{new}", &s[0..s.len()-1]);
}
}
}
s = format!("{s}{}", "0".repeat(SHOW_SIG - s.len()));
// at this point, s is guaranteed to have exactly SHOW_SIG digits.
let neg = if neg {"-"} else {""};
if (p.abs() as usize) < MAX_LEN {
// Print whole decimal
if p >= 0 {
let q = p as usize;
let first = &s[0..q+1];
let mut rest = &s[q+1..];
rest = rest.trim_end_matches('0');
if rest == "" {
return format!("{neg}{first}");
} else {
return format!("{neg}{first}.{rest}");
}
} else {
let q = p.abs() as usize;
let t = format!("0.{}{s}", "0".repeat(q-1));
return format!("{neg}{}", t.trim_end_matches('0'));
}
} else {
// Print full scientific notation
let first = &s[0..1];
let mut rest = &s[1..];
rest = rest.trim_end_matches('0');
if rest == "" {
return format!("{neg}{first}e{p}");
} else {
return format!("{neg}{first}.{rest}e{p}");
}
}
}

View File

@ -1,5 +1,7 @@
use rug::Rational;
use rug::Integer;
use num::rational::BigRational;
use num::BigInt;
use num::Num;
use num::Signed;
use std::ops::{
Add, Sub, Mul, Div,
@ -22,7 +24,7 @@ macro_rules! cant_do {
#[derive(Debug)]
#[derive(Clone)]
pub struct RationalBase where {
pub val: Rational
pub val: BigRational
}
impl ToString for RationalBase{
@ -33,18 +35,12 @@ impl ToString for RationalBase{
impl RationalBase {
pub fn from_frac(t: i64, b: i64) -> Option<RationalBase> {
let v = Rational::from((t, b));
let v = BigRational::new_raw(BigInt::from(t), BigInt::from(b));
return Some(RationalBase{ val: v });
}
}
impl ScalarBase for RationalBase {
fn from_f64(f: f64) -> Option<RationalBase> {
let v = Rational::from_f64(f);
if v.is_none() { return None }
return Some(RationalBase{ val: v.unwrap() });
}
fn from_string(s: &str) -> Option<RationalBase> {
// Scientific notation
let mut sci = s.split("e");
@ -89,7 +85,7 @@ impl ScalarBase for RationalBase {
// From fraction string
let r = Rational::from_str_radix(&s, 10);
let r = BigRational::from_str_radix(&s, 10);
let r = match r {
Ok(x) => x,
Err(_) => return None
@ -100,18 +96,13 @@ impl ScalarBase for RationalBase {
}
fn fract(&self) -> Option<RationalBase> {
Some(RationalBase{val: self.val.clone().fract_floor(Integer::new()).0})
}
fn fract(&self) -> Option<RationalBase> { Some(RationalBase{val: self.val.fract()}) }
fn is_int(&self) -> bool { self.val.is_integer() }
fn is_int(&self) -> bool {
self.fract() == RationalBase::from_f64(0f64)
}
fn is_zero(&self) -> bool {self.val == Rational::from((0,1))}
fn is_one(&self) -> bool {self.val == Rational::from((1,1))}
fn is_negative(&self) -> bool { self.val.clone().signum() == -1 }
fn is_positive(&self) -> bool { self.val.clone().signum() == 1 }
fn is_zero(&self) -> bool {self.val == BigRational::from_integer(BigInt::from(0))}
fn is_one(&self) -> bool {self.val == BigRational::from_integer(BigInt::from(1))}
fn is_negative(&self) -> bool { self.val.is_negative() }
fn is_positive(&self) -> bool { self.val.is_positive() }
fn abs(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().abs()})}
fn floor(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().floor()})}
@ -153,9 +144,7 @@ impl Add for RationalBase where {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self {
val: self.val + other.val
}
Self { val: self.val + other.val }
}
}
@ -169,9 +158,7 @@ impl Sub for RationalBase {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self {
val: self.val - other.val
}
Self { val: self.val - other.val }
}
}
@ -185,9 +172,7 @@ impl Mul for RationalBase {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
Self {
val: self.val * other.val
}
Self { val: self.val * other.val }
}
}
@ -201,9 +186,7 @@ impl Div for RationalBase {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
Self {
val: self.val / other.val
}
Self { val: self.val / other.val }
}
}
@ -217,9 +200,7 @@ impl Neg for RationalBase where {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
val: -self.val
}
Self { val: -self.val }
}
}
@ -228,15 +209,15 @@ impl Rem<RationalBase> for RationalBase {
fn rem(self, modulus: RationalBase) -> Self::Output {
if {
*self.val.denom() != 1 ||
*modulus.val.denom() != 1
*self.val.denom() != BigInt::from(1) ||
*modulus.val.denom() != BigInt::from(1)
} { panic!() }
RationalBase{
val : Rational::from((
val : BigRational::new_raw(
self.val.numer() % modulus.val.numer(),
1
))
BigInt::from(1)
)
}
}
}

View File

@ -7,7 +7,7 @@ use std::ops::{
};
use std::cmp::Ordering;
use super::floatbase::FloatBase as FloatBase;
use super::FloatBase as FloatBase;
use super::rationalbase::RationalBase;
@ -21,7 +21,6 @@ pub trait ScalarBase:
PartialEq + PartialOrd
{
// Creation
fn from_f64(f: f64) -> Option<Self>;
fn from_string(s: &str) -> Option<Self>;
// Utility
@ -87,8 +86,8 @@ fn to_float(r: Scalar) -> Scalar {
match &r {
Scalar::Float {..} => r,
Scalar::Rational {v} => wrap_float!(
FloatBase::from(v.val.numer()).unwrap() /
FloatBase::from(v.val.denom()).unwrap()
FloatBase::from_string(&v.val.numer().to_string()).unwrap() /
FloatBase::from_string(&v.val.denom().to_string()).unwrap()
)
}
}
@ -105,13 +104,13 @@ impl ToString for Scalar {
// Creation methods
impl Scalar {
pub fn new_float(f: f64) -> Option<Self> {
let v = FloatBase::from_f64(f);
let v = FloatBase::from_string(&f.to_string());
if v.is_none() { return None; }
return Some(wrap_float!(v.unwrap()));
}
pub fn new_rational(f: f64) -> Option<Self> {
let r = RationalBase::from_f64(f);
let r = RationalBase::from_string(&f.to_string());
if r.is_none() { return None; }
return Some(wrap_rational!(r.unwrap()));
}
@ -185,8 +184,8 @@ impl Scalar {
pub fn is_nan(&self) -> bool {
match self {
Scalar::Float {v} => {v.val.is_nan()},
Scalar::Rational {..} => {panic!()}
Scalar::Float{ v } => {v.val.is_nan()},
Scalar::Rational {..} => {false}
}
}

View File

@ -4,6 +4,7 @@ use std::ops::{
MulAssign, DivAssign
};
use crate::context::Context;
use crate::quantity::Scalar;
use crate::quantity::Quantity;
use super::FreeUnit;
@ -16,8 +17,8 @@ pub struct Unit {
pub val: HashMap<FreeUnit, Scalar>
}
impl ToString for Unit {
fn to_string(&self) -> String {
impl Unit {
pub fn display(&self, context: &Context) -> String {
if self.unitless() { return String::new(); };
@ -37,7 +38,11 @@ impl ToString for Unit {
if *p == Scalar::new_rational(1f64).unwrap() {
t.push_str(&format!("{c}·"));
} else if p.is_int() && !p.to_string().contains("e"){
} else if {
context.config.enable_super_powers &&
p.is_int() &&
!p.to_string().contains("e")
} {
t.push_str(&c);
for c in p.to_string().chars() {
t.push( match c {
@ -74,7 +79,11 @@ impl ToString for Unit {
bottom_count += 1;
if t.len() != 0 && *p == Scalar::new_rational(-1f64).unwrap() {
b.push_str(&format!("{c}·"));
} else if p.is_int() && !p.to_string().contains("e") {
} else if {
context.config.enable_super_powers &&
p.is_int() &&
!p.to_string().contains("e")
} {
b.push_str(&c);
for c in p.to_string().chars() {
if c == '-' && t.len() != 0 { continue; }
@ -266,7 +275,6 @@ impl Unit {
let mut q = Quantity::new_rational(1f64).unwrap();
q.set_unit(b);
return Some(q);
}
}

View File

@ -10,8 +10,10 @@ fn eval_to_str(s: &str) -> Result<String, ()> {
};
//let out_str = g.print();
return match evaluate(&g, &mut Context::new()) {
Ok(x) => Ok(x.to_string_outer()),
let mut c = Context::new();
return match evaluate(&mut c, &g) {
Ok(x) => Ok(x.display_outer(&c)),
Err(_) => Err(())
};
}
@ -129,7 +131,7 @@ fn operators() {
good_expr("125", "5^(+3)");
good_expr("125", "+5^3");
good_expr("0.2148", "3 ^ (-1.4)");
good_expr("0.21479", "3 ^ (-1.4)");
// Should parse as ((2^3)^4)^5
good_expr("1.1529e18", "2^3^4^5");
@ -160,6 +162,7 @@ fn operators() {
good_expr("2", "6/3");
good_expr("2", "5%3");
good_expr("4", "2^5 mod 7");
good_expr("8", "5+3");
good_expr("64", "4^3");
good_expr("64", "4 ^ 3");
@ -182,6 +185,7 @@ fn operators() {
bad_expr("1e5!");
bad_expr("0^(-1)");
bad_expr("pi!");
bad_expr("2.5 mod 8");
}
#[test]