Added define operator

This commit is contained in:
2023-06-14 20:18:28 -07:00
parent b6343db0d6
commit 6bd7043971
10 changed files with 124 additions and 60 deletions

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