Compare commits

..

13 Commits

Author SHA1 Message Date
rm-dr b8bde580a3
Added CtoF and FtoC 2023-10-15 12:07:09 -07:00
rm-dr 819e444378
Updated README 2023-10-15 11:42:40 -07:00
rm-dr bc1716eefd
Version bump 2023-10-15 11:37:51 -07:00
rm-dr 42555e0df7
Updated default.nix 2023-10-15 11:37:02 -07:00
rm-dr cd1074d30f
Updated build docs 2023-10-15 11:36:53 -07:00
rm-dr 7726eea34d
Updated nix derivation 2023-10-15 11:30:09 -07:00
rm-dr 6fd315a9fe
Version bump 2023-10-15 11:29:19 -07:00
rm-dr 0d81d58370
Fixed subtract unit check 2023-10-15 11:26:54 -07:00
rm-dr 2ff42bf62a
Added unit checks to temp conversions 2023-10-15 11:26:45 -07:00
rm-dr b5a23f814f
Fixed colors 2023-10-15 11:26:15 -07:00
rm-dr e3e23a686d
Fixed `del ans` 2023-10-15 11:26:08 -07:00
rm-dr 93c34ca4a4
Updated docs 2023-10-15 11:25:19 -07:00
rm-dr 80f17ac210 Whitespace 2023-10-15 09:30:32 -07:00
14 changed files with 146 additions and 32 deletions

View File

@ -29,3 +29,4 @@ What to do
- `cargo publish` - `cargo publish`
- Update web demo & pull server (`make docker`) - Update web demo & pull server (`make docker`)
- Update aur package - Update aur package
- Update `default.nix` (test with `make nix`)

2
Cargo.lock generated
View File

@ -28,7 +28,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "daisycalc" name = "daisycalc"
version = "1.1.5" version = "1.1.7"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"num", "num",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "daisycalc" name = "daisycalc"
version = "1.1.5" 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"

View File

@ -1,6 +1,9 @@
release: release:
cargo build --release cargo build --release
nix:
nix-build -E 'let pkgs = import <nixpkgs> { }; in pkgs.callPackage ./default.nix {}'
test: test:
cargo test cargo test

View File

@ -14,17 +14,17 @@ Many features are missing, this is still under development.
- **Nix:** Use `default.nix`. Daisy isn't in nixpkgs yet, you'll need to add something like the following to `configuration.nix`: - **Nix:** Use `default.nix`. Daisy isn't in nixpkgs yet, you'll need to add something like the following to `configuration.nix`:
```nix ```nix
let let
daisy = builtins.fetchGit { daisy = builtins.fetchGit {
url = "https://github.com/rm-dr/daisy.git"; url = "https://github.com/rm-dr/daisy.git";
ref = "master"; ref = "master";
} + /default.nix; } + /default.nix;
in in
{ {
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
(callPackage daisy { }) (callPackage daisy { })
]; ];
} }
``` ```
# 📹 Screenshot # 📹 Screenshot
@ -33,9 +33,7 @@ Many features are missing, this is still under development.
# 🛠️ 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.
@ -77,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

View File

@ -20,6 +20,8 @@
- 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 - Tuple operations
- Number theory: select a group, inverses, etc
- Negative mod
## Prompt ## Prompt
- Fix terminal color detection - Fix terminal color detection

View File

@ -1,17 +1,17 @@
{ lib, fetchgit, rustPlatform }: { lib, fetchgit, rustPlatform }:
rustPlatform.buildRustPackage rec { rustPlatform.buildRustPackage rec {
pname = "daisy"; pname = "daisy";
version = "1.1.4"; version = "1.1.7";
cargoLock.lockFile = src + /Cargo.lock; cargoLock.lockFile = src + /Cargo.lock;
src = fetchgit { src = builtins.fetchGit {
url = "https://github.com/rm-dr/daisy.git"; url = "https://github.com/rm-dr/daisy.git";
rev = "v${version}"; ref = "refs/tags/v${version}";
sha256 = "sha256-aENuKtE1+tBRN0HZzRr8Gk+dVEYTiP6FNRz817Sk88o="; #rev = ""; Ideally, we'd have a hash here, but that would make git history messy.
}; };
meta = with lib; { meta = with lib; {
description = "A pretty command-line scientific calculator"; description = "A general-purpose scientific calculator";
homepage = "https://github.com/rm-dr/daisy"; homepage = "https://github.com/rm-dr/daisy";
#license = licenses.GPL; #license = licenses.GPL;
maintainers = [ maintainers.tailhook ]; maintainers = [ maintainers.tailhook ];

View File

@ -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",
) )
) )
} }

View File

@ -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(());

View File

@ -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)? },
}; };

View File

@ -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();

View File

@ -124,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;

View File

@ -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
} }
} }

View File

@ -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)");
} }