mirror of
https://github.com/rm-dr/daisy
synced 2025-07-04 01:29:33 -07:00
Compare commits
15 Commits
v1.0.1
...
3a08cfb2d3
Author | SHA1 | Date | |
---|---|---|---|
3a08cfb2d3
|
|||
b136353d36
|
|||
a125e867c4
|
|||
c477302c88
|
|||
edc859dc01
|
|||
77c357c2f3
|
|||
3ae5383eed
|
|||
4055c08217
|
|||
6969d17cce
|
|||
6e3609d85e
|
|||
f9ec4d82fe
|
|||
86b5356d4f
|
|||
cc81c3979c
|
|||
b846a7c144
|
|||
09996801d8
|
27
TODO.md
27
TODO.md
@ -1,38 +1,34 @@
|
||||
## Version Bump checklist
|
||||
- TODO: build and publish script
|
||||
- update Cargo.toml
|
||||
- run cargo test
|
||||
- commit
|
||||
- git tag -a v1.0.0 -m "Version 1.0.0"
|
||||
- git push
|
||||
- git push origin v1.0.0
|
||||
- push
|
||||
- merge
|
||||
- git tag -a v1.0.0 -m "Version 1.0.0" on merge commit
|
||||
- cargo publish
|
||||
- Update packages
|
||||
|
||||
|
||||
|
||||
## Pre-release
|
||||
- Fix linelocation (consistent, what does an operator's linelocation mean?)
|
||||
- Tuple operations
|
||||
- we don't need vectors as arguments to operators
|
||||
- Assignment tests
|
||||
- Fix linelocation when evaluating functions
|
||||
|
||||
## Parser
|
||||
- Better error when `sin = 2`
|
||||
- Should functions be operators?
|
||||
- Binary, hex, octal numbers
|
||||
|
||||
|
||||
## General
|
||||
- Better tests (assignment, many expressions in one context)
|
||||
- Optional config file
|
||||
- Optional history file
|
||||
- daisyrc file
|
||||
- Compile to WASM, publish a webapp
|
||||
- evaluate straight from command line
|
||||
- Auto-push to crates.io
|
||||
- Package for debian
|
||||
|
||||
|
||||
|
||||
## Internals
|
||||
- Non-recursive treeify
|
||||
- Faster factorial function. Maybe use gamma instead?
|
||||
@ -40,7 +36,6 @@
|
||||
- Remove rug dependency (too big, incompatible)
|
||||
|
||||
## Math Features
|
||||
- Dice
|
||||
- Mean, Median, Min
|
||||
- Arbitrary base logarithm
|
||||
- Derivatives
|
||||
@ -48,20 +43,16 @@
|
||||
- Complex numbers
|
||||
- acot/acoth functions
|
||||
- Sums and products with functional arguments
|
||||
- Add functions: gcd, inverse mod, dice
|
||||
|
||||
## Prompt
|
||||
- Fix terminal color detection
|
||||
- Live syntax/output (like firefox js terminal)
|
||||
- Syntax highlight input and output
|
||||
- Syntax highlighting
|
||||
- fish-style tab completion
|
||||
- Numbered expressions, history recall
|
||||
- Color configuration
|
||||
- Enable/disable unit sets (defaults?)
|
||||
- Consistent unit ordering
|
||||
- Better linelocation
|
||||
- we shouldn't need to re-print user input on evaluation errors, red arrows should adjust themselves to the prettyprinted string
|
||||
- Backend-independent colorful printing
|
||||
- Better colors in error texts
|
||||
- Better substitution. Consistent: when ascii, when unicode?
|
||||
- Command to list substitutions
|
||||
|
||||
## Units
|
||||
|
@ -224,7 +224,7 @@ pub fn do_command(
|
||||
if args.len() != 2 {
|
||||
return FormattedText::new(
|
||||
format!(
|
||||
"[c]{first}[n] [t]takes exactly two arguments.[n]\n\n",
|
||||
"[c]{first}[n] [t]takes exactly one argument.[n]\n\n",
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ pub struct Config {
|
||||
// with prettier unicode alternatives?
|
||||
//
|
||||
// Automatically disabled if enable_unicode is off.
|
||||
//pub enable_substituion: bool,
|
||||
pub enable_substituion: bool,
|
||||
|
||||
// Should we print simple powers
|
||||
// as unicode superscript chars?
|
||||
@ -38,7 +38,7 @@ impl Config {
|
||||
pub fn new() -> Config {
|
||||
Config{
|
||||
term_color_type: 2,
|
||||
//enable_substituion: true,
|
||||
enable_substituion: true,
|
||||
//enable_unicode: true,
|
||||
enable_super_powers: true,
|
||||
enable_one_over_power: true
|
||||
@ -126,6 +126,7 @@ impl Context {
|
||||
} else { panic!() }
|
||||
}
|
||||
|
||||
// Can we define a new variable with this name?
|
||||
pub fn valid_varible(&self, s: &str) -> bool {
|
||||
if {
|
||||
Function::from_string(s).is_some() ||
|
||||
@ -145,10 +146,17 @@ impl Context {
|
||||
}
|
||||
}
|
||||
|
||||
// Can we get a value fro mthis variable name?
|
||||
pub fn is_varible(&self, s: &str) -> bool {
|
||||
return {
|
||||
self.valid_varible(s) &&
|
||||
(self.variables.contains_key(s) || self.shadow.contains_key(s))
|
||||
(
|
||||
s == "ans" &&
|
||||
self.history.len() != 0
|
||||
) ||
|
||||
(
|
||||
self.valid_varible(s) &&
|
||||
(self.variables.contains_key(s) || self.shadow.contains_key(s))
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::io::Write;
|
||||
use termion::raw::RawTerminal;
|
||||
use termion::color;
|
||||
use termion::style;
|
||||
use crate::formattedtext;
|
||||
use crate::parser::substitute_cursor;
|
||||
use crate::context::Context;
|
||||
|
||||
const PROMPT_STR: &str = "==> ";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PromptBuffer {
|
||||
// History
|
||||
@ -38,30 +39,11 @@ impl PromptBuffer {
|
||||
|
||||
// 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> {
|
||||
// Draw prettyprinted expression
|
||||
let (_, s) = substitute_cursor(context, &self.get_contents(), self.buffer.chars().count());
|
||||
|
||||
write!(
|
||||
stdout, "\r{}{}==>{}{} {}",
|
||||
style::Bold,
|
||||
color::Fg(color::Blue),
|
||||
color::Fg(color::Reset),
|
||||
style::Reset,
|
||||
s
|
||||
)?;
|
||||
|
||||
// If this string is shorter, clear the remaining old one.
|
||||
if s.chars().count() < self.last_print_len {
|
||||
write!(
|
||||
stdout, "{}{}",
|
||||
" ".repeat(self.last_print_len - s.chars().count()),
|
||||
termion::cursor::Left((self.last_print_len - s.chars().count()) as u16)
|
||||
)?;
|
||||
}
|
||||
|
||||
self.last_print_len = s.chars().count();
|
||||
stdout.flush()?;
|
||||
return Ok(());
|
||||
let tmp = self.cursor;
|
||||
self.cursor = 0;
|
||||
let r = self.write_prompt(context, stdout);
|
||||
self.cursor = tmp;
|
||||
return r;
|
||||
}
|
||||
|
||||
pub fn write_prompt(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||
@ -69,38 +51,30 @@ impl PromptBuffer {
|
||||
let i = if l == 0 {0} else {l - self.cursor};
|
||||
|
||||
// Draw prettyprinted expression
|
||||
let (display_cursor, s) = substitute_cursor(context, &self.get_contents(), i);
|
||||
|
||||
let (display_c, s) = substitute_cursor(context, &self.get_contents(), i);
|
||||
|
||||
write!(
|
||||
stdout, "\r{}{}==>{}{} {}",
|
||||
style::Bold,
|
||||
color::Fg(color::Blue),
|
||||
color::Fg(color::Reset),
|
||||
style::Reset,
|
||||
stdout, "\r{}{PROMPT_STR}{}{}",
|
||||
formattedtext::format_map('p', context).unwrap(),
|
||||
formattedtext::format_map('n', context).unwrap(),
|
||||
s
|
||||
)?;
|
||||
|
||||
// If this string is shorter, clear the remaining old one.
|
||||
if s.chars().count() < self.last_print_len {
|
||||
write!(
|
||||
stdout, "{}{}",
|
||||
" ".repeat(self.last_print_len - s.chars().count()),
|
||||
termion::cursor::Left((self.last_print_len - s.chars().count()) as u16)
|
||||
)?;
|
||||
}
|
||||
|
||||
|
||||
// Move cursor to correct position
|
||||
if display_cursor != 0 {
|
||||
write!(
|
||||
stdout, "{}",
|
||||
termion::cursor::Left(display_cursor as u16)
|
||||
" ".repeat(self.last_print_len - s.chars().count()),
|
||||
)?;
|
||||
stdout.flush()?;
|
||||
}
|
||||
self.last_print_len = s.chars().count();
|
||||
|
||||
write!(
|
||||
stdout, "\r{}",
|
||||
termion::cursor::Right((display_c + PROMPT_STR.chars().count()) as u16)
|
||||
)?;
|
||||
|
||||
stdout.flush()?;
|
||||
self.last_print_len = s.chars().count();
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
@ -23,11 +23,11 @@ pub fn main() -> Result<(), std::io::Error> {
|
||||
// Set color compatibilty
|
||||
let term_colors = stdout.available_colors().unwrap_or(0);
|
||||
if term_colors >= 256 {
|
||||
context.config.term_color_type = 2
|
||||
context.config.term_color_type = 2;
|
||||
} else if term_colors >= 8 {
|
||||
context.config.term_color_type = 1
|
||||
context.config.term_color_type = 1;
|
||||
} else {
|
||||
context.config.term_color_type = 0
|
||||
context.config.term_color_type = 0;
|
||||
}
|
||||
|
||||
context.config.check();
|
||||
|
@ -75,12 +75,26 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
||||
if let Expression::Quantity(la, a) = a {
|
||||
if let Expression::Quantity(lb, b) = b {
|
||||
if !a.unit.compatible_with(&b.unit) {
|
||||
let a = a.convert_to_base().unit;
|
||||
let b = b.convert_to_base().unit;
|
||||
|
||||
let a_s: String;
|
||||
let b_s: String;
|
||||
if a.unitless() {
|
||||
a_s = String::from("scalar");
|
||||
} else {
|
||||
a_s = a.display(context);
|
||||
}
|
||||
|
||||
if b.unitless() {
|
||||
b_s = String::from("scalar");
|
||||
} else {
|
||||
b_s = b.display(context);
|
||||
}
|
||||
|
||||
return Err((
|
||||
*la + *lb + *op_loc,
|
||||
DaisyError::IncompatibleUnits(
|
||||
a.convert_to_base().unit.display(context),
|
||||
b.convert_to_base().unit.display(context)
|
||||
)
|
||||
DaisyError::IncompatibleUnits(a_s, b_s)
|
||||
));
|
||||
}
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone())));
|
||||
@ -98,12 +112,24 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
||||
if let Expression::Quantity(la, a) = a {
|
||||
if let Expression::Quantity(lb, b) = b {
|
||||
if !a.unit.compatible_with(&b.unit) {
|
||||
|
||||
let a_s: String;
|
||||
let b_s: String;
|
||||
if a.unitless() {
|
||||
a_s = String::from("scalar");
|
||||
} else {
|
||||
a_s = a.display(context);
|
||||
}
|
||||
|
||||
if b.unitless() {
|
||||
b_s = String::from("scalar");
|
||||
} else {
|
||||
b_s = b.display(context);
|
||||
}
|
||||
|
||||
return Err((
|
||||
*la + *lb + *op_loc,
|
||||
DaisyError::IncompatibleUnits(
|
||||
a.convert_to_base().unit.display(context),
|
||||
b.convert_to_base().unit.display(context)
|
||||
)
|
||||
DaisyError::IncompatibleUnits(a_s, b_s)
|
||||
));
|
||||
}
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() - b.clone())));
|
||||
@ -162,6 +188,11 @@ 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 vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, DaisyError::BadMath)); }
|
||||
|
||||
|
||||
let o = va.clone() % vb.clone();
|
||||
if o.is_nan() {return Err((*la + *lb + *op_loc, DaisyError::BadMath));}
|
||||
|
||||
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, va.clone() % vb.clone())));
|
||||
} else { return Ok(None); }
|
||||
} else { return Ok(None); }
|
||||
@ -176,12 +207,26 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
||||
if let Expression::Quantity(lb, vb) = b {
|
||||
let n = va.clone().convert_to(vb.clone());
|
||||
if n.is_none() {
|
||||
let va = va.convert_to_base().unit;
|
||||
let vb = vb.convert_to_base().unit;
|
||||
|
||||
let a_s: String;
|
||||
let b_s: String;
|
||||
if va.unitless() {
|
||||
a_s = String::from("scalar");
|
||||
} else {
|
||||
a_s = a.display(context);
|
||||
}
|
||||
|
||||
if vb.unitless() {
|
||||
b_s = String::from("scalar");
|
||||
} else {
|
||||
b_s = b.display(context);
|
||||
}
|
||||
|
||||
return Err((
|
||||
*la + *lb + *op_loc,
|
||||
DaisyError::IncompatibleUnits(
|
||||
va.convert_to_base().unit.display(context),
|
||||
vb.convert_to_base().unit.display(context)
|
||||
)
|
||||
DaisyError::IncompatibleUnits(a_s, b_s)
|
||||
));
|
||||
}
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, n.unwrap())));
|
||||
|
@ -22,7 +22,8 @@ impl ToString for FormattedText {
|
||||
fn format_map_none(c: char) -> Option<String> {
|
||||
Some(match c {
|
||||
'n'|'i'|'t'|'a'|
|
||||
'e'|'c'|'s'|'r'
|
||||
'e'|'c'|'s'|'r'|
|
||||
'p'
|
||||
=> { "".to_string() },
|
||||
_ => { return None }
|
||||
})
|
||||
@ -37,34 +38,39 @@ fn format_map_ansi(c: char) -> Option<String> {
|
||||
'i' => { // Normal italic text
|
||||
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
||||
},
|
||||
't' => { // Title text
|
||||
't' => { // Title text (should be cyan)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(6)), color::Bg(color::Reset))
|
||||
},
|
||||
'a' => { // Colored text
|
||||
'a' => { // Colored text (should be pink)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
||||
},
|
||||
'e' => { // Error titles
|
||||
'e' => { // Error titles (should be red)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(1)), color::Bg(color::Reset))
|
||||
},
|
||||
'c' => { // Console text
|
||||
'c' => { // Console text (inverted black on white)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7)))
|
||||
},
|
||||
's' => { // Repeat prompt (how => is styled)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset))
|
||||
},
|
||||
'r' => { // Result prompt (how = is styled)
|
||||
'p' => { // Input prompt (how ==> is styled) (should be blue)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(4)), color::Bg(color::Reset))
|
||||
},
|
||||
's' => { // Repeat prompt (how => is styled) (should be pink)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
||||
},
|
||||
'r' => { // Result prompt (how = is styled) (should be green)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset))
|
||||
},
|
||||
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// style::reset also resets color.
|
||||
// Make sure color comes AFTER style reset.
|
||||
fn format_map_full(c: char) -> Option<String> {
|
||||
Some(match c {
|
||||
'n' => { // Normal text
|
||||
format!("{}{}", color::Fg(color::Reset), style::Reset)
|
||||
format!("{}{}", style::Reset, color::Fg(color::Reset))
|
||||
},
|
||||
'i' => { // Normal italic text
|
||||
format!("{}{}", color::Fg(color::Reset), style::Italic)
|
||||
@ -73,7 +79,7 @@ fn format_map_full(c: char) -> Option<String> {
|
||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||
},
|
||||
'a' => { // Colored text
|
||||
format!("{}{}", color::Fg(color::Magenta), style::Reset)
|
||||
format!("{}{}", style::Reset, color::Fg(color::Magenta))
|
||||
},
|
||||
'e' => { // Error titles
|
||||
format!("{}{}", color::Fg(color::Red), style::Bold)
|
||||
@ -81,6 +87,9 @@ fn format_map_full(c: char) -> Option<String> {
|
||||
'c' => { // Console text
|
||||
format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
||||
},
|
||||
'p' => { // Input prompt (how ==> is styled)
|
||||
format!("{}{}", color::Fg(color::Blue), style::Bold)
|
||||
},
|
||||
's' => { // Repeat prompt (how => is styled)
|
||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||
},
|
||||
@ -88,17 +97,24 @@ fn format_map_full(c: char) -> Option<String> {
|
||||
format!("{}{}", color::Fg(color::Green), style::Bold)
|
||||
},
|
||||
|
||||
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
pub fn format_map(c: char, context: &Context) -> Option<String> {
|
||||
match context.config.term_color_type {
|
||||
0 => format_map_none(c),
|
||||
1 => format_map_ansi(c),
|
||||
2 => format_map_full(c),
|
||||
_ => unreachable!("Invalid term_color_type")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl FormattedText {
|
||||
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||
write!(
|
||||
stdout,
|
||||
"\r\n",
|
||||
)?;
|
||||
write!(stdout, "\n")?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@ -145,12 +161,7 @@ impl FormattedText {
|
||||
match (a, b) {
|
||||
(c, ']') => { // Normal text
|
||||
|
||||
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")
|
||||
};
|
||||
let q = format_map(c, context);
|
||||
|
||||
if q.is_some() {
|
||||
s.push_str(&q.unwrap());
|
||||
@ -173,7 +184,7 @@ impl FormattedText {
|
||||
}
|
||||
}
|
||||
|
||||
write!(stdout, "{}", s)?;
|
||||
write!(stdout, "\r{}", s)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,23 @@ use super::super::LineLocation;
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
pub enum Expression {
|
||||
// Meaning of `LineLocation`:
|
||||
//
|
||||
// For Variables, Constants, Quantities, Tuples:
|
||||
// If this expression was parsed, LineLocation is what part of the prompt was parsed to get this expression
|
||||
// If this expression is the result of a calculation, LineLocaion is the sum of the LineLocations of
|
||||
// all expressions used to make it. In other words, it points to the part of the prompt that was evaluated
|
||||
// to get this new expression.
|
||||
//
|
||||
// For Operators:
|
||||
// Linelocation points to the operator's position in the prompt.
|
||||
// If this is a function, it points to the function name.
|
||||
// If this is `+`, `!`, or etc, it points to that character.
|
||||
// Operator arguments are NOT included in this linelocation.
|
||||
//
|
||||
//
|
||||
// All the above rules are implemented when parsing and evaluating expressions.
|
||||
|
||||
Variable(LineLocation, String),
|
||||
Quantity(LineLocation, Quantity),
|
||||
Constant(LineLocation, Constant),
|
||||
|
@ -21,8 +21,10 @@ pub fn parse(
|
||||
context: &Context, s: &String
|
||||
) -> Result<Expression, (LineLocation, DaisyError)> {
|
||||
|
||||
let expressions = stage::tokenize(context, s);
|
||||
let (_, expressions) = stage::find_subs(expressions);
|
||||
let mut expressions = stage::tokenize(context, s);
|
||||
if context.config.enable_substituion {
|
||||
(_, expressions) = stage::find_subs(expressions);
|
||||
}
|
||||
let g = stage::groupify(context, expressions)?;
|
||||
let g = stage::treeify(context, g)?;
|
||||
|
||||
@ -33,7 +35,14 @@ pub fn parse_no_context(s: &String) -> Result<Expression, (LineLocation, DaisyEr
|
||||
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 {
|
||||
if !context.config.enable_substituion { return s.clone(); }
|
||||
let (_, s) = substitute_cursor(context, s, s.chars().count());
|
||||
return s;
|
||||
}
|
||||
@ -43,34 +52,45 @@ pub fn substitute_cursor(
|
||||
s: &String, // The string to substitute
|
||||
c: usize // Location of the cursor right now
|
||||
) -> (
|
||||
usize, // Location of cursor in substituted string
|
||||
usize, // New cursor
|
||||
String // String with substitutions
|
||||
) {
|
||||
|
||||
if !context.config.enable_substituion { return (c, s.clone()); }
|
||||
if s == "" { return (c, s.clone()) }
|
||||
|
||||
|
||||
let mut new_s = s.clone();
|
||||
|
||||
let l = s.chars().count();
|
||||
let expressions = stage::tokenize(context, s);
|
||||
let (mut subs, _) = stage::find_subs(expressions);
|
||||
let mut new_c = l - c;
|
||||
let mut new_c = c.clone();
|
||||
|
||||
while subs.len() > 0 {
|
||||
let r = subs.pop_back().unwrap();
|
||||
// Apply substitutions in reverse order
|
||||
// r is the current substitution: (linelocation, string)
|
||||
let r = subs.pop_back().unwrap();
|
||||
|
||||
if { // Don't substitute if our cursor is inside the substitution
|
||||
c >= r.0.pos &&
|
||||
c < r.0.pos+r.0.len
|
||||
} { continue; }
|
||||
|
||||
if c < r.0.pos {
|
||||
let ct = r.1.chars().count();
|
||||
if ct >= r.0.len {
|
||||
if new_c >= ct - r.0.len {
|
||||
new_c += ct - r.0.len
|
||||
}
|
||||
} else {
|
||||
new_c -= r.0.len - ct
|
||||
// If this substitution is before our cursor,
|
||||
// we need to adjust our cursor's position.
|
||||
if c > r.0.pos {
|
||||
let c_o = r.0.len; // Old length
|
||||
let c_n = r.1.chars().count(); // New length
|
||||
|
||||
if c_n > c_o {
|
||||
// Move cursor right by difference
|
||||
new_c += c_n - c_o;
|
||||
|
||||
} else if c_n < c_o {
|
||||
// Move cursor left by difference
|
||||
if new_c >= c_o - c_n {
|
||||
new_c -= c_o - c_n;
|
||||
} else { new_c = 0; }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,11 +6,69 @@ use super::super::{
|
||||
};
|
||||
|
||||
|
||||
fn sub_string(s: &str) -> Option<&'static str> {
|
||||
let r = match s {
|
||||
|
||||
/* Only found in operator tokens */
|
||||
|
||||
"*" => "×",
|
||||
"/" => "÷",
|
||||
"sqrt" => "√",
|
||||
"rt" => "√",
|
||||
|
||||
|
||||
|
||||
/* Only found in word tokens */
|
||||
|
||||
// Greek letters
|
||||
"alpha" => "α",
|
||||
"beta" => "β",
|
||||
"gamma" => "γ",
|
||||
"delta" => "δ",
|
||||
"epsilon" => "ε",
|
||||
"zeta" => "ζ",
|
||||
"eta" => "η",
|
||||
"theta" => "θ",
|
||||
//"iota" => {Some("ι")}, // looks just like i
|
||||
//"kappa" => {Some("κ")}, // looks just like k
|
||||
"lambda" => "λ",
|
||||
"mu" => "μ",
|
||||
//"nu" => {Some("ν")}, // looks just like v
|
||||
"xi" => "ξ",
|
||||
//"omicron" => {Some("ο")}, // looks exactly like o
|
||||
"pi" => "π",
|
||||
"rho" => "ρ",
|
||||
"sigma" => "σ",
|
||||
"tau" => "τ",
|
||||
//"upsilon" => {Some("υ")}, // looks just like u
|
||||
"phi" => "φ",
|
||||
"chi" => "χ",
|
||||
//"psi" => {Some("ψ")}, Conflict with pound / square inch
|
||||
"omega" => "ω",
|
||||
|
||||
// Constants
|
||||
"epsilon_zero" => "ε₀",
|
||||
"eps_zero" => "ε₀",
|
||||
"g_zero" => "g₀",
|
||||
"mu_zero" => "μ₀",
|
||||
"h_bar" => "ℏ",
|
||||
|
||||
// Misc
|
||||
"deg" => "°",
|
||||
|
||||
_ => { return None; }
|
||||
};
|
||||
return Some(r);
|
||||
}
|
||||
|
||||
|
||||
// Finds substitutions in an array of tokens.
|
||||
// Returns new token array and substitution list.
|
||||
pub fn find_subs(
|
||||
mut g: VecDeque<Token>,
|
||||
) -> (
|
||||
VecDeque<(LineLocation, String)>,
|
||||
VecDeque<Token>
|
||||
VecDeque<(LineLocation, String)>, // List of substrings to replace (in order)
|
||||
VecDeque<Token> // New token array, with updated strings and linelocations
|
||||
) {
|
||||
|
||||
// Array of replacements
|
||||
@ -24,62 +82,19 @@ pub fn find_subs(
|
||||
while g.len() > 0 {
|
||||
let mut t = g.pop_front().unwrap();
|
||||
|
||||
|
||||
let target: Option<&str> = match &mut t {
|
||||
Token::Operator(_, s) => {
|
||||
let target = match &s[..] {
|
||||
"*" => {Some("×")},
|
||||
"/" => {Some("÷")},
|
||||
"sqrt" => {Some("√")},
|
||||
"rt" => {Some("√")},
|
||||
_ => {None}
|
||||
};
|
||||
let target = sub_string(s);
|
||||
|
||||
// Update token contents too.
|
||||
// This makes sure that errors also contain the updated text.
|
||||
// This makes errors and printouts use the updated string.
|
||||
if target.is_some() { *s = String::from(target.unwrap()); }
|
||||
target
|
||||
},
|
||||
|
||||
Token::Word(_, s) => {
|
||||
let target = match &s[..] {
|
||||
// Greek letters
|
||||
"alpha" => {Some("α")},
|
||||
"beta" => {Some("β")},
|
||||
"gamma" => {Some("γ")},
|
||||
"delta" => {Some("δ")},
|
||||
"epsilon" => {Some("ε")},
|
||||
"zeta" => {Some("ζ")},
|
||||
"eta" => {Some("η")},
|
||||
"theta" => {Some("θ")},
|
||||
//"iota" => {Some("ι")},
|
||||
//"kappa" => {Some("κ")},
|
||||
"lambda" => {Some("λ")},
|
||||
"mu" => {Some("μ")},
|
||||
//"nu" => {Some("ν")},
|
||||
"xi" => {Some("ξ")},
|
||||
//"omicron" => {Some("ο")},
|
||||
"pi" => {Some("π")},
|
||||
"rho" => {Some("ρ")},
|
||||
"sigma" => {Some("σ")},
|
||||
"tau" => {Some("τ")},
|
||||
//"upsilon" => {Some("υ")},
|
||||
"phi" => {Some("φ")},
|
||||
"chi" => {Some("χ")},
|
||||
//"psi" => {Some("ψ")}, Conflict with pound / square inch
|
||||
"omega" => {Some("ω")},
|
||||
|
||||
// Constants
|
||||
"epsilon_zero" => {Some("ε₀")},
|
||||
"eps_zero" => {Some("ε₀")},
|
||||
"g_zero" => {Some("g₀")},
|
||||
"mu_zero" => {Some("μ₀")},
|
||||
"h_bar" => {Some("ℏ")},
|
||||
|
||||
// Misc
|
||||
"deg" => {Some("°")}
|
||||
_ => {None}
|
||||
};
|
||||
|
||||
let target = sub_string(s);
|
||||
if target.is_some() { *s = String::from(target.unwrap()); }
|
||||
target
|
||||
},
|
||||
@ -88,7 +103,7 @@ pub fn find_subs(
|
||||
};
|
||||
|
||||
if target.is_none() {
|
||||
// Even if nothing changed, we need to update token location
|
||||
// Even if nothing changed, we need to update the new token's linelocation
|
||||
let l = t.get_mut_linelocation();
|
||||
*l = LineLocation{pos: l.pos - offset, len: l.len};
|
||||
} else {
|
||||
|
@ -244,7 +244,7 @@ impl Rem<FloatBase> for FloatBase {
|
||||
(!modulus.fract().unwrap().is_zero())
|
||||
} { panic!() }
|
||||
|
||||
FloatBase{val : self.val.fract() % modulus.val.fract()}
|
||||
FloatBase{val : self.val.trunc() % modulus.val.trunc()}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -186,7 +186,7 @@ impl Scalar {
|
||||
pub fn is_nan(&self) -> bool {
|
||||
match self {
|
||||
Scalar::Float {v} => {v.val.is_nan()},
|
||||
Scalar::Rational {..} => {panic!()}
|
||||
Scalar::Rational {..} => {false}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -162,6 +162,7 @@ fn operators() {
|
||||
|
||||
good_expr("2", "6/3");
|
||||
good_expr("2", "5%3");
|
||||
good_expr("4", "2^5 mod 7");
|
||||
good_expr("8", "5+3");
|
||||
good_expr("64", "4^3");
|
||||
good_expr("64", "4 ^ 3");
|
||||
@ -184,6 +185,7 @@ fn operators() {
|
||||
bad_expr("1e5!");
|
||||
bad_expr("0^(-1)");
|
||||
bad_expr("pi!");
|
||||
bad_expr("2.5 mod 8");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Reference in New Issue
Block a user