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(
stdout: &mut RawTerminal<std::io::Stdout>,
s: &String,
context: &Context
context: &mut Context
) -> Result<parser::Token, ()> {
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout).unwrap();
@ -118,6 +118,16 @@ fn do_expression(
style::Reset,
color::Fg(color::Reset),
).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) {
command::do_command(&mut stdout, &in_str)?;
} 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); }
}

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.
loop {
// 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.
// 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()))
} {
if !g.is_quantity() {
*g = match g {
Token::Quantity(_) => panic!(),
Token::Constant(c) => { evaluate(&c.value(), context).unwrap() },
Token::Variable(s) => { context.get_variable(&s).unwrap() },
Token::Operator(Operator::Function(f), v) => { eval_function(&f, &v)? },
let new = match g {
Token::Quantity(_) => None,
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
coords.pop();
@ -56,6 +60,13 @@ pub fn evaluate(t: &Token, context: &mut Context) -> Result<Token, EvalError> {
} else {
// Move down the tree
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,
TooBig,
ZeroDivision,
IncompatibleUnit
IncompatibleUnit,
BadDefineName
}

View File

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

View File

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

View File

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

View File

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

View File

@ -11,7 +11,8 @@ use super::Function;
#[derive(Clone)]
#[repr(usize)]
pub enum Operator {
ModuloLong = 0, // Mod invoked with "mod"
Define = 0, // Variable and function definition
ModuloLong, // Mod invoked with "mod"
DivideLong,
UnitConvert,
Subtract,
@ -68,6 +69,7 @@ impl Operator {
}
return match s {
"=" => {Some( Operator::Define )},
"+" => {Some( Operator::Add )},
"-" => {Some( Operator::Subtract )},
"neg" => {Some( Operator::Negative )},
@ -166,6 +168,7 @@ impl Operator {
| Operator::Power
| Operator::ModuloLong
| Operator::UnitConvert
| Operator::Define
=> { Token::Operator(self, args) },
}
}
@ -201,6 +204,14 @@ impl Operator {
Operator::Divide |
Operator::Subtract => { panic!() }
Operator::Define => {
return format!(
"{} = {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
);
},
Operator::Flip => {
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)]
pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Token>> {
match self {
@ -46,14 +53,34 @@ impl Token {
}
#[inline(always)]
pub fn get_at_coords<'a>(g: &'a mut Token, coords: &Vec<usize>) -> &'a mut Token {
let mut h = &mut *g;
for t in coords.iter() {
let inner = h.get_args_mut().unwrap();
h = &mut inner[*t];
pub fn get_args(&self) -> Option<&VecDeque<Token>> {
match self {
Token::Operator(_, ref a) => Some(a),
_ => None
}
}
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();
return match evaluate(&g, &Context::new()) {
return match evaluate(&g, &mut Context::new()) {
Ok(x) => Ok(x.to_string_outer()),
Err(_) => Err(())
};