Added define operator

pull/2/head
Mark 2023-06-14 20:18:28 -07:00
parent b6343db0d6
commit 6bd7043971
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
10 changed files with 124 additions and 60 deletions

View File

@ -24,7 +24,7 @@ use crate::context::Context;
fn do_expression( fn do_expression(
stdout: &mut RawTerminal<std::io::Stdout>, stdout: &mut RawTerminal<std::io::Stdout>,
s: &String, s: &String,
context: &Context context: &mut Context
) -> Result<parser::Token, ()> { ) -> Result<parser::Token, ()> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout).unwrap(); RawTerminal::suspend_raw_mode(&stdout).unwrap();
@ -118,6 +118,16 @@ fn do_expression(
style::Reset, style::Reset,
color::Fg(color::Reset), color::Fg(color::Reset),
).unwrap(); ).unwrap();
},
Err(EvalError::BadDefineName) => {
write!(
stdout, "\n {}{}Evaluation Error: {}Invalid variable name{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
).unwrap();
} }
} }
} }
@ -167,7 +177,7 @@ pub fn main() -> Result<(), std::io::Error> {
} else if command::is_command(&in_str) { } else if command::is_command(&in_str) {
command::do_command(&mut stdout, &in_str)?; command::do_command(&mut stdout, &in_str)?;
} else { } else {
let r = do_expression(&mut stdout, &in_str, &context); let r = do_expression(&mut stdout, &in_str, &mut context);
if let Ok(t) = r { context.push_hist(t); } if let Ok(t) = r { context.push_hist(t); }
} }

View File

@ -26,7 +26,9 @@ pub fn evaluate(t: &Token, context: &mut Context) -> Result<Token, EvalError> {
// Exits when we finish parsing the root node. // Exits when we finish parsing the root node.
loop { loop {
// Current position in the tree // Current position in the tree
let g = root.get_at_coords_mut(&coords[0 .. coords.len() - 1]); let g = root.get_at_coords_mut(
&coords[0 .. coords.len() - 1]
).unwrap();
// "Move up" step. // "Move up" step.
// We move up if we're at a leaf or if we're out of children to move down to. // We move up if we're at a leaf or if we're out of children to move down to.
@ -36,16 +38,18 @@ pub fn evaluate(t: &Token, context: &mut Context) -> Result<Token, EvalError> {
(coords.len() != 0 && (*coords.last().unwrap() >= g.get_args().unwrap().len())) (coords.len() != 0 && (*coords.last().unwrap() >= g.get_args().unwrap().len()))
} { } {
if !g.is_quantity() {
*g = match g {
Token::Quantity(_) => panic!(),
Token::Constant(c) => { evaluate(&c.value(), context).unwrap() }, let new = match g {
Token::Variable(s) => { context.get_variable(&s).unwrap() }, Token::Quantity(_) => None,
Token::Operator(Operator::Function(f), v) => { eval_function(&f, &v)? },
Token::Operator(o, v) => { eval_operator(&o, &v, context)? }, Token::Constant(c) => { Some(evaluate(&c.value(), context).unwrap()) },
} Token::Variable(s) => { context.get_variable(&s) },
} Token::Operator(Operator::Function(f), v) => { Some(eval_function(&f, &v)?) },
Token::Operator(o, v) => { eval_operator(&o, &v, context)? },
};
if new.is_some() { *g = new.unwrap()}
// Move up the tree // Move up the tree
coords.pop(); coords.pop();
@ -56,6 +60,13 @@ pub fn evaluate(t: &Token, context: &mut Context) -> Result<Token, EvalError> {
} else { } else {
// Move down the tree // Move down the tree
coords.push(0); coords.push(0);
let n = root.get_at_coords(&coords[..]);
if let Some(n) = n {
if let Token::Operator(Operator::Define, _) = n {
*coords.last_mut().unwrap() += 1;
}
}
} }
} }

View File

@ -9,5 +9,6 @@ pub enum EvalError {
BadMath, BadMath,
TooBig, TooBig,
ZeroDivision, ZeroDivision,
IncompatibleUnit IncompatibleUnit,
BadDefineName
} }

View File

