mirror of
https://github.com/rm-dr/daisy
synced 2025-02-21 22:49:24 -08:00
Compare commits
No commits in common. "bab305e11d7c8f88ed9ef243d5ecadfe781df83d" and "a6c3ffa68dfe9d79f532e1c985eadfd4f1858c26" have entirely different histories.
bab305e11d
...
a6c3ffa68d
168
Cargo.lock
generated
168
Cargo.lock
generated
@ -8,6 +8,12 @@ version = "1.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "az"
|
||||||
|
version = "1.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
@ -22,14 +28,24 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "daisycalc"
|
name = "daisycalc"
|
||||||
version = "1.1.0"
|
version = "1.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"num",
|
"rug",
|
||||||
"termion",
|
"termion",
|
||||||
"toml",
|
"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]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.12.3"
|
version = "0.12.3"
|
||||||
@ -48,9 +64,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.147"
|
version = "0.2.140"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
@ -58,82 +74,6 @@ version = "2.5.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
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]]
|
[[package]]
|
||||||
name = "numtoa"
|
name = "numtoa"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@ -158,6 +98,17 @@ dependencies = [
|
|||||||
"redox_syscall",
|
"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]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.164"
|
version = "1.0.164"
|
||||||
@ -219,6 +170,63 @@ dependencies = [
|
|||||||
"winnow",
|
"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]]
|
[[package]]
|
||||||
name = "winnow"
|
name = "winnow"
|
||||||
version = "0.4.6"
|
version = "0.4.6"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "daisycalc"
|
name = "daisycalc"
|
||||||
version = "1.1.0"
|
version = "1.0.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
build = "buildscript/main.rs"
|
build = "buildscript/main.rs"
|
||||||
license = "GPL-3.0-only"
|
license = "GPL-3.0-only"
|
||||||
@ -28,8 +28,7 @@ cfg-if = "1.0.0"
|
|||||||
|
|
||||||
[target.'cfg(target_family = "unix")'.dependencies]
|
[target.'cfg(target_family = "unix")'.dependencies]
|
||||||
termion = "2.0.1"
|
termion = "2.0.1"
|
||||||
num = "0.4.1"
|
rug = "1.19.2"
|
||||||
#astro-float = "0.7.1"
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
toml = "0.7.4"
|
toml = "0.7.4"
|
31
TODO.md
31
TODO.md
@ -1,40 +1,46 @@
|
|||||||
## Version Bump checklist
|
## Version Bump checklist
|
||||||
- TODO: build and publish script
|
|
||||||
- update Cargo.toml
|
- update Cargo.toml
|
||||||
- run cargo test
|
- run cargo test
|
||||||
- commit
|
- commit
|
||||||
- push
|
- git tag -a v1.0.0 -m "Version 1.0.0"
|
||||||
- merge
|
- git push
|
||||||
- git tag -a v1.0.0 -m "Version 1.0.0" on merge commit
|
- git push origin v1.0.0
|
||||||
- cargo publish
|
- cargo publish
|
||||||
- Update packages
|
- Update packages
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Pre-release
|
## Pre-release
|
||||||
|
- Fix linelocation (consistent, what does an operator's linelocation mean?)
|
||||||
- Tuple operations
|
- Tuple operations
|
||||||
- we don't need vectors as arguments to operators
|
- we don't need vectors as arguments to operators
|
||||||
- Fix linelocation when evaluating functions
|
- Assignment tests
|
||||||
|
|
||||||
## Parser
|
## Parser
|
||||||
|
- Better error when `sin = 2`
|
||||||
- Should functions be operators?
|
- Should functions be operators?
|
||||||
- Binary, hex, octal numbers
|
- Binary, hex, octal numbers
|
||||||
|
|
||||||
|
|
||||||
## General
|
## General
|
||||||
- Better tests (assignment, many expressions in one context)
|
|
||||||
- Optional config file
|
- Optional config file
|
||||||
- Optional history file
|
- Optional history file
|
||||||
|
- daisyrc file
|
||||||
- Compile to WASM, publish a webapp
|
- Compile to WASM, publish a webapp
|
||||||
- evaluate straight from command line
|
- evaluate straight from command line
|
||||||
- Package for debian, nix
|
- Auto-push to crates.io
|
||||||
|
- Package for debian
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Internals
|
## Internals
|
||||||
- Non-recursive treeify
|
- Non-recursive treeify
|
||||||
- Faster factorial function. Maybe use gamma instead?
|
- Faster factorial function. Maybe use gamma instead?
|
||||||
- Arbitrary precision float (rug doesn't offer arbitrary exponents)
|
- Arbitrary precision float (rug doesn't offer arbitrary exponents)
|
||||||
|
- Remove rug dependency (too big, incompatible)
|
||||||
|
|
||||||
## Math Features
|
## Math Features
|
||||||
|
- Dice
|
||||||
- Mean, Median, Min
|
- Mean, Median, Min
|
||||||
- Arbitrary base logarithm
|
- Arbitrary base logarithm
|
||||||
- Derivatives
|
- Derivatives
|
||||||
@ -42,16 +48,21 @@
|
|||||||
- Complex numbers
|
- Complex numbers
|
||||||
- acot/acoth functions
|
- acot/acoth functions
|
||||||
- Sums and products with functional arguments
|
- Sums and products with functional arguments
|
||||||
- Add functions: gcd, inverse mod, dice
|
|
||||||
|
|
||||||
## Prompt
|
## Prompt
|
||||||
- Fix terminal color detection
|
|
||||||
- Live syntax/output (like firefox js terminal)
|
- Live syntax/output (like firefox js terminal)
|
||||||
- Syntax highlighting
|
- Syntax highlight input and output
|
||||||
- fish-style tab completion
|
- fish-style tab completion
|
||||||
- Numbered expressions, history recall
|
- Numbered expressions, history recall
|
||||||
|
- Color configuration
|
||||||
- Enable/disable unit sets (defaults?)
|
- Enable/disable unit sets (defaults?)
|
||||||
- Consistent unit ordering
|
- 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
|
## Units
|
||||||
- long prefixes (megatonne, etc)
|
- long prefixes (megatonne, etc)
|
||||||
|
11
shell.nix
11
shell.nix
@ -1,11 +0,0 @@
|
|||||||
let
|
|
||||||
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/f155f0cf4ea43c4e3c8918d2d327d44777b6cad4.tar.gz") {};
|
|
||||||
in pkgs.mkShell {
|
|
||||||
buildInputs = with pkgs; [
|
|
||||||
cargo
|
|
||||||
rustc
|
|
||||||
rustfmt
|
|
||||||
m4
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
@ -224,7 +224,7 @@ pub fn do_command(
|
|||||||
if args.len() != 2 {
|
if args.len() != 2 {
|
||||||
return FormattedText::new(
|
return FormattedText::new(
|
||||||
format!(
|
format!(
|
||||||
"[c]{first}[n] [t]takes exactly one argument.[n]\n\n",
|
"[c]{first}[n] [t]takes exactly two arguments.[n]\n\n",
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ pub struct Config {
|
|||||||
// with prettier unicode alternatives?
|
// with prettier unicode alternatives?
|
||||||
//
|
//
|
||||||
// Automatically disabled if enable_unicode is off.
|
// Automatically disabled if enable_unicode is off.
|
||||||
pub enable_substituion: bool,
|
//pub enable_substituion: bool,
|
||||||
|
|
||||||
// Should we print simple powers
|
// Should we print simple powers
|
||||||
// as unicode superscript chars?
|
// as unicode superscript chars?
|
||||||
@ -38,7 +38,7 @@ impl Config {
|
|||||||
pub fn new() -> Config {
|
pub fn new() -> Config {
|
||||||
Config{
|
Config{
|
||||||
term_color_type: 2,
|
term_color_type: 2,
|
||||||
enable_substituion: true,
|
//enable_substituion: true,
|
||||||
//enable_unicode: true,
|
//enable_unicode: true,
|
||||||
enable_super_powers: true,
|
enable_super_powers: true,
|
||||||
enable_one_over_power: true
|
enable_one_over_power: true
|
||||||
@ -61,7 +61,7 @@ impl Config {
|
|||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
//#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
|
||||||
@ -76,12 +76,12 @@ pub struct Context {
|
|||||||
// General functions
|
// General functions
|
||||||
impl Context {
|
impl Context {
|
||||||
pub fn new() -> Context {
|
pub fn new() -> Context {
|
||||||
Context {
|
Context{
|
||||||
config: Config::new(),
|
config: Config::new(),
|
||||||
history: Vec::new(),
|
history: Vec::new(),
|
||||||
variables: HashMap::new(),
|
variables: HashMap::new(),
|
||||||
functions: HashMap::new(),
|
functions: HashMap::new(),
|
||||||
shadow: HashMap::new()
|
shadow: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +126,6 @@ impl Context {
|
|||||||
} else { panic!() }
|
} else { panic!() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can we define a new variable with this name?
|
|
||||||
pub fn valid_varible(&self, s: &str) -> bool {
|
pub fn valid_varible(&self, s: &str) -> bool {
|
||||||
if {
|
if {
|
||||||
Function::from_string(s).is_some() ||
|
Function::from_string(s).is_some() ||
|
||||||
@ -146,17 +145,10 @@ impl Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can we get a value fro mthis variable name?
|
|
||||||
pub fn is_varible(&self, s: &str) -> bool {
|
pub fn is_varible(&self, s: &str) -> bool {
|
||||||
return {
|
return {
|
||||||
(
|
self.valid_varible(s) &&
|
||||||
s == "ans" &&
|
(self.variables.contains_key(s) || self.shadow.contains_key(s))
|
||||||
self.history.len() != 0
|
|
||||||
) ||
|
|
||||||
(
|
|
||||||
self.valid_varible(s) &&
|
|
||||||
(self.variables.contains_key(s) || self.shadow.contains_key(s))
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use termion::raw::RawTerminal;
|
use termion::raw::RawTerminal;
|
||||||
use crate::formattedtext;
|
use termion::color;
|
||||||
|
use termion::style;
|
||||||
use crate::parser::substitute_cursor;
|
use crate::parser::substitute_cursor;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
|
||||||
const PROMPT_STR: &str = "==> ";
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PromptBuffer {
|
pub struct PromptBuffer {
|
||||||
// History
|
// History
|
||||||
@ -39,11 +38,30 @@ impl PromptBuffer {
|
|||||||
|
|
||||||
// Same as write_primpt, but pretends there is no cursor
|
// Same as write_primpt, but pretends there is no cursor
|
||||||
pub fn write_prompt_nocursor(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
pub fn write_prompt_nocursor(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||||
let tmp = self.cursor;
|
// Draw prettyprinted expression
|
||||||
self.cursor = 0;
|
let (_, s) = substitute_cursor(context, &self.get_contents(), self.buffer.chars().count());
|
||||||
let r = self.write_prompt(context, stdout);
|
|
||||||
self.cursor = tmp;
|
write!(
|
||||||
return r;
|
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)
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.last_print_len = s.chars().count();
|
||||||
|
stdout.flush()?;
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_prompt(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
pub fn write_prompt(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||||
@ -51,30 +69,38 @@ impl PromptBuffer {
|
|||||||
let i = if l == 0 {0} else {l - self.cursor};
|
let i = if l == 0 {0} else {l - self.cursor};
|
||||||
|
|
||||||
// Draw prettyprinted expression
|
// Draw prettyprinted expression
|
||||||
let (display_c, s) = substitute_cursor(context, &self.get_contents(), i);
|
let (display_cursor, s) = substitute_cursor(context, &self.get_contents(), i);
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
stdout, "\r{}{PROMPT_STR}{}{}",
|
stdout, "\r{}{}==>{}{} {}",
|
||||||
formattedtext::format_map('p', context).unwrap(),
|
style::Bold,
|
||||||
formattedtext::format_map('n', context).unwrap(),
|
color::Fg(color::Blue),
|
||||||
|
color::Fg(color::Reset),
|
||||||
|
style::Reset,
|
||||||
s
|
s
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// If this string is shorter, clear the remaining old one.
|
// If this string is shorter, clear the remaining old one.
|
||||||
if s.chars().count() < self.last_print_len {
|
if s.chars().count() < self.last_print_len {
|
||||||
write!(
|
write!(
|
||||||
stdout, "{}",
|
stdout, "{}{}",
|
||||||
" ".repeat(self.last_print_len - s.chars().count()),
|
" ".repeat(self.last_print_len - s.chars().count()),
|
||||||
|
termion::cursor::Left((self.last_print_len - s.chars().count()) as u16)
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(
|
|
||||||
stdout, "\r{}",
|
// Move cursor to correct position
|
||||||
termion::cursor::Right((display_c + PROMPT_STR.chars().count()) as u16)
|
if display_cursor != 0 {
|
||||||
)?;
|
write!(
|
||||||
|
stdout, "{}",
|
||||||
|
termion::cursor::Left(display_cursor as u16)
|
||||||
|
)?;
|
||||||
|
stdout.flush()?;
|
||||||
|
}
|
||||||
|
self.last_print_len = s.chars().count();
|
||||||
|
|
||||||
stdout.flush()?;
|
stdout.flush()?;
|
||||||
self.last_print_len = s.chars().count();
|
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -23,11 +23,11 @@ pub fn main() -> Result<(), std::io::Error> {
|
|||||||
// Set color compatibilty
|
// Set color compatibilty
|
||||||
let term_colors = stdout.available_colors().unwrap_or(0);
|
let term_colors = stdout.available_colors().unwrap_or(0);
|
||||||
if term_colors >= 256 {
|
if term_colors >= 256 {
|
||||||
context.config.term_color_type = 2;
|
context.config.term_color_type = 2
|
||||||
} else if term_colors >= 8 {
|
} else if term_colors >= 8 {
|
||||||
context.config.term_color_type = 1;
|
context.config.term_color_type = 1
|
||||||
} else {
|
} else {
|
||||||
context.config.term_color_type = 0;
|
context.config.term_color_type = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
context.config.check();
|
context.config.check();
|
||||||
|
@ -63,7 +63,7 @@ pub fn evaluate(
|
|||||||
|
|
||||||
context.get_variable(&s)
|
context.get_variable(&s)
|
||||||
},
|
},
|
||||||
Expression::Operator(_, Operator::Function(_), _) => { eval_function(g)? },
|
Expression::Operator(_, Operator::Function(_), _) => { Some(eval_function(g)?) },
|
||||||
Expression::Operator(_, _, _) => { eval_operator(context, g)? },
|
Expression::Operator(_, _, _) => { eval_operator(context, g)? },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ fn to_radians(q: Quantity) -> Result<Quantity, ()> {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
|
pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyError)> {
|
||||||
|
|
||||||
let Expression::Operator(loc, Operator::Function(f), args) = g else {unreachable!()};
|
let Expression::Operator(loc, Operator::Function(f), args) = g else {unreachable!()};
|
||||||
|
|
||||||
@ -41,112 +41,113 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
let Expression::Quantity(l, q) = a else { return Ok(None); };
|
let Expression::Quantity(l, q) = a else {panic!()};
|
||||||
|
|
||||||
|
|
||||||
match f {
|
match f {
|
||||||
Function::NoUnit => { return Ok(Some(Expression::Quantity(*loc + *l, q.without_unit()))); }
|
Function::NoUnit => { return Ok(Expression::Quantity(*loc + *l, q.without_unit())); }
|
||||||
Function::ToBase => { return Ok(Some(Expression::Quantity(*loc + *l, q.convert_to_base()))); }
|
Function::ToBase => { return Ok(Expression::Quantity(*loc + *l, q.convert_to_base())); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Function::Abs => {
|
Function::Abs => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.abs())));
|
return Ok(Expression::Quantity(*loc + *l, q.abs()));
|
||||||
},
|
},
|
||||||
Function::Floor => {
|
Function::Floor => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.floor())));
|
return Ok(Expression::Quantity(*loc + *l, q.floor()));
|
||||||
},
|
},
|
||||||
Function::Ceil => {
|
Function::Ceil => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.ceil())));
|
return Ok(Expression::Quantity(*loc + *l, q.ceil()));
|
||||||
},
|
},
|
||||||
Function::Round => {
|
Function::Round => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.round())));
|
return Ok(Expression::Quantity(*loc + *l, q.round()));
|
||||||
},
|
},
|
||||||
Function::NaturalLog => {
|
Function::NaturalLog => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.ln())));
|
return Ok(Expression::Quantity(*loc + *l, q.ln()));
|
||||||
},
|
},
|
||||||
Function::TenLog => {
|
Function::TenLog => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.log10())));
|
return Ok(Expression::Quantity(*loc + *l, q.log10()));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Function::Sin => {
|
Function::Sin => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sin())));
|
return Ok(Expression::Quantity(*loc + *l, q.sin()));
|
||||||
},
|
},
|
||||||
Function::Cos => {
|
Function::Cos => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.cos())));
|
return Ok(Expression::Quantity(*loc + *l, q.cos()));
|
||||||
},
|
},
|
||||||
Function::Tan => {
|
Function::Tan => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.tan())));
|
return Ok(Expression::Quantity(*loc + *l, q.tan()));
|
||||||
},
|
},
|
||||||
Function::Csc => {
|
Function::Csc => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.csc())));
|
return Ok(Expression::Quantity(*loc + *l, q.csc()));
|
||||||
},
|
},
|
||||||
Function::Sec => {
|
Function::Sec => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sec())));
|
return Ok(Expression::Quantity(*loc + *l, q.sec()));
|
||||||
},
|
},
|
||||||
Function::Cot => {
|
Function::Cot => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.cot())));
|
return Ok(Expression::Quantity(*loc + *l, q.cot()));
|
||||||
},
|
},
|
||||||
Function::Sinh => {
|
Function::Sinh => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sinh())));
|
return Ok(Expression::Quantity(*loc + *l, q.sinh()));
|
||||||
},
|
},
|
||||||
Function::Cosh => {
|
Function::Cosh => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.cosh())));
|
return Ok(Expression::Quantity(*loc + *l, q.cosh()));
|
||||||
},
|
},
|
||||||
Function::Tanh => {
|
Function::Tanh => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.tanh())));
|
return Ok(Expression::Quantity(*loc + *l, q.tanh()));
|
||||||
},
|
},
|
||||||
Function::Csch => {
|
Function::Csch => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.csch())));
|
return Ok(Expression::Quantity(*loc + *l, q.csch()));
|
||||||
},
|
},
|
||||||
Function::Sech => {
|
Function::Sech => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.sech())));
|
return Ok(Expression::Quantity(*loc + *l, q.sech()));
|
||||||
},
|
},
|
||||||
Function::Coth => {
|
Function::Coth => {
|
||||||
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.coth())));
|
return Ok(Expression::Quantity(*loc + *l, q.coth()));
|
||||||
},
|
},
|
||||||
Function::Asin => {
|
Function::Asin => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.asin())));
|
return Ok(Expression::Quantity(*loc + *l, q.asin()));
|
||||||
},
|
},
|
||||||
Function::Acos => {
|
Function::Acos => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.acos())));
|
return Ok(Expression::Quantity(*loc + *l, q.acos()));
|
||||||
},
|
},
|
||||||
Function::Atan => {
|
Function::Atan => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.atan())));
|
return Ok(Expression::Quantity(*loc + *l, q.atan()));
|
||||||
},
|
},
|
||||||
Function::Asinh => {
|
Function::Asinh => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.asinh())));
|
return Ok(Expression::Quantity(*loc + *l, q.asinh()));
|
||||||
},
|
},
|
||||||
Function::Acosh => {
|
Function::Acosh => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.acosh())));
|
return Ok(Expression::Quantity(*loc + *l, q.acosh()));
|
||||||
},
|
},
|
||||||
Function::Atanh => {
|
Function::Atanh => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, q.atanh())));
|
return Ok(Expression::Quantity(*loc + *l, q.atanh()));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
@ -159,7 +160,7 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
let mut r = q.without_unit();
|
let mut r = q.without_unit();
|
||||||
r += Quantity::new_rational(-273.15f64).unwrap();
|
r += Quantity::new_rational(-273.15f64).unwrap();
|
||||||
|
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
return Ok(Expression::Quantity(*loc + *l, r));
|
||||||
},
|
},
|
||||||
Function::ToFahrenheit => {
|
Function::ToFahrenheit => {
|
||||||
let mut k = Quantity::new_rational(1f64).unwrap();
|
let mut k = Quantity::new_rational(1f64).unwrap();
|
||||||
@ -171,7 +172,7 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
r += Quantity::new_rational(-459.67).unwrap();
|
r += Quantity::new_rational(-459.67).unwrap();
|
||||||
|
|
||||||
|
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
return Ok(Expression::Quantity(*loc + *l, r));
|
||||||
},
|
},
|
||||||
Function::FromCelsius => {
|
Function::FromCelsius => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
@ -180,7 +181,7 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
r += q.clone();
|
r += q.clone();
|
||||||
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
||||||
|
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
return Ok(Expression::Quantity(*loc + *l, r));
|
||||||
},
|
},
|
||||||
Function::FromFahrenheit => {
|
Function::FromFahrenheit => {
|
||||||
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
|
||||||
@ -190,7 +191,7 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
r *= Quantity::new_rational_from_frac(5i64, 9i64).unwrap();
|
r *= Quantity::new_rational_from_frac(5i64, 9i64).unwrap();
|
||||||
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
r.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
||||||
|
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
return Ok(Expression::Quantity(*loc + *l, r));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -75,26 +75,12 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
if let Expression::Quantity(la, a) = a {
|
if let Expression::Quantity(la, a) = a {
|
||||||
if let Expression::Quantity(lb, b) = b {
|
if let Expression::Quantity(lb, b) = b {
|
||||||
if !a.unit.compatible_with(&b.unit) {
|
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((
|
return Err((
|
||||||
*la + *lb + *op_loc,
|
*la + *lb + *op_loc,
|
||||||
DaisyError::IncompatibleUnits(a_s, b_s)
|
DaisyError::IncompatibleUnits(
|
||||||
|
a.convert_to_base().unit.display(context),
|
||||||
|
b.convert_to_base().unit.display(context)
|
||||||
|
)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone())));
|
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone())));
|
||||||
@ -112,24 +98,12 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
if let Expression::Quantity(la, a) = a {
|
if let Expression::Quantity(la, a) = a {
|
||||||
if let Expression::Quantity(lb, b) = b {
|
if let Expression::Quantity(lb, b) = b {
|
||||||
if !a.unit.compatible_with(&b.unit) {
|
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((
|
return Err((
|
||||||
*la + *lb + *op_loc,
|
*la + *lb + *op_loc,
|
||||||
DaisyError::IncompatibleUnits(a_s, b_s)
|
DaisyError::IncompatibleUnits(
|
||||||
|
a.convert_to_base().unit.display(context),
|
||||||
|
b.convert_to_base().unit.display(context)
|
||||||
|
)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() - b.clone())));
|
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() - b.clone())));
|
||||||
@ -164,8 +138,7 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
|
|
||||||
if let Expression::Quantity(la, a) = a {
|
if let Expression::Quantity(la, a) = a {
|
||||||
if let Expression::Quantity(lb, b) = b {
|
if let Expression::Quantity(lb, b) = b {
|
||||||
let o = a.clone() * b.clone();
|
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() * b.clone())));
|
||||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, o)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,11 +162,6 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, DaisyError::BadMath)); }
|
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)); }
|
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())));
|
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, va.clone() % vb.clone())));
|
||||||
} else { return Ok(None); }
|
} else { return Ok(None); }
|
||||||
} else { return Ok(None); }
|
} else { return Ok(None); }
|
||||||
@ -208,26 +176,12 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
if let Expression::Quantity(lb, vb) = b {
|
if let Expression::Quantity(lb, vb) = b {
|
||||||
let n = va.clone().convert_to(vb.clone());
|
let n = va.clone().convert_to(vb.clone());
|
||||||
if n.is_none() {
|
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((
|
return Err((
|
||||||
*la + *lb + *op_loc,
|
*la + *lb + *op_loc,
|
||||||
DaisyError::IncompatibleUnits(a_s, b_s)
|
DaisyError::IncompatibleUnits(
|
||||||
|
va.convert_to_base().unit.display(context),
|
||||||
|
vb.convert_to_base().unit.display(context)
|
||||||
|
)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, n.unwrap())));
|
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, n.unwrap())));
|
||||||
|
@ -22,8 +22,7 @@ impl ToString for FormattedText {
|
|||||||
fn format_map_none(c: char) -> Option<String> {
|
fn format_map_none(c: char) -> Option<String> {
|
||||||
Some(match c {
|
Some(match c {
|
||||||
'n'|'i'|'t'|'a'|
|
'n'|'i'|'t'|'a'|
|
||||||
'e'|'c'|'s'|'r'|
|
'e'|'c'|'s'|'r'
|
||||||
'p'
|
|
||||||
=> { "".to_string() },
|
=> { "".to_string() },
|
||||||
_ => { return None }
|
_ => { return None }
|
||||||
})
|
})
|
||||||
@ -38,39 +37,34 @@ fn format_map_ansi(c: char) -> Option<String> {
|
|||||||
'i' => { // Normal italic text
|
'i' => { // Normal italic text
|
||||||
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
||||||
},
|
},
|
||||||
't' => { // Title text (should be cyan)
|
't' => { // Title text
|
||||||
format!("{}{}", color::Fg(color::AnsiValue(6)), color::Bg(color::Reset))
|
format!("{}{}", color::Fg(color::AnsiValue(6)), color::Bg(color::Reset))
|
||||||
},
|
},
|
||||||
'a' => { // Colored text (should be pink)
|
'a' => { // Colored text
|
||||||
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
||||||
},
|
},
|
||||||
'e' => { // Error titles (should be red)
|
'e' => { // Error titles
|
||||||
format!("{}{}", color::Fg(color::AnsiValue(1)), color::Bg(color::Reset))
|
format!("{}{}", color::Fg(color::AnsiValue(1)), color::Bg(color::Reset))
|
||||||
},
|
},
|
||||||
'c' => { // Console text (inverted black on white)
|
'c' => { // Console text
|
||||||
format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7)))
|
format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7)))
|
||||||
},
|
},
|
||||||
'p' => { // Input prompt (how ==> is styled) (should be blue)
|
's' => { // Repeat prompt (how => is styled)
|
||||||
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))
|
format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset))
|
||||||
},
|
},
|
||||||
|
'r' => { // Result prompt (how = is styled)
|
||||||
|
format!("{}{}", color::Fg(color::AnsiValue(4)), color::Bg(color::Reset))
|
||||||
|
},
|
||||||
|
|
||||||
_ => { return None }
|
_ => { return None }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// style::reset also resets color.
|
|
||||||
// Make sure color comes AFTER style reset.
|
|
||||||
fn format_map_full(c: char) -> Option<String> {
|
fn format_map_full(c: char) -> Option<String> {
|
||||||
Some(match c {
|
Some(match c {
|
||||||
'n' => { // Normal text
|
'n' => { // Normal text
|
||||||
format!("{}{}", style::Reset, color::Fg(color::Reset))
|
format!("{}{}", color::Fg(color::Reset), style::Reset)
|
||||||
},
|
},
|
||||||
'i' => { // Normal italic text
|
'i' => { // Normal italic text
|
||||||
format!("{}{}", color::Fg(color::Reset), style::Italic)
|
format!("{}{}", color::Fg(color::Reset), style::Italic)
|
||||||
@ -79,7 +73,7 @@ fn format_map_full(c: char) -> Option<String> {
|
|||||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||||
},
|
},
|
||||||
'a' => { // Colored text
|
'a' => { // Colored text
|
||||||
format!("{}{}", style::Reset, color::Fg(color::Magenta))
|
format!("{}{}", color::Fg(color::Magenta), style::Reset)
|
||||||
},
|
},
|
||||||
'e' => { // Error titles
|
'e' => { // Error titles
|
||||||
format!("{}{}", color::Fg(color::Red), style::Bold)
|
format!("{}{}", color::Fg(color::Red), style::Bold)
|
||||||
@ -87,9 +81,6 @@ fn format_map_full(c: char) -> Option<String> {
|
|||||||
'c' => { // Console text
|
'c' => { // Console text
|
||||||
format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
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)
|
's' => { // Repeat prompt (how => is styled)
|
||||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||||
},
|
},
|
||||||
@ -97,24 +88,17 @@ fn format_map_full(c: char) -> Option<String> {
|
|||||||
format!("{}{}", color::Fg(color::Green), style::Bold)
|
format!("{}{}", color::Fg(color::Green), style::Bold)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
_ => { return None }
|
_ => { 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 {
|
impl FormattedText {
|
||||||
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||||
write!(stdout, "\n")?;
|
write!(
|
||||||
|
stdout,
|
||||||
|
"\r\n",
|
||||||
|
)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,7 +145,12 @@ impl FormattedText {
|
|||||||
match (a, b) {
|
match (a, b) {
|
||||||
(c, ']') => { // Normal text
|
(c, ']') => { // Normal text
|
||||||
|
|
||||||
let q = format_map(c, context);
|
let q = 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")
|
||||||
|
};
|
||||||
|
|
||||||
if q.is_some() {
|
if q.is_some() {
|
||||||
s.push_str(&q.unwrap());
|
s.push_str(&q.unwrap());
|
||||||
@ -184,7 +173,7 @@ impl FormattedText {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(stdout, "\r{}", s)?;
|
write!(stdout, "{}", s)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,23 +11,6 @@ use super::super::LineLocation;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum Expression {
|
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),
|
Variable(LineLocation, String),
|
||||||
Quantity(LineLocation, Quantity),
|
Quantity(LineLocation, Quantity),
|
||||||
Constant(LineLocation, Constant),
|
Constant(LineLocation, Constant),
|
||||||
|
@ -21,10 +21,8 @@ pub fn parse(
|
|||||||
context: &Context, s: &String
|
context: &Context, s: &String
|
||||||
) -> Result<Expression, (LineLocation, DaisyError)> {
|
) -> Result<Expression, (LineLocation, DaisyError)> {
|
||||||
|
|
||||||
let mut expressions = stage::tokenize(context, s);
|
let expressions = stage::tokenize(context, s);
|
||||||
if context.config.enable_substituion {
|
let (_, expressions) = stage::find_subs(expressions);
|
||||||
(_, expressions) = stage::find_subs(expressions);
|
|
||||||
}
|
|
||||||
let g = stage::groupify(context, expressions)?;
|
let g = stage::groupify(context, expressions)?;
|
||||||
let g = stage::treeify(context, g)?;
|
let g = stage::treeify(context, g)?;
|
||||||
|
|
||||||
@ -35,14 +33,7 @@ pub fn parse_no_context(s: &String) -> Result<Expression, (LineLocation, DaisyEr
|
|||||||
parse(&Context::new(), s)
|
parse(&Context::new(), s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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 {
|
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());
|
let (_, s) = substitute_cursor(context, s, s.chars().count());
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
@ -52,45 +43,34 @@ pub fn substitute_cursor(
|
|||||||
s: &String, // The string to substitute
|
s: &String, // The string to substitute
|
||||||
c: usize // Location of the cursor right now
|
c: usize // Location of the cursor right now
|
||||||
) -> (
|
) -> (
|
||||||
usize, // New cursor
|
usize, // Location of cursor in substituted string
|
||||||
String // String with substitutions
|
String // String with substitutions
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if !context.config.enable_substituion { return (c, s.clone()); }
|
|
||||||
if s == "" { return (c, s.clone()) }
|
if s == "" { return (c, s.clone()) }
|
||||||
|
|
||||||
|
|
||||||
let mut new_s = s.clone();
|
let mut new_s = s.clone();
|
||||||
|
|
||||||
|
let l = s.chars().count();
|
||||||
let expressions = stage::tokenize(context, s);
|
let expressions = stage::tokenize(context, s);
|
||||||
let (mut subs, _) = stage::find_subs(expressions);
|
let (mut subs, _) = stage::find_subs(expressions);
|
||||||
let mut new_c = c.clone();
|
let mut new_c = l - c;
|
||||||
|
|
||||||
while subs.len() > 0 {
|
while subs.len() > 0 {
|
||||||
// Apply substitutions in reverse order
|
|
||||||
// r is the current substitution: (linelocation, string)
|
|
||||||
let r = subs.pop_back().unwrap();
|
let r = subs.pop_back().unwrap();
|
||||||
|
// Apply substitutions in reverse order
|
||||||
|
|
||||||
if { // Don't substitute if our cursor is inside the substitution
|
if { // Don't substitute if our cursor is inside the substitution
|
||||||
c >= r.0.pos &&
|
c >= r.0.pos &&
|
||||||
c < r.0.pos+r.0.len
|
c < r.0.pos+r.0.len
|
||||||
} { continue; }
|
} { continue; }
|
||||||
|
|
||||||
// If this substitution is before our cursor,
|
if c < r.0.pos {
|
||||||
// we need to adjust our cursor's position.
|
let ct = r.1.chars().count();
|
||||||
if c > r.0.pos {
|
if ct >= r.0.len {
|
||||||
let c_o = r.0.len; // Old length
|
if new_c >= ct - r.0.len {
|
||||||
let c_n = r.1.chars().count(); // New length
|
new_c += ct - r.0.len
|
||||||
|
}
|
||||||
if c_n > c_o {
|
} else {
|
||||||
// Move cursor right by difference
|
new_c -= r.0.len - ct
|
||||||
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; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,69 +6,11 @@ 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(
|
pub fn find_subs(
|
||||||
mut g: VecDeque<Token>,
|
mut g: VecDeque<Token>,
|
||||||
) -> (
|
) -> (
|
||||||
VecDeque<(LineLocation, String)>, // List of substrings to replace (in order)
|
VecDeque<(LineLocation, String)>,
|
||||||
VecDeque<Token> // New token array, with updated strings and linelocations
|
VecDeque<Token>
|
||||||
) {
|
) {
|
||||||
|
|
||||||
// Array of replacements
|
// Array of replacements
|
||||||
@ -82,19 +24,62 @@ pub fn find_subs(
|
|||||||
while g.len() > 0 {
|
while g.len() > 0 {
|
||||||
let mut t = g.pop_front().unwrap();
|
let mut t = g.pop_front().unwrap();
|
||||||
|
|
||||||
|
|
||||||
let target: Option<&str> = match &mut t {
|
let target: Option<&str> = match &mut t {
|
||||||
Token::Operator(_, s) => {
|
Token::Operator(_, s) => {
|
||||||
let target = sub_string(s);
|
let target = match &s[..] {
|
||||||
|
"*" => {Some("×")},
|
||||||
|
"/" => {Some("÷")},
|
||||||
|
"sqrt" => {Some("√")},
|
||||||
|
"rt" => {Some("√")},
|
||||||
|
_ => {None}
|
||||||
|
};
|
||||||
|
|
||||||
// Update token contents too.
|
// Update token contents too.
|
||||||
// This makes errors and printouts use the updated string.
|
// This makes sure that errors also contain the updated text.
|
||||||
if target.is_some() { *s = String::from(target.unwrap()); }
|
if target.is_some() { *s = String::from(target.unwrap()); }
|
||||||
target
|
target
|
||||||
},
|
},
|
||||||
|
|
||||||
Token::Word(_, s) => {
|
Token::Word(_, s) => {
|
||||||
let target = sub_string(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}
|
||||||
|
};
|
||||||
|
|
||||||
if target.is_some() { *s = String::from(target.unwrap()); }
|
if target.is_some() { *s = String::from(target.unwrap()); }
|
||||||
target
|
target
|
||||||
},
|
},
|
||||||
@ -103,7 +88,7 @@ pub fn find_subs(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if target.is_none() {
|
if target.is_none() {
|
||||||
// Even if nothing changed, we need to update the new token's linelocation
|
// Even if nothing changed, we need to update token location
|
||||||
let l = t.get_mut_linelocation();
|
let l = t.get_mut_linelocation();
|
||||||
*l = LineLocation{pos: l.pos - offset, len: l.len};
|
*l = LineLocation{pos: l.pos - offset, len: l.len};
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,7 +8,6 @@ use std::ops::{
|
|||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use super::ScalarBase;
|
use super::ScalarBase;
|
||||||
use super::dec_to_sci;
|
|
||||||
|
|
||||||
|
|
||||||
macro_rules! foward {
|
macro_rules! foward {
|
||||||
@ -26,39 +25,16 @@ pub struct F64Base where {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for F64Base {
|
impl ToString for F64Base {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String { self.val.to_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 {
|
impl ScalarBase for F64Base {
|
||||||
|
|
||||||
|
fn from_f64(f: f64) -> Option<F64Base> {
|
||||||
|
return Some(F64Base{ val: f });
|
||||||
|
}
|
||||||
|
|
||||||
fn from_string(s: &str) -> Option<F64Base> {
|
fn from_string(s: &str) -> Option<F64Base> {
|
||||||
let v = s.parse::<f64>();
|
let v = s.parse::<f64>();
|
||||||
let v = match v {
|
let v = match v {
|
||||||
@ -75,7 +51,6 @@ impl ScalarBase for F64Base {
|
|||||||
fn is_one(&self) -> bool {self.val == 1f64}
|
fn is_one(&self) -> bool {self.val == 1f64}
|
||||||
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
|
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
|
||||||
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
|
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
|
||||||
fn is_int(&self) -> bool { self.val.floor() == self.val }
|
|
||||||
|
|
||||||
foward!(abs);
|
foward!(abs);
|
||||||
foward!(floor);
|
foward!(floor);
|
||||||
@ -85,11 +60,9 @@ impl ScalarBase for F64Base {
|
|||||||
foward!(sin);
|
foward!(sin);
|
||||||
foward!(cos);
|
foward!(cos);
|
||||||
foward!(tan);
|
foward!(tan);
|
||||||
|
foward!(csc);
|
||||||
fn csc(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.sin() }) }
|
foward!(sec);
|
||||||
fn sec(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cos() }) }
|
foward!(cot);
|
||||||
fn cot(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.tan() }) }
|
|
||||||
|
|
||||||
foward!(asin);
|
foward!(asin);
|
||||||
foward!(acos);
|
foward!(acos);
|
||||||
foward!(atan);
|
foward!(atan);
|
||||||
@ -97,11 +70,9 @@ impl ScalarBase for F64Base {
|
|||||||
foward!(sinh);
|
foward!(sinh);
|
||||||
foward!(cosh);
|
foward!(cosh);
|
||||||
foward!(tanh);
|
foward!(tanh);
|
||||||
|
foward!(csch);
|
||||||
fn csch(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.sinh() }) }
|
foward!(sech);
|
||||||
fn sech(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cosh() }) }
|
foward!(coth);
|
||||||
fn coth(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.tanh() }) }
|
|
||||||
|
|
||||||
foward!(asinh);
|
foward!(asinh);
|
||||||
foward!(acosh);
|
foward!(acosh);
|
||||||
foward!(atanh);
|
foward!(atanh);
|
||||||
@ -190,11 +161,11 @@ impl Rem<F64Base> for F64Base {
|
|||||||
|
|
||||||
fn rem(self, modulus: F64Base) -> Self::Output {
|
fn rem(self, modulus: F64Base) -> Self::Output {
|
||||||
if {
|
if {
|
||||||
(!self.is_int()) ||
|
(!self.fract().unwrap().is_zero()) ||
|
||||||
(!modulus.is_int())
|
(!modulus.fract().unwrap().is_zero())
|
||||||
} { panic!() }
|
} { panic!() }
|
||||||
|
|
||||||
F64Base{val : self.val.round() % modulus.val.round()}
|
F64Base{val : self.val.fract() % modulus.val.fract()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use bigdecimal::BigDecimal;
|
use rug::Float;
|
||||||
use bigdecimal::Zero;
|
use rug::Assign;
|
||||||
use bigdecimal::RoundingMode;
|
use rug::ops::AssignRound;
|
||||||
use std::str::FromStr;
|
use rug::ops::Pow;
|
||||||
|
|
||||||
use std::ops::{
|
use std::ops::{
|
||||||
Add, Sub, Mul, Div,
|
Add, Sub, Mul, Div,
|
||||||
@ -14,151 +14,158 @@ use std::ops::{
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use super::ScalarBase;
|
use super::ScalarBase;
|
||||||
use super::dec_to_sci;
|
use super::PRINT_LEN;
|
||||||
|
use super::FLOAT_PRECISION;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct FloatBase where {
|
pub struct FloatBase where {
|
||||||
pub val: BigDecimal
|
pub val: Float
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FloatBase {
|
impl FloatBase {
|
||||||
pub fn new(s: &str) -> FloatBase {
|
pub fn from<T>(a: T) -> Option<FloatBase> where
|
||||||
return FloatBase {
|
Float: Assign<T> + AssignRound<T>
|
||||||
val: s.parse().unwrap()
|
{
|
||||||
};
|
let v = Float::with_val(FLOAT_PRECISION, a);
|
||||||
|
return Some(FloatBase{ val: v });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl ToString for FloatBase {
|
impl ToString for FloatBase {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
|
let (sign, mut string, exp) = self.val.to_sign_string_exp(10, Some(PRINT_LEN));
|
||||||
|
|
||||||
if self.val.is_nan() {
|
// zero, nan, or inf.
|
||||||
return "NaN".to_string();
|
let sign = if sign {"-"} else {""};
|
||||||
} else if self.val.is_inf_neg() {
|
if exp.is_none() { return format!("{sign}{string}"); }
|
||||||
return "-Inf".to_string();
|
let exp = exp.unwrap();
|
||||||
} else if self.val.is_inf_pos() {
|
|
||||||
return "+Inf".to_string();
|
// Remove trailing zeros.
|
||||||
|
// At this point, string is guaranteed to be nonzero.
|
||||||
|
while string.chars().last().unwrap() == '0' {
|
||||||
|
string.remove(string.len() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let exp_u: usize;
|
||||||
|
|
||||||
// Already in scientific notation,we just need to trim significant digits.
|
if exp < 0 {
|
||||||
let mut _a = self.val.round(32, astro_float::RoundingMode::Up).to_string();
|
exp_u = (-exp).try_into().unwrap()
|
||||||
let mut _b = _a.split('e');
|
} else {
|
||||||
|
exp_u = exp.try_into().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
let mut s = String::from(_b.next().unwrap()); // Decimal
|
if exp_u >= PRINT_LEN {
|
||||||
let p: i64 = _b.next().unwrap().parse().unwrap(); // Exponent
|
// Exponential notation
|
||||||
|
let pre = &string[0..1];
|
||||||
|
let post = &string[1..];
|
||||||
|
|
||||||
// Remove negative sign from string
|
format!(
|
||||||
let neg = s.starts_with("-");
|
"{pre}{}{post}e{}",
|
||||||
if neg { s = String::from(&s[1..]); }
|
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())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
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> {
|
fn from_string(s: &str) -> Option<FloatBase> {
|
||||||
let v = BigDecimal::from_str(s);
|
let v = Float::parse(s);
|
||||||
let v = match v {
|
let v = match v {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => return None
|
Err(_) => return None
|
||||||
};
|
};
|
||||||
|
|
||||||
return Some(FloatBase{ val: v });
|
return Some(
|
||||||
|
FloatBase{ val:
|
||||||
|
Float::with_val(FLOAT_PRECISION, v)
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//foward!(fract);
|
foward!(fract);
|
||||||
|
|
||||||
fn is_zero(&self) -> bool {self.val.is_zero()}
|
fn is_zero(&self) -> bool {self.val.is_zero()}
|
||||||
fn is_one(&self) -> bool {self.val == BigDecimal::from_str("1").unwrap()}
|
fn is_one(&self) -> bool {self.val == Float::with_val(FLOAT_PRECISION, 1)}
|
||||||
fn is_negative(&self) -> bool { self.val.sign() == num::bigint::Sign::Minus }
|
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
|
||||||
fn is_positive(&self) -> bool { self.val.sign() == num::bigint::Sign::Plus }
|
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
|
||||||
|
|
||||||
fn is_int(&self) -> bool { self.val.is_integer() }
|
fn is_int(&self) -> bool {
|
||||||
|
self.fract() == FloatBase::from_f64(0f64)
|
||||||
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) })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ceil(&self) -> Option<FloatBase> {
|
foward!(abs);
|
||||||
let (_, scale) = self.val.as_bigint_and_exponent();
|
foward!(floor);
|
||||||
Some(FloatBase{ val: self.val.with_scale_round(scale, RoundingMode::Up) })
|
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 fract(&self) -> Option<FloatBase> { Some(self.clone() - self.floor().unwrap()) }
|
fn pow(&self, base: FloatBase) -> Option<FloatBase> {
|
||||||
|
Some(FloatBase{ val: self.val.clone().pow(base.val)})
|
||||||
|
|
||||||
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() })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -216,7 +223,7 @@ impl Div for FloatBase {
|
|||||||
|
|
||||||
impl DivAssign for FloatBase where {
|
impl DivAssign for FloatBase where {
|
||||||
fn div_assign(&mut self, other: Self) {
|
fn div_assign(&mut self, other: Self) {
|
||||||
self.val = self.val.clone() / other.val;
|
self.val /= other.val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,11 +240,11 @@ impl Rem<FloatBase> for FloatBase {
|
|||||||
|
|
||||||
fn rem(self, modulus: FloatBase) -> Self::Output {
|
fn rem(self, modulus: FloatBase) -> Self::Output {
|
||||||
if {
|
if {
|
||||||
(!self.is_int()) ||
|
(!self.fract().unwrap().is_zero()) ||
|
||||||
(!modulus.is_int())
|
(!modulus.fract().unwrap().is_zero())
|
||||||
} { panic!() }
|
} { panic!() }
|
||||||
|
|
||||||
FloatBase{val : self.val.round(0) % modulus.val.round(0)}
|
FloatBase{val : self.val.fract() % modulus.val.fract()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,92 +1,10 @@
|
|||||||
//const FLOAT_PRECISION: u32 = 1024;
|
const FLOAT_PRECISION: u32 = 1024;
|
||||||
const SHOW_SIG: usize = 5; // How many significant digits we will show in output
|
const PRINT_LEN: 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 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;
|
mod scalar;
|
||||||
pub use self::scalar::Scalar;
|
pub use self::scalar::Scalar;
|
||||||
pub use self::scalar::ScalarBase;
|
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}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,5 @@
|
|||||||
use num::rational::BigRational;
|
use rug::Rational;
|
||||||
use num::BigInt;
|
use rug::Integer;
|
||||||
use num::Num;
|
|
||||||
use num::Signed;
|
|
||||||
|
|
||||||
use std::ops::{
|
use std::ops::{
|
||||||
Add, Sub, Mul, Div,
|
Add, Sub, Mul, Div,
|
||||||
@ -24,7 +22,7 @@ macro_rules! cant_do {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RationalBase where {
|
pub struct RationalBase where {
|
||||||
pub val: BigRational
|
pub val: Rational
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for RationalBase{
|
impl ToString for RationalBase{
|
||||||
@ -35,12 +33,18 @@ impl ToString for RationalBase{
|
|||||||
|
|
||||||
impl RationalBase {
|
impl RationalBase {
|
||||||
pub fn from_frac(t: i64, b: i64) -> Option<RationalBase> {
|
pub fn from_frac(t: i64, b: i64) -> Option<RationalBase> {
|
||||||
let v = BigRational::new_raw(BigInt::from(t), BigInt::from(b));
|
let v = Rational::from((t, b));
|
||||||
return Some(RationalBase{ val: v });
|
return Some(RationalBase{ val: v });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScalarBase for RationalBase {
|
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> {
|
fn from_string(s: &str) -> Option<RationalBase> {
|
||||||
// Scientific notation
|
// Scientific notation
|
||||||
let mut sci = s.split("e");
|
let mut sci = s.split("e");
|
||||||
@ -85,7 +89,7 @@ impl ScalarBase for RationalBase {
|
|||||||
|
|
||||||
|
|
||||||
// From fraction string
|
// From fraction string
|
||||||
let r = BigRational::from_str_radix(&s, 10);
|
let r = Rational::from_str_radix(&s, 10);
|
||||||
let r = match r {
|
let r = match r {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => return None
|
Err(_) => return None
|
||||||
@ -96,13 +100,18 @@ impl ScalarBase for RationalBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn fract(&self) -> Option<RationalBase> { Some(RationalBase{val: self.val.fract()}) }
|
fn fract(&self) -> Option<RationalBase> {
|
||||||
fn is_int(&self) -> bool { self.val.is_integer() }
|
Some(RationalBase{val: self.val.clone().fract_floor(Integer::new()).0})
|
||||||
|
}
|
||||||
|
|
||||||
fn is_zero(&self) -> bool {self.val == BigRational::from_integer(BigInt::from(0))}
|
fn is_int(&self) -> bool {
|
||||||
fn is_one(&self) -> bool {self.val == BigRational::from_integer(BigInt::from(1))}
|
self.fract() == RationalBase::from_f64(0f64)
|
||||||
fn is_negative(&self) -> bool { self.val.is_negative() }
|
}
|
||||||
fn is_positive(&self) -> bool { self.val.is_positive() }
|
|
||||||
|
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 abs(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().abs()})}
|
fn abs(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().abs()})}
|
||||||
fn floor(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().floor()})}
|
fn floor(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().floor()})}
|
||||||
@ -144,7 +153,9 @@ impl Add for RationalBase where {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self::Output {
|
fn add(self, other: Self) -> Self::Output {
|
||||||
Self { val: self.val + other.val }
|
Self {
|
||||||
|
val: self.val + other.val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +169,9 @@ impl Sub for RationalBase {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn sub(self, other: Self) -> Self::Output {
|
fn sub(self, other: Self) -> Self::Output {
|
||||||
Self { val: self.val - other.val }
|
Self {
|
||||||
|
val: self.val - other.val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +185,9 @@ impl Mul for RationalBase {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn mul(self, other: Self) -> Self::Output {
|
fn mul(self, other: Self) -> Self::Output {
|
||||||
Self { val: self.val * other.val }
|
Self {
|
||||||
|
val: self.val * other.val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,7 +201,9 @@ impl Div for RationalBase {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn div(self, other: Self) -> Self::Output {
|
fn div(self, other: Self) -> Self::Output {
|
||||||
Self { val: self.val / other.val }
|
Self {
|
||||||
|
val: self.val / other.val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,7 +217,9 @@ impl Neg for RationalBase where {
|
|||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn neg(self) -> Self::Output {
|
fn neg(self) -> Self::Output {
|
||||||
Self { val: -self.val }
|
Self {
|
||||||
|
val: -self.val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,15 +228,15 @@ impl Rem<RationalBase> for RationalBase {
|
|||||||
|
|
||||||
fn rem(self, modulus: RationalBase) -> Self::Output {
|
fn rem(self, modulus: RationalBase) -> Self::Output {
|
||||||
if {
|
if {
|
||||||
*self.val.denom() != BigInt::from(1) ||
|
*self.val.denom() != 1 ||
|
||||||
*modulus.val.denom() != BigInt::from(1)
|
*modulus.val.denom() != 1
|
||||||
} { panic!() }
|
} { panic!() }
|
||||||
|
|
||||||
RationalBase{
|
RationalBase{
|
||||||
val : BigRational::new_raw(
|
val : Rational::from((
|
||||||
self.val.numer() % modulus.val.numer(),
|
self.val.numer() % modulus.val.numer(),
|
||||||
BigInt::from(1)
|
1
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use std::ops::{
|
|||||||
};
|
};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use super::FloatBase as FloatBase;
|
use super::floatbase::FloatBase as FloatBase;
|
||||||
use super::rationalbase::RationalBase;
|
use super::rationalbase::RationalBase;
|
||||||
|
|
||||||
|
|
||||||
@ -21,6 +21,7 @@ pub trait ScalarBase:
|
|||||||
PartialEq + PartialOrd
|
PartialEq + PartialOrd
|
||||||
{
|
{
|
||||||
// Creation
|
// Creation
|
||||||
|
fn from_f64(f: f64) -> Option<Self>;
|
||||||
fn from_string(s: &str) -> Option<Self>;
|
fn from_string(s: &str) -> Option<Self>;
|
||||||
|
|
||||||
// Utility
|
// Utility
|
||||||
@ -86,8 +87,8 @@ fn to_float(r: Scalar) -> Scalar {
|
|||||||
match &r {
|
match &r {
|
||||||
Scalar::Float {..} => r,
|
Scalar::Float {..} => r,
|
||||||
Scalar::Rational {v} => wrap_float!(
|
Scalar::Rational {v} => wrap_float!(
|
||||||
FloatBase::from_string(&v.val.numer().to_string()).unwrap() /
|
FloatBase::from(v.val.numer()).unwrap() /
|
||||||
FloatBase::from_string(&v.val.denom().to_string()).unwrap()
|
FloatBase::from(v.val.denom()).unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,13 +105,13 @@ impl ToString for Scalar {
|
|||||||
// Creation methods
|
// Creation methods
|
||||||
impl Scalar {
|
impl Scalar {
|
||||||
pub fn new_float(f: f64) -> Option<Self> {
|
pub fn new_float(f: f64) -> Option<Self> {
|
||||||
let v = FloatBase::from_string(&f.to_string());
|
let v = FloatBase::from_f64(f);
|
||||||
if v.is_none() { return None; }
|
if v.is_none() { return None; }
|
||||||
return Some(wrap_float!(v.unwrap()));
|
return Some(wrap_float!(v.unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_rational(f: f64) -> Option<Self> {
|
pub fn new_rational(f: f64) -> Option<Self> {
|
||||||
let r = RationalBase::from_string(&f.to_string());
|
let r = RationalBase::from_f64(f);
|
||||||
if r.is_none() { return None; }
|
if r.is_none() { return None; }
|
||||||
return Some(wrap_rational!(r.unwrap()));
|
return Some(wrap_rational!(r.unwrap()));
|
||||||
}
|
}
|
||||||
@ -184,8 +185,8 @@ impl Scalar {
|
|||||||
|
|
||||||
pub fn is_nan(&self) -> bool {
|
pub fn is_nan(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Scalar::Float{ v } => {v.val.is_nan()},
|
Scalar::Float {v} => {v.val.is_nan()},
|
||||||
Scalar::Rational {..} => {false}
|
Scalar::Rational {..} => {panic!()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,6 +275,7 @@ impl Unit {
|
|||||||
let mut q = Quantity::new_rational(1f64).unwrap();
|
let mut q = Quantity::new_rational(1f64).unwrap();
|
||||||
q.set_unit(b);
|
q.set_unit(b);
|
||||||
return Some(q);
|
return Some(q);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ fn operators() {
|
|||||||
|
|
||||||
good_expr("125", "5^(+3)");
|
good_expr("125", "5^(+3)");
|
||||||
good_expr("125", "+5^3");
|
good_expr("125", "+5^3");
|
||||||
good_expr("0.21479", "3 ^ (-1.4)");
|
good_expr("0.2148", "3 ^ (-1.4)");
|
||||||
|
|
||||||
// Should parse as ((2^3)^4)^5
|
// Should parse as ((2^3)^4)^5
|
||||||
good_expr("1.1529e18", "2^3^4^5");
|
good_expr("1.1529e18", "2^3^4^5");
|
||||||
@ -162,7 +162,6 @@ fn operators() {
|
|||||||
|
|
||||||
good_expr("2", "6/3");
|
good_expr("2", "6/3");
|
||||||
good_expr("2", "5%3");
|
good_expr("2", "5%3");
|
||||||
good_expr("4", "2^5 mod 7");
|
|
||||||
good_expr("8", "5+3");
|
good_expr("8", "5+3");
|
||||||
good_expr("64", "4^3");
|
good_expr("64", "4^3");
|
||||||
good_expr("64", "4 ^ 3");
|
good_expr("64", "4 ^ 3");
|
||||||
@ -185,7 +184,6 @@ fn operators() {
|
|||||||
bad_expr("1e5!");
|
bad_expr("1e5!");
|
||||||
bad_expr("0^(-1)");
|
bad_expr("0^(-1)");
|
||||||
bad_expr("pi!");
|
bad_expr("pi!");
|
||||||
bad_expr("2.5 mod 8");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user