Compare commits
58 Commits
Author | SHA1 | Date | |
---|---|---|---|
228cbf281f | |||
18a7d592ef | |||
b8bde580a3 | |||
819e444378 | |||
1fae11a380 | |||
bc1716eefd | |||
42555e0df7 | |||
cd1074d30f | |||
937b45e3b0 | |||
7726eea34d | |||
6fd315a9fe | |||
0d81d58370 | |||
2ff42bf62a | |||
b5a23f814f | |||
e3e23a686d | |||
93c34ca4a4 | |||
80f17ac210 | |||
3c8ed9f4f3 | |||
27ea64e172 | |||
b7a6378589 | |||
d6f1ce0442 | |||
5e34017225 | |||
d9730fd699 | |||
5788ffa085 | |||
a75f6197cd | |||
2f2c005c86 | |||
ffebcf2e15 | |||
4e1df2399b | |||
![]() |
5a921cfc7b | ||
2531524ffd | |||
e5525adcca | |||
a61cbf29e5 | |||
39ab47530e | |||
be28669416 | |||
7658ff76ef | |||
e9d392b80b | |||
63d4a01095 | |||
6a025cbf1a | |||
8a6c623ef2 | |||
a1db2b6bb0 | |||
c2d3e613d4 | |||
96a34dc05d | |||
42fdcd5853 | |||
cffb3726cc | |||
ef74b67f90 | |||
32463ad6aa | |||
413c34c440 | |||
a1d190b06e | |||
178708a4ac | |||
5bb8e2c4ce | |||
49b88af2bb | |||
a0fe0a9385 | |||
7215afcc7e | |||
3ab559b240 | |||
810e2a1267 | |||
798c9ceae9 | |||
bab305e11d | |||
a6c3ffa68d |
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
/target
|
/target
|
||||||
/src/target
|
/src/target
|
||||||
/pkg
|
/pkg
|
||||||
|
/result
|
32
CONTRIBUTING.MD
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Contribution guidelines for Daisy
|
||||||
|
|
||||||
|
|
||||||
|
## Pull requests
|
||||||
|
All PRs should merge to `dev`. See "branching" below.
|
||||||
|
|
||||||
|
|
||||||
|
## Versioning
|
||||||
|
Daisy uses [semantic versioning](https://semver.org/), which looks like `maj.min.patch`. In short:
|
||||||
|
- `maj`: Incremented on large, incompatible changes
|
||||||
|
- `min`: Incremented on new features
|
||||||
|
- `patch`: Incremented on bugfixes
|
||||||
|
|
||||||
|
Whenever one of the above fields is incremented, all sub-fields are reset to zero. When `min` is incremented, `patch` is set to zero.
|
||||||
|
|
||||||
|
|
||||||
|
## Branching
|
||||||
|
Daisy uses a branch model much like the one described [here](https://nvie.com/posts/a-successful-git-branching-model).
|
||||||
|
- `master`: 1 commit = 1 release. This is always a merge comit from `dev`, and is always tagged with a version.
|
||||||
|
- `dev`: main development branch. Never deleted. Most work happens here.
|
||||||
|
- `features`: temporary branches pulled from `dev` and merged to `dev` for larger features.
|
||||||
|
- Always merge with git's `--no-ff` option for a more detailed history.
|
||||||
|
|
||||||
|
|
||||||
|
## Releases
|
||||||
|
What to do
|
||||||
|
- Merge into `master` (`git merge --no-ff dev`)
|
||||||
|
- Tag merge commit on `master` (`git tag -a v1.0.0 -m "Version 1.0.0"`)
|
||||||
|
- `cargo publish`
|
||||||
|
- Update web demo & pull server (`make docker`)
|
||||||
|
- Update aur package
|
||||||
|
- Update `default.nix` (test with `make nix`)
|
2
Cargo.lock
generated
@ -28,7 +28,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "daisycalc"
|
name = "daisycalc"
|
||||||
version = "1.1.1"
|
version = "1.1.7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"num",
|
"num",
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "daisycalc"
|
name = "daisycalc"
|
||||||
version = "1.1.1"
|
version = "1.1.7"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
build = "buildscript/main.rs"
|
build = "buildscript/main.rs"
|
||||||
license = "GPL-3.0-only"
|
license = "GPL-3.0-only"
|
||||||
description = "A high-precision terminal scientific calculator."
|
description = "A pretty TUI scientific calculator."
|
||||||
repository = "https://git.betalupi.com/Mark/daisy"
|
repository = "https://git.betalupi.com/Mark/daisy"
|
||||||
homepage = "https://git.betalupi.com/Mark/daisy"
|
homepage = "https://git.betalupi.com/Mark/daisy"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
22
Makefile
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
release:
|
||||||
|
cargo build --release
|
||||||
|
|
||||||
|
nix:
|
||||||
|
nix-build -E 'let pkgs = import <nixpkgs> { }; in pkgs.callPackage ./default.nix {}'
|
||||||
|
|
||||||
|
test:
|
||||||
|
cargo test
|
||||||
|
|
||||||
|
run:
|
||||||
|
cargo run
|
||||||
|
|
||||||
|
wasm:
|
||||||
|
wasm-pack build --release --target web --out-dir server/pkg
|
||||||
|
|
||||||
|
publish:
|
||||||
|
cargo test
|
||||||
|
cargo publish
|
||||||
|
|
||||||
|
docker: wasm
|
||||||
|
docker build ./server -t git.betalupi.com/mark/daisy --no-cache
|
||||||
|
docker push git.betalupi.com/mark/daisy
|
32
README.md
@ -1,19 +1,31 @@
|
|||||||

|

|
||||||
|
|
||||||
A high-precision scientific calculator with support for units, derivatives, and more.
|
A pretty, general-purpose scientific calculator with support for units, derivatives, and more.
|
||||||
|
|
||||||
Many features are missing, this is still under development.
|
Many features are missing, this is still under development.
|
||||||
|
|
||||||
**Web demo: [here](daisy.betalupi.com)**
|
**Web demo: [here](https://daisy.betalupi.com) (won't work on mobile)**
|
||||||
|
|
||||||
# 📦 Installation
|
# 📦 Installation
|
||||||
|
- **From source:** `cargo build --release`, binary will be at `./target/release/daisy`
|
||||||
- **Cargo:** `cargo install daisycalc`
|
- **Cargo:** `cargo install daisycalc`
|
||||||
- **Arch:** `yay -S daisy`
|
- **Arch:** `yay -S daisy`
|
||||||
- **Debian:** coming soon
|
- **Debian:** coming soon
|
||||||
|
- **Nix:** Use `default.nix`. Daisy isn't in nixpkgs yet, you'll need to add something like the following to `configuration.nix`:
|
||||||
|
|
||||||
From source: `cargo build --release` \
|
```nix
|
||||||
Binary will be in `target/release/daisy`
|
let
|
||||||
|
daisy = builtins.fetchGit {
|
||||||
|
url = "https://github.com/rm-dr/daisy.git";
|
||||||
|
ref = "master";
|
||||||
|
} + /default.nix;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
environment.systemPackages = with pkgs; [
|
||||||
|
(callPackage daisy { })
|
||||||
|
];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
# 📹 Screenshot
|
# 📹 Screenshot
|
||||||
|
|
||||||
@ -21,9 +33,7 @@ Binary will be in `target/release/daisy`
|
|||||||
|
|
||||||
# 🛠️ Features
|
# 🛠️ Features
|
||||||
- Open-source
|
- Open-source
|
||||||
- Extremely high precision
|
- Carefully designed and easy-to-read prompt
|
||||||
- Uses a rational datatype when possible, and a high-precision float when not.
|
|
||||||
- Pretty printing in prompt (with special substitutions)
|
|
||||||
- Supports many physical units, with metric and binary prefixes
|
- Supports many physical units, with metric and binary prefixes
|
||||||
- Supports exponential notation
|
- Supports exponential notation
|
||||||
- Clear syntax, parsed input is always re-printed as a sanity check.
|
- Clear syntax, parsed input is always re-printed as a sanity check.
|
||||||
@ -65,6 +75,10 @@ Daisy instead provides four functions (`fromCelsius`, `toCelsius`, `fromFahrenhe
|
|||||||
- "from" functions take a scalar and return a value in Kelvin: `fromCelsius(0) = 273.15K`
|
- "from" functions take a scalar and return a value in Kelvin: `fromCelsius(0) = 273.15K`
|
||||||
- "to" functions take a value in Kelvin and return a scalar: `toCelsius(273.15 K) = 0`
|
- "to" functions take a value in Kelvin and return a scalar: `toCelsius(273.15 K) = 0`
|
||||||
|
|
||||||
|
Functions `FtoC` and `CtoF` are also provided:
|
||||||
|
- `FtoC(x) = toCelsius(fromFahrenheit(x))`
|
||||||
|
- `CtoF(x) = toFahrenheit(fromCelsius(x))`
|
||||||
|
|
||||||
|
|
||||||
## Multiplication Order
|
## Multiplication Order
|
||||||
|
|
||||||
|
36
TODO.md
@ -1,55 +1,33 @@
|
|||||||
## Version Bump checklist
|
|
||||||
- TODO: build and publish script
|
|
||||||
- update Cargo.toml
|
|
||||||
- run cargo test
|
|
||||||
- commit
|
|
||||||
- push
|
|
||||||
- merge
|
|
||||||
- git tag -a v1.0.0 -m "Version 1.0.0" on merge commit
|
|
||||||
- cargo publish
|
|
||||||
- Update packages
|
|
||||||
- Build wasm & push changes
|
|
||||||
|
|
||||||
|
|
||||||
## Pre-release
|
|
||||||
- Tuple operations
|
|
||||||
- we don't need vectors as arguments to operators
|
|
||||||
- Fix linelocation when evaluating functions
|
|
||||||
|
|
||||||
## Parser
|
## Parser
|
||||||
- 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)
|
- Better tests (assignment, many expressions in one context)
|
||||||
- Optional config file
|
- Optional config file
|
||||||
- Optional history file
|
- Optional history file
|
||||||
- evaluate straight from command line
|
|
||||||
- Package for debian, nix
|
- Package for debian, nix
|
||||||
|
|
||||||
|
|
||||||
## 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 floats
|
||||||
|
- we don't need vectors as arguments to operators
|
||||||
|
|
||||||
## Math Features
|
## Math Features
|
||||||
- Mean, Median, Min
|
- Mean, Median, Min
|
||||||
- Arbitrary base logarithm
|
- Arbitrary base logarithm
|
||||||
- Derivatives
|
|
||||||
- CAS features (trig, roots and powers)
|
|
||||||
- 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
|
- Add functions: gcd, inverse mod, dice
|
||||||
|
- Tuple operations
|
||||||
|
- Number theory: select a group, inverses, etc
|
||||||
|
- Negative mod
|
||||||
|
|
||||||
## Prompt
|
## Prompt
|
||||||
- Fix terminal color detection
|
- Fix terminal color detection
|
||||||
- Live syntax/output (like firefox js terminal)
|
- Live syntax/output (like firefox js terminal)
|
||||||
- Syntax highlighting
|
- Syntax highlighting
|
||||||
- fish-style tab completion
|
- Numbered history recall
|
||||||
- Numbered expressions, history recall
|
|
||||||
- Enable/disable unit sets (defaults?)
|
- Enable/disable unit sets (defaults?)
|
||||||
- Consistent unit ordering
|
- Consistent unit ordering
|
||||||
|
|
||||||
@ -57,5 +35,5 @@
|
|||||||
- long prefixes (megatonne, etc)
|
- long prefixes (megatonne, etc)
|
||||||
- HMS for degrees
|
- HMS for degrees
|
||||||
- Exact radians, using pi constant?
|
- Exact radians, using pi constant?
|
||||||
- Weird units: flops, lumen, lux, bel
|
- Odd units: flops, lumen, lux, bel
|
||||||
- Command to list units
|
- Command to list units
|
@ -49,7 +49,7 @@ value = "1.602176634e-19 C"
|
|||||||
enum_name = "ElectronMass"
|
enum_name = "ElectronMass"
|
||||||
pretty_name = "Electron mass"
|
pretty_name = "Electron mass"
|
||||||
strings = ["electronmass"]
|
strings = ["electronmass"]
|
||||||
value = "9.1093837015-31 kg"
|
value = "9.1093837015e-31 kg"
|
||||||
|
|
||||||
[[constant]]
|
[[constant]]
|
||||||
enum_name = "ProtonMass"
|
enum_name = "ProtonMass"
|
||||||
|
19
default.nix
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{ lib, fetchgit, rustPlatform }:
|
||||||
|
rustPlatform.buildRustPackage rec {
|
||||||
|
pname = "daisy";
|
||||||
|
version = "1.1.7";
|
||||||
|
cargoLock.lockFile = src + /Cargo.lock;
|
||||||
|
|
||||||
|
src = builtins.fetchGit {
|
||||||
|
url = "https://github.com/rm-dr/daisy.git";
|
||||||
|
ref = "refs/tags/v${version}";
|
||||||
|
#rev = ""; Ideally, we'd have a hash here, but that would make git history messy.
|
||||||
|
};
|
||||||
|
|
||||||
|
meta = with lib; {
|
||||||
|
description = "A general-purpose scientific calculator";
|
||||||
|
homepage = "https://github.com/rm-dr/daisy";
|
||||||
|
#license = licenses.GPL;
|
||||||
|
maintainers = [ maintainers.tailhook ];
|
||||||
|
};
|
||||||
|
}
|
2
server/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/site/node_modules
|
||||||
|
/pkg
|
@ -7,7 +7,7 @@ networks:
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
daisy:
|
daisy:
|
||||||
build: ./.
|
build: .
|
||||||
container_name: daisy
|
container_name: daisy
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
@ -22,7 +22,6 @@
|
|||||||
|
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
overflow: hidden;
|
|
||||||
|
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
}
|
}
|
||||||
@ -87,7 +86,7 @@
|
|||||||
|
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<img id="banner" src = "resources/daisy-light.svg" alt="Daisy Banner"/>
|
<img id="banner" src = "resources/daisy-light.svg" alt="Daisy Banner"/>
|
||||||
<p>A high-precision, general-purpose scientific calculator</p>
|
<p>A pretty, general-purpose scientific calculator</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="terminal"></div>
|
<div id="terminal"></div>
|
||||||
@ -101,6 +100,11 @@
|
|||||||
<a href="https://aur.archlinux.org/packages/daisy" target="_blank" rel="noopener noreferrer">AUR</a>
|
<a href="https://aur.archlinux.org/packages/daisy" target="_blank" rel="noopener noreferrer">AUR</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
|
||||||
|
if (mobile) { alert("This site may not work on mobile."); }
|
||||||
|
</script>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
// See wasm-pack docs
|
// See wasm-pack docs
|
||||||
// Build with `wasm-pack build --release --target web --out-dir pkg`
|
// Build with `wasm-pack build --release --target web --out-dir pkg`
|
17
server/site/package-lock.json
generated
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "site",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"dependencies": {
|
||||||
|
"xterm": "^5.3.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/xterm": {
|
||||||
|
"version": "5.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/xterm/-/xterm-5.3.0.tgz",
|
||||||
|
"integrity": "sha512-8QqjlekLUFTrU6x7xck1MsPzPA571K5zNqWm0M0oroYEWVOptZ0+ubQSkQ3uxIEhcIHRujJy6emDWX4A7qyFzg=="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 5.7 KiB After Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 5.7 KiB After Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 92 KiB After Width: | Height: | Size: 92 KiB |
15
shell.nix
@ -1,11 +1,22 @@
|
|||||||
|
{ nixpkgs ? import <nixpkgs> { }}:
|
||||||
let
|
let
|
||||||
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/f155f0cf4ea43c4e3c8918d2d327d44777b6cad4.tar.gz") {};
|
# If you set hash to an empty string, you'll get
|
||||||
|
# an error with the correct hash.
|
||||||
|
pinnedPkgs = nixpkgs.fetchFromGitHub {
|
||||||
|
owner = "NixOS";
|
||||||
|
repo = "nixpkgs";
|
||||||
|
rev = "4ecab3273592f27479a583fb6d975d4aba3486fe";
|
||||||
|
sha256 = "btHN1czJ6rzteeCuE/PNrdssqYD2nIA4w48miQAFloM=";
|
||||||
|
};
|
||||||
|
pkgs = import pinnedPkgs {};
|
||||||
|
|
||||||
in pkgs.mkShell {
|
in pkgs.mkShell {
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
cargo
|
cargo
|
||||||
rustc
|
rustc
|
||||||
rustfmt
|
rustfmt
|
||||||
m4
|
rust-analyzer
|
||||||
|
wasm-pack
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
site/.gitignore
vendored
@ -1,3 +0,0 @@
|
|||||||
/node_modules
|
|
||||||
/package-lock.json
|
|
||||||
/pkg
|
|
@ -57,8 +57,8 @@ pub fn do_command(
|
|||||||
|
|
||||||
t.push(
|
t.push(
|
||||||
concat!(
|
concat!(
|
||||||
"Daisy is a high-precision, general-purpose\n",
|
"Daisy is a pretty, general-purpose\n",
|
||||||
"scientific calculator.\n",
|
"TUI scientific calculator.\n",
|
||||||
"\n",
|
"\n",
|
||||||
" - Use Up/Down arrows to navigate history.\n",
|
" - Use Up/Down arrows to navigate history.\n",
|
||||||
" - Use Ctrl-C or Ctrl-D to quit.\n",
|
" - Use Ctrl-C or Ctrl-D to quit.\n",
|
||||||
@ -152,6 +152,9 @@ pub fn do_command(
|
|||||||
" Fahrenheit to Kelvin [c]fromF, fromFahrenheit[n]\n",
|
" Fahrenheit to Kelvin [c]fromF, fromFahrenheit[n]\n",
|
||||||
" Kelvin to Fahrenheit [c]toF, toFahrenheit[n]\n",
|
" Kelvin to Fahrenheit [c]toF, toFahrenheit[n]\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" Celsius to Fahrenheit [c]CtoF[n]\n",
|
||||||
|
" Fahrenheit to Celsius [c]FtoC[n]\n",
|
||||||
|
"\n",
|
||||||
" convert to base unit [c]tobase[n]\n",
|
" convert to base unit [c]tobase[n]\n",
|
||||||
" remove units [c]nounit[n]\n",
|
" remove units [c]nounit[n]\n",
|
||||||
"\n\n"
|
"\n\n"
|
||||||
@ -247,7 +250,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] [e]takes exactly one argument.[n]\n\n",
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -261,7 +264,7 @@ pub fn do_command(
|
|||||||
Err(()) => {
|
Err(()) => {
|
||||||
FormattedText::new(
|
FormattedText::new(
|
||||||
format!(
|
format!(
|
||||||
"[c]{v}[n] [t]isn't a variable.[n]\n\n",
|
"[c]{v}[n] [e]isn't a variable.[n]\n\n",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -90,6 +90,7 @@ impl Context {
|
|||||||
|
|
||||||
pub fn delete(&mut self, s: &String) -> Result<(), ()> {
|
pub fn delete(&mut self, s: &String) -> Result<(), ()> {
|
||||||
if !(self.is_varible(s) || self.is_function(s)) { return Err(()) };
|
if !(self.is_varible(s) || self.is_function(s)) { return Err(()) };
|
||||||
|
if s == "ans" { return Err(()) };
|
||||||
if self.is_varible(s) { self.variables.remove(s); }
|
if self.is_varible(s) { self.variables.remove(s); }
|
||||||
if self.is_function(s) { self.functions.remove(s); }
|
if self.is_function(s) { self.functions.remove(s); }
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -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(_), _) => { eval_function(context, g)? },
|
||||||
Expression::Operator(_, _, _) => { eval_operator(context, g)? },
|
Expression::Operator(_, _, _) => { eval_operator(context, g)? },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use crate::parser::Expression;
|
use crate::parser::Expression;
|
||||||
use crate::parser::Function;
|
use crate::parser::Function;
|
||||||
use crate::parser::Operator;
|
use crate::parser::Operator;
|
||||||
use crate::parser::LineLocation;
|
use crate::parser::LineLocation;
|
||||||
use crate::quantity::FreeUnit;
|
use crate::quantity::FreeUnit;
|
||||||
|
use crate::quantity::Unit;
|
||||||
use crate::quantity::WholeUnit;
|
use crate::quantity::WholeUnit;
|
||||||
use crate::quantity::Quantity;
|
use crate::quantity::Quantity;
|
||||||
use crate::quantity::Scalar;
|
use crate::quantity::Scalar;
|
||||||
use crate::errors::DaisyError;
|
use crate::errors::DaisyError;
|
||||||
|
use crate::context::Context;
|
||||||
|
use super::evaluate;
|
||||||
|
|
||||||
// If unitless, do nothing
|
// If unitless, do nothing
|
||||||
// If compatible with radians, convert to radians and return unitless
|
// If compatible with radians, convert to radians and return unitless
|
||||||
@ -26,7 +30,7 @@ fn to_radians(q: Quantity) -> Result<Quantity, ()> {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
|
pub fn eval_function(context: &mut Context, g: &Expression) -> Result<Option<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!()};
|
||||||
|
|
||||||
@ -150,21 +154,82 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
Function::CtoF => {
|
||||||
|
return Ok(evaluate(context,
|
||||||
|
&Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::ToFahrenheit),
|
||||||
|
VecDeque::from(vec![Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::FromCelsius),
|
||||||
|
VecDeque::from(vec![Expression::Quantity(*l, q.clone())])
|
||||||
|
)])
|
||||||
|
)
|
||||||
|
).ok());
|
||||||
|
},
|
||||||
|
|
||||||
|
Function::FtoC => {
|
||||||
|
return Ok(evaluate(context,
|
||||||
|
&Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::ToCelsius),
|
||||||
|
VecDeque::from(vec![Expression::Operator(
|
||||||
|
*l + *loc,
|
||||||
|
Operator::Function(Function::FromFahrenheit),
|
||||||
|
VecDeque::from(vec![Expression::Quantity(*l, q.clone())])
|
||||||
|
)])
|
||||||
|
)
|
||||||
|
).ok());
|
||||||
|
},
|
||||||
|
|
||||||
Function::ToCelsius => {
|
Function::ToCelsius => {
|
||||||
let mut k = Quantity::new_rational(1f64).unwrap();
|
let mut k = Quantity::new_rational(1f64).unwrap();
|
||||||
k.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
k.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
||||||
let Some(q) = q.convert_to(k) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)) };
|
|
||||||
|
let q_s: String;
|
||||||
|
if q.unitless() {
|
||||||
|
q_s = String::from("scalar");
|
||||||
|
} else {
|
||||||
|
q_s = q.convert_to_base().unit().display(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(q) = q.convert_to(k) else {
|
||||||
|
return Err((
|
||||||
|
*loc + *l,
|
||||||
|
DaisyError::IncompatibleUnits(
|
||||||
|
q_s,
|
||||||
|
Unit::from_free(FreeUnit::from_whole(WholeUnit::Kelvin)).display(context)
|
||||||
|
)
|
||||||
|
))
|
||||||
|
};
|
||||||
|
|
||||||
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(Some(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();
|
||||||
k.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
k.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
|
||||||
let Some(q) = q.convert_to(k) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)) };
|
|
||||||
|
let q_s: String;
|
||||||
|
if q.unitless() {
|
||||||
|
q_s = String::from("scalar");
|
||||||
|
} else {
|
||||||
|
q_s = q.convert_to_base().unit().display(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(q) = q.convert_to(k) else {
|
||||||
|
return Err((
|
||||||
|
*loc + *l,
|
||||||
|
DaisyError::IncompatibleUnits(
|
||||||
|
q_s,
|
||||||
|
Unit::from_free(FreeUnit::from_whole(WholeUnit::Kelvin)).display(context)
|
||||||
|
)
|
||||||
|
))
|
||||||
|
};
|
||||||
|
|
||||||
let mut r = q.without_unit();
|
let mut r = q.without_unit();
|
||||||
r *= Quantity::new_rational_from_frac(9i64, 5i64).unwrap();
|
r *= Quantity::new_rational_from_frac(9i64, 5i64).unwrap();
|
||||||
@ -173,8 +238,18 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
|
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
return Ok(Some(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::IncompatibleUnits(
|
||||||
|
q.convert_to_base().unit().display(context),
|
||||||
|
"scalar".to_string()
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
let mut r = Quantity::new_rational(273.15f64).unwrap();
|
let mut r = Quantity::new_rational(273.15f64).unwrap();
|
||||||
r += q.clone();
|
r += q.clone();
|
||||||
@ -182,8 +257,18 @@ pub fn eval_function(g: &Expression) -> Result<Option<Expression>, (LineLocation
|
|||||||
|
|
||||||
return Ok(Some(Expression::Quantity(*loc + *l, r)));
|
return Ok(Some(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::IncompatibleUnits(
|
||||||
|
q.convert_to_base().unit().display(context),
|
||||||
|
"scalar".to_string()
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
let mut r = q.clone();
|
let mut r = q.clone();
|
||||||
r += Quantity::new_rational(459.67).unwrap();
|
r += Quantity::new_rational(459.67).unwrap();
|
||||||
|
@ -19,6 +19,7 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
|
|
||||||
if args.len() != 1 {panic!()};
|
if args.len() != 1 {panic!()};
|
||||||
let a = &args[0];
|
let a = &args[0];
|
||||||
|
let mut args_ll = op_loc.clone();
|
||||||
|
|
||||||
if sh_vars.len() == 1 {
|
if sh_vars.len() == 1 {
|
||||||
if let Expression::Tuple(l, v) = a {
|
if let Expression::Tuple(l, v) = a {
|
||||||
@ -28,6 +29,7 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
args_ll += a.get_linelocation();
|
||||||
context.add_shadow(sh_vars[0].clone(), Some(a.clone()));
|
context.add_shadow(sh_vars[0].clone(), Some(a.clone()));
|
||||||
} else {
|
} else {
|
||||||
let Expression::Tuple(l, v) = a else {
|
let Expression::Tuple(l, v) = a else {
|
||||||
@ -46,18 +48,28 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < sh_vars.len() {
|
while i < sh_vars.len() {
|
||||||
|
args_ll += v[i].get_linelocation();
|
||||||
context.add_shadow(sh_vars[i].clone(), Some(v[i].clone()));
|
context.add_shadow(sh_vars[i].clone(), Some(v[i].clone()));
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let r = evaluate(context, &exp)?;
|
let r = evaluate(context, &exp);
|
||||||
context.clear_shadow();
|
context.clear_shadow();
|
||||||
|
|
||||||
|
match r {
|
||||||
|
Ok(mut r) => {
|
||||||
|
r.set_linelocation(&args_ll);
|
||||||
return Ok(Some(r));
|
return Ok(Some(r));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Err( (_, err) ) => {
|
||||||
|
return Err((args_ll, err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
Operator::Negative => {
|
Operator::Negative => {
|
||||||
if args.len() != 1 { panic!() };
|
if args.len() != 1 { panic!() };
|
||||||
let args = &args[0];
|
let args = &args[0];
|
||||||
@ -112,6 +124,8 @@ 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 a_s: String;
|
||||||
let b_s: String;
|
let b_s: String;
|
||||||
|
20
src/lib.rs
@ -68,9 +68,7 @@ cfg_if::cfg_if! {
|
|||||||
return format!("\r\n{}", daisy_prompt(state));
|
return format!("\r\n{}", daisy_prompt(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
if in_str.trim() == "quit" {
|
|
||||||
return "[quit]".to_string();
|
|
||||||
} else {
|
|
||||||
let r = crate::do_string( unsafe { &mut (*state).context }, &in_str);
|
let r = crate::do_string( unsafe { &mut (*state).context }, &in_str);
|
||||||
|
|
||||||
match r {
|
match r {
|
||||||
@ -78,7 +76,6 @@ cfg_if::cfg_if! {
|
|||||||
out += t;
|
out += t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"\x7F" => { unsafe { (*state).promptbuffer.backspace(); } },
|
"\x7F" => { unsafe { (*state).promptbuffer.backspace(); } },
|
||||||
@ -91,7 +88,20 @@ cfg_if::cfg_if! {
|
|||||||
//'\x04' | '\x03'
|
//'\x04' | '\x03'
|
||||||
//=> { break 'outer; },
|
//=> { break 'outer; },
|
||||||
|
|
||||||
_ => { unsafe { (*state).promptbuffer.add_char(s.chars().next().unwrap()); } },
|
// Only process sane characters
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
let c = s.chars().next().unwrap();
|
||||||
|
match c {
|
||||||
|
'a'..='z' | 'A'..='Z' | '0'..='9'
|
||||||
|
|'!'|'@'|'#'|'$'|'%'|'^'|'&'|'*'|'('|')'
|
||||||
|
|'?'|'~'|','|'.'|'['|']'|' '
|
||||||
|
|'<'|'>'|'/'|'_'|'-'|':'|'|'|'='|'+'|';'
|
||||||
|
=> { unsafe { (*state).promptbuffer.add_char(c); } },
|
||||||
|
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let t = unsafe { (*state).promptbuffer.write_prompt(&mut (*state).context) };
|
let t = unsafe { (*state).promptbuffer.write_prompt(&mut (*state).context) };
|
||||||
|
@ -120,7 +120,7 @@ pub fn main() -> Result<(), std::io::Error> {
|
|||||||
// Only process sane characters
|
// Only process sane characters
|
||||||
'a'..='z' | 'A'..='Z' | '0'..='9'
|
'a'..='z' | 'A'..='Z' | '0'..='9'
|
||||||
|'!'|'@'|'#'|'$'|'%'|'^'|'&'|'*'|'('|')'
|
|'!'|'@'|'#'|'$'|'%'|'^'|'&'|'*'|'('|')'
|
||||||
|'?'|'~'|','|'.'|'['|']'
|
|'?'|'~'|','|'.'|'['|']'|' '
|
||||||
|'<'|'>'|'/'|'_'|'-'|':'|'|'|'='|'+'|';'
|
|'<'|'>'|'/'|'_'|'-'|':'|'|'|'='|'+'|';'
|
||||||
=> { pb.add_char(*q); },
|
=> { pb.add_char(*q); },
|
||||||
|
|
||||||
|
@ -37,7 +37,9 @@ pub enum Function {
|
|||||||
FromCelsius,
|
FromCelsius,
|
||||||
ToCelsius,
|
ToCelsius,
|
||||||
FromFahrenheit,
|
FromFahrenheit,
|
||||||
ToFahrenheit
|
ToFahrenheit,
|
||||||
|
CtoF,
|
||||||
|
FtoC,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -74,6 +76,8 @@ impl ToString for Function {
|
|||||||
Function::ToCelsius => {String::from("tocelsius") },
|
Function::ToCelsius => {String::from("tocelsius") },
|
||||||
Function::FromFahrenheit => { String::from("fromfahrenheit") },
|
Function::FromFahrenheit => { String::from("fromfahrenheit") },
|
||||||
Function::ToFahrenheit => { String::from("tofahrenheit") },
|
Function::ToFahrenheit => { String::from("tofahrenheit") },
|
||||||
|
Function::FtoC => { String::from("FtoC") },
|
||||||
|
Function::CtoF => { String::from("CtoF") },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,6 +130,11 @@ impl Function {
|
|||||||
"fromF" => {Some(Function::FromFahrenheit)},
|
"fromF" => {Some(Function::FromFahrenheit)},
|
||||||
"fromfahrenheit" => {Some(Function::FromFahrenheit)},
|
"fromfahrenheit" => {Some(Function::FromFahrenheit)},
|
||||||
"fromFahrenheit" => {Some(Function::FromFahrenheit)},
|
"fromFahrenheit" => {Some(Function::FromFahrenheit)},
|
||||||
|
|
||||||
|
"FtoC" => {Some(Function::FtoC)},
|
||||||
|
"ftoc" => {Some(Function::FtoC)},
|
||||||
|
"ctof" => {Some(Function::CtoF)},
|
||||||
|
"CtoF" => {Some(Function::CtoF)},
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -232,5 +232,11 @@ fn functions() {
|
|||||||
|
|
||||||
good_expr("2", "nounit(2 mm)");
|
good_expr("2", "nounit(2 mm)");
|
||||||
good_expr("2", "nounit(2 meter * second)");
|
good_expr("2", "nounit(2 meter * second)");
|
||||||
|
|
||||||
|
good_expr("37.778", "FtoC(100)");
|
||||||
|
good_expr("73.399", "CtoF(23)");
|
||||||
|
good_expr("-17.778", "FtoC(0)");
|
||||||
|
good_expr("31.999", "CtoF(0)");
|
||||||
|
|
||||||
//good_expr("5000 m²·g/(s²·A²)", "tobase(5H)");
|
//good_expr("5000 m²·g/(s²·A²)", "tobase(5H)");
|
||||||
}
|
}
|