1 Commits

Author SHA1 Message Date
a6c3ffa68d Merge v1.0.1 2023-08-17 10:30:26 -07:00
14 changed files with 213 additions and 277 deletions

27
TODO.md
View File

@ -1,34 +1,38 @@
## 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
- Auto-push to crates.io
- Package for debian - 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?
@ -36,6 +40,7 @@
- Remove rug dependency (too big, incompatible) - 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
@ -43,16 +48,20 @@
- 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 - Command to list substitutions
## Units ## Units

View File

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

View File

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

View File

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

View File

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

View File

@ -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())));
@ -188,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); }
@ -207,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())));

View File

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

View File

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

View File

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

View File

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

View File

@ -244,7 +244,7 @@ impl Rem<FloatBase> for FloatBase {
(!modulus.fract().unwrap().is_zero()) (!modulus.fract().unwrap().is_zero())
} { panic!() } } { panic!() }
FloatBase{val : self.val.trunc() % modulus.val.trunc()} FloatBase{val : self.val.fract() % modulus.val.fract()}
} }
} }

View File

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

View File

@ -186,7 +186,7 @@ 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!()}
} }
} }

View File

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