@ -4,8 +4,9 @@ use crate::quantity::Quantity;
use crate::parser::Operator; use crate::parser::Operator;
use crate::parser::Token; use crate::parser::Token;
use super::EvalError; use super::EvalError;
use crate::context::Context;
pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, EvalError> { pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Context) -> Result<Option<Token>, EvalError> {
match op { match op {
// Handled seperately in evaluate.rs // Handled seperately in evaluate.rs
@ -19,32 +20,42 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, Eva
Operator::DivideLong | Operator::DivideLong |
Operator::Subtract => { panic!() } Operator::Subtract => { panic!() }
Operator::Define => {
if args.len() != 2 { panic!() };
let b = &args[1];
if let Token::Variable(s) = &args[0] {
context.push_var(s.clone(), b.clone());
return Ok(Some(b.clone()));
} else { return Err(EvalError::BadDefineName); }
},
Operator::Negative => { Operator::Negative => {
if args.len() != 1 {panic!()}; if args.len() != 1 { panic!() };
let args = &args[0]; let args = &args[0];
if let Token::Quantity(v) = args { if let Token::Quantity(v) = args {
return Ok(Token::Quantity(-v.clone())); return Ok(Some(Token::Quantity(-v.clone())));
} else { panic!(); } } else { return Ok(None); }
}, },
Operator::Flip => { Operator::Flip => {
if args.len() != 1 {panic!()}; if args.len() != 1 { panic!() };
let args = &args[0]; let args = &args[0];
if let Token::Quantity(v) = args { if let Token::Quantity(v) = args {
if v.is_zero() { return Err(EvalError::ZeroDivision); } if v.is_zero() { return Err(EvalError::ZeroDivision); }
return Ok(Token::Quantity( return Ok(Some(Token::Quantity(
Quantity::new_rational(1f64).unwrap()/v.clone() Quantity::new_rational(1f64).unwrap()/v.clone()
)); )));
} else { panic!(); } } else { return Ok(None); }
}, },
Operator::Add => { Operator::Add => {
let mut sum: Quantity; let mut sum: Quantity;
if let Token::Quantity(s) = &args[0] { if let Token::Quantity(s) = &args[0] {
sum = s.clone(); sum = s.clone();
} else {panic!()}; } else { return Ok(None); };
let mut i: usize = 1; let mut i: usize = 1;
while i < args.len() { while i < args.len() {
@ -56,12 +67,10 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, Eva
} }
sum += v.clone(); sum += v.clone();
} else { } else { return Ok(None); }
panic!();
}
i += 1; i += 1;
} }
return Ok(Token::Quantity(sum)); return Ok(Some(Token::Quantity(sum)));
}, },
Operator::Multiply => { Operator::Multiply => {
@ -70,16 +79,14 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, Eva
let j = i; let j = i;
if let Token::Quantity(v) = j { if let Token::Quantity(v) = j {
prod *= v.clone(); prod *= v.clone();
} else { } else { return Ok(None); }
panic!();
}
} }
return Ok(Token::Quantity(prod)); return Ok(Some(Token::Quantity(prod)));
}, },
Operator::ModuloLong Operator::ModuloLong
| Operator::Modulo => { | Operator::Modulo => {
if args.len() != 2 {panic!()}; if args.len() != 2 { panic!() };
let a = &args[0]; let a = &args[0];
let b = &args[1]; let b = &args[1];
@ -94,14 +101,14 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, Eva
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); } if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); }
if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); } if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); }
return Ok(Token::Quantity(va.clone() % vb.clone())); return Ok(Some(Token::Quantity(va.clone() % vb.clone())));
} else { panic!(); } } else { return Ok(None); }
} else { panic!(); } } else { return Ok(None); }
}, },
Operator::UnitConvert Operator::UnitConvert
=> { => {
if args.len() != 2 {panic!()}; if args.len() != 2 { panic!() };
let a = &args[0]; let a = &args[0];
let b = &args[1]; let b = &args[1];
@ -111,9 +118,9 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, Eva
if n.is_none() { if n.is_none() {
return Err(EvalError::IncompatibleUnit); return Err(EvalError::IncompatibleUnit);
} }
return Ok(Token::Quantity(n.unwrap())); return Ok(Some(Token::Quantity(n.unwrap())));
} else { panic!(); } } else { return Ok(None); }
} else { panic!(); } } else { return Ok(None); }
}, },
Operator::Power => { Operator::Power => {
@ -134,9 +141,9 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, Eva
let p = va.pow(vb.clone()); let p = va.pow(vb.clone());
if p.is_nan() {return Err(EvalError::BadMath);} if p.is_nan() {return Err(EvalError::BadMath);}
return Ok(Token::Quantity(p)); return Ok(Some(Token::Quantity(p)));
} else { panic!(); } } else { return Ok(None); }
} else { panic!(); } } else { return Ok(None); }
}, },
Operator::Factorial => { Operator::Factorial => {
@ -159,8 +166,8 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>) -> Result<Token, Eva
u = u - Quantity::new_rational(1f64).unwrap(); u = u - Quantity::new_rational(1f64).unwrap();
} }
return Ok(Token::Quantity(prod)); return Ok(Some(Token::Quantity(prod)));
} else { panic!(); } } else { return Ok(None); }
} }
}; };
} }

View File

@ -15,7 +15,6 @@ pub enum ParserError {
ExtraCloseParen, ExtraCloseParen,
EmptyGroup, EmptyGroup,
Syntax, Syntax,
Undefined(String),
BadNumber BadNumber
} }
@ -34,9 +33,6 @@ impl ToString for ParserError {
ParserError::Syntax => { ParserError::Syntax => {
String::from("Syntax") String::from("Syntax")
}, },
ParserError::Undefined(s) => {
format!("\"{s}\" isn't defined")
},
ParserError::BadNumber => { ParserError::BadNumber => {
String::from("Invalid number") String::from("Invalid number")
} }

View File

@ -68,13 +68,15 @@ impl PreToken {
} }
let r = Quantity::new_rational_from_string(&s); let r = Quantity::new_rational_from_string(&s);
if r.is_none() { if r.is_none() {
return Err((l, ParserError::BadNumber)) return Err((l, ParserError::BadNumber))
} }
return Ok(Token::Quantity(r.unwrap())); return Ok(Token::Quantity(r.unwrap()));
}, },
PreToken::PreWord(l, s) => { PreToken::PreWord(_l, s) => {
let c = Constant::from_string(&s); let c = Constant::from_string(&s);
if c.is_some() { return Ok(Token::Constant(c.unwrap())); } if c.is_some() { return Ok(Token::Constant(c.unwrap())); }
@ -84,8 +86,7 @@ impl PreToken {
let c = context.get_variable(&s); let c = context.get_variable(&s);
if c.is_some() { return Ok(Token::Variable(s)); } if c.is_some() { return Ok(Token::Variable(s)); }
return Ok(Token::Variable(s));
return Err((l, ParserError::Undefined(s)));
} }
PreToken::Container(v) => { return Ok(v); } PreToken::Container(v) => { return Ok(v); }

View File

@ -136,7 +136,7 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// Operator // Operator
'*'|'×'|'/'|'÷'| '*'|'×'|'/'|'÷'|
'^'|'!'|'%' '^'|'!'|'%'|'='
=> { => {
match &mut t { match &mut t {
Some(PreToken::PreOperator(_, val)) => { val.push(c); }, Some(PreToken::PreOperator(_, val)) => { val.push(c); },

View File

@ -11,7 +11,8 @@ use super::Function;
#[derive(Clone)] #[derive(Clone)]
#[repr(usize)] #[repr(usize)]
pub enum Operator { pub enum Operator {
ModuloLong = 0, // Mod invoked with "mod" Define = 0, // Variable and function definition
ModuloLong, // Mod invoked with "mod"
DivideLong, DivideLong,
UnitConvert, UnitConvert,
Subtract, Subtract,
@ -68,6 +69,7 @@ impl Operator {
} }
return match s { return match s {
"=" => {Some( Operator::Define )},
"+" => {Some( Operator::Add )}, "+" => {Some( Operator::Add )},
"-" => {Some( Operator::Subtract )}, "-" => {Some( Operator::Subtract )},
"neg" => {Some( Operator::Negative )}, "neg" => {Some( Operator::Negative )},
@ -166,6 +168,7 @@ impl Operator {
| Operator::Power | Operator::Power
| Operator::ModuloLong | Operator::ModuloLong
| Operator::UnitConvert | Operator::UnitConvert
| Operator::Define
=> { Token::Operator(self, args) }, => { Token::Operator(self, args) },
} }
} }
@ -201,6 +204,14 @@ impl Operator {
Operator::Divide | Operator::Divide |
Operator::Subtract => { panic!() } Operator::Subtract => { panic!() }
Operator::Define => {
return format!(
"{} = {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
);
},
Operator::Flip => { Operator::Flip => {
return format!("{}⁻¹", Operator::Divide.add_parens_to_arg(&args[0])); return format!("{}⁻¹", Operator::Divide.add_parens_to_arg(&args[0]));
}, },

View File

@ -37,6 +37,13 @@ impl Token {
} }
} }
pub fn is_quantity(&self) -> bool {
match self {
Token::Quantity(_) => true,
_ => false
}
}
#[inline(always)] #[inline(always)]
pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Token>> { pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Token>> {
match self { match self {
@ -46,14 +53,34 @@ impl Token {
} }
#[inline(always)] #[inline(always)]
pub fn get_at_coords<'a>(g: &'a mut Token, coords: &Vec<usize>) -> &'a mut Token { pub fn get_args(&self) -> Option<&VecDeque<Token>> {
let mut h = &mut *g; match self {
Token::Operator(_, ref a) => Some(a),
for t in coords.iter() { _ => None
let inner = h.get_args_mut().unwrap();
h = &mut inner[*t];
} }
}
return h; #[inline(always)]
pub fn get_at_coords<'a, 'b, I>(&'a self, coords: I) -> Option<&'a Token>
where I: IntoIterator<Item = &'b usize> + Sized {
let mut g = self;
for t in coords.into_iter() {
let args = g.get_args();
let Some(args) = args else { return None; };
g = &args[*t];
}
return Some(g);
}
#[inline(always)]
pub fn get_at_coords_mut<'a, 'b, I>(&'a mut self, coords: I) -> Option<&'a mut Token>
where I: IntoIterator<Item = &'b usize> + Sized {
let mut g = self;
for t in coords.into_iter() {
let args = g.get_args_mut();
let Some(args) = args else { return None; };
g = &mut args[*t];
}
return Some(g);
} }
} }

View File

@ -10,7 +10,7 @@ fn eval_to_str(s: &str) -> Result<String, ()> {
}; };
//let out_str = g.print(); //let out_str = g.print();
return match evaluate(&g, &Context::new()) { return match evaluate(&g, &mut Context::new()) {
Ok(x) => Ok(x.to_string_outer()), Ok(x) => Ok(x.to_string_outer()),
Err(_) => Err(()) Err(_) => Err(())
}; };