This commit is contained in:
2023-06-17 19:08:05 -07:00
parent 1495dcd561
commit 73049eeec4
7 changed files with 197 additions and 250 deletions

View File

@ -26,7 +26,7 @@ pub fn find_subs(
let mut t = g.pop_back().unwrap();
let target: Option<&str> = match &mut t {
Token::PreOperator(_, s) => {
Token::Operator(_, s) => {
let target = match &s[..] {
"*" => {Some("×")},
"/" => {Some("÷")},
@ -41,7 +41,7 @@ pub fn find_subs(
target
},
Token::PreWord(_, s) => {
Token::Word(_, s) => {
let target = match &s[..] {
// Greek letters
"alpha" => {Some("α")},

View File

@ -19,10 +19,10 @@ fn lookback_signs(
if i == 0 {
let a: Token = g.remove(i).unwrap();
match &a {
Token::PreOperator(l,o)
Token::Operator(l,o)
=> {
if o == "-" {
g.insert(i, Token::PreOperator(*l, String::from("neg")));
g.insert(i, Token::Operator(*l, String::from("neg")));
} else if o == "+" {
continue; // We should not increment i if we remove a token
} else {g.insert(i, a);}
@ -35,7 +35,7 @@ fn lookback_signs(
let b: Token = g.remove(i-1).unwrap();
match (&a, &b) {
(Token::PreOperator(_, sa), Token::PreOperator(l,sb))
(Token::Operator(_, sa), Token::Operator(l,sb))
=> {
if {
let o = Operator::from_string(sa);
@ -47,7 +47,7 @@ fn lookback_signs(
)
} {
if sb == "-" {
g.insert(i-1, Token::PreOperator(*l, String::from("neg")));
g.insert(i-1, Token::Operator(*l, String::from("neg")));
g.insert(i-1, a);
} else if sb == "+" {
g.insert(i-1, a);
@ -71,7 +71,7 @@ fn lookback_signs(
let b: Token = g.remove(i-1).unwrap();
match (&a, &b) {
(Token::PreOperator(_,sa), Token::PreOperator(_,sb))
(Token::Operator(_,sa), Token::Operator(_,sb))
=> {
if !((sa == "neg") && (sb == "neg")) {
g.insert(i-1, b);
@ -108,19 +108,19 @@ fn lookback(
match (&a, &b) {
// Insert ImplicitMultiply
(Token::PreGroup(_,_), Token::PreGroup(l ,_))
| (Token::PreGroup(_,_), Token::PreQuantity(l,_))
| (Token::PreQuantity(_,_), Token::PreGroup(l,_))
| (Token::PreGroup(_,_), Token::PreWord(l,_))
| (Token::PreWord(_,_), Token::PreGroup(l,_))
| (Token::PreQuantity(_,_), Token::PreWord(l,_))
| (Token::PreWord(_,_), Token::PreQuantity(l,_))
| (Token::PreWord(_,_), Token::PreWord(l,_))
(Token::Group(_,_), Token::Group(l ,_))
| (Token::Group(_,_), Token::Quantity(l,_))
| (Token::Quantity(_,_), Token::Group(l,_))
| (Token::Group(_,_), Token::Word(l,_))
| (Token::Word(_,_), Token::Group(l,_))
| (Token::Quantity(_,_), Token::Word(l,_))
| (Token::Word(_,_), Token::Quantity(l,_))
| (Token::Word(_,_), Token::Word(l,_))
=> {
let loc = LineLocation{pos: l.pos-1, len: 0};
g.insert(i-1, b);
g.insert(i-1, Token::PreOperator(
g.insert(i-1, Token::Operator(
loc,
String::from("i*")
));
@ -128,9 +128,9 @@ fn lookback(
},
// Insert implicit multiplications for right-unary operators
(Token::PreQuantity(_,_), Token::PreOperator(l,s))
| (Token::PreGroup(_,_), Token::PreOperator(l,s))
| (Token::PreWord(_,_), Token::PreOperator(l,s))
(Token::Quantity(_,_), Token::Operator(l,s))
| (Token::Group(_,_), Token::Operator(l,s))
| (Token::Word(_,_), Token::Operator(l,s))
=> {
let o = Operator::from_string(s);
let loc = LineLocation{pos: l.pos-1, len: 0};
@ -139,7 +139,7 @@ fn lookback(
if o.is_some() {
let o = o.unwrap();
if (!o.is_binary()) && (!o.is_left_associative()) {
g.insert(i-1, Token::PreOperator(
g.insert(i-1, Token::Operator(
loc,
String::from("i*")
));
@ -149,9 +149,9 @@ fn lookback(
},
// Insert implicit multiplications for left-unary operators.
(Token::PreOperator(_,s), Token::PreQuantity(l,_))
| (Token::PreOperator(_,s), Token::PreGroup(l,_))
| (Token::PreOperator(_,s), Token::PreWord(l,_))
(Token::Operator(_,s), Token::Quantity(l,_))
| (Token::Operator(_,s), Token::Group(l,_))
| (Token::Operator(_,s), Token::Word(l,_))
=> {
let o = Operator::from_string(s);
let loc = LineLocation{pos: l.pos-1, len: 0};
@ -160,7 +160,7 @@ fn lookback(
if o.is_some() {
let o = o.unwrap();
if (!o.is_binary()) && o.is_left_associative() {
g.insert(i-1, Token::PreOperator(
g.insert(i-1, Token::Operator(
loc,
String::from("i*")
));
@ -170,7 +170,7 @@ fn lookback(
},
// The following are syntax errors
(Token::PreQuantity(la,_), Token::PreQuantity(lb,_))
(Token::Quantity(la,_), Token::Quantity(lb,_))
=> {
return Err((
LineLocation{pos: la.pos, len: lb.pos - la.pos + lb.len},
@ -206,12 +206,12 @@ pub fn groupify(
let (l_now, v_now) = levels.last_mut().unwrap();
match t {
Token::PreGroupStart(l) => {
Token::GroupStart(l) => {
levels.push((l, VecDeque::with_capacity(8)));
i_level += 1;
},
Token::PreGroupEnd(l) => {
Token::GroupEnd(l) => {
let l = LineLocation {
pos: l_now.pos,
len: l.len + l.pos - l_now.pos
@ -226,7 +226,7 @@ pub fn groupify(
let (_, v_now) = levels.last_mut().unwrap();
lookback(&mut v)?;
v_now.push_back(Token::PreGroup(l, v));
v_now.push_back(Token::Group(l, v));
},
_ => {
@ -252,12 +252,12 @@ pub fn groupify(
if v.len() == 0 { return Err((l, ParserError::EmptyGroup)) }
lookback(&mut v)?;
v_now.push_back(Token::PreGroup(l, v));
v_now.push_back(Token::Group(l, v));
}
let (_, mut v) = levels.pop().unwrap();
lookback(&mut v)?;
return Ok(Token::PreGroup(LineLocation{pos:0, len:0}, v));
return Ok(Token::Group(LineLocation{pos:0, len:0}, v));
}

View File

@ -14,11 +14,11 @@ fn push_token(g: &mut VecDeque<Token>, t: Option<Token>, stop_i: usize) {
let mut t = t.unwrap();
match t {
Token::PreGroupStart(ref mut l)
| Token::PreGroupEnd(ref mut l)
| Token::PreOperator(ref mut l, _)
| Token::PreQuantity(ref mut l, _)
| Token::PreWord(ref mut l, _)
Token::GroupStart(ref mut l)
| Token::GroupEnd(ref mut l)
| Token::Operator(ref mut l, _)
| Token::Quantity(ref mut l, _)
| Token::Word(ref mut l, _)
=> {
*l = LineLocation{
pos: l.pos,
@ -26,7 +26,7 @@ fn push_token(g: &mut VecDeque<Token>, t: Option<Token>, stop_i: usize) {
};
},
Token::PreGroup(_,_)
Token::Group(_,_)
| Token::Container(_)
=> panic!()
};
@ -34,14 +34,14 @@ fn push_token(g: &mut VecDeque<Token>, t: Option<Token>, stop_i: usize) {
// `2e` isn't exponential notation, it's 2*e.
// If a number ends in `e`, disconnect the `e` and make it a word.
if let Token::PreQuantity(l, s) = &t {
if let Token::Quantity(l, s) = &t {
let last = &s[s.len()-1..];
if last == "e" {
g.push_back(Token::PreQuantity(
g.push_back(Token::Quantity(
LineLocation { pos: l.pos, len: l.len-1 },
String::from(&s[0..s.len()-1])
));
g.push_back(Token::PreWord(
g.push_back(Token::Word(
LineLocation { pos: l.pos + l.len - 1, len: 1 },
String::from("e")
));
@ -51,9 +51,9 @@ fn push_token(g: &mut VecDeque<Token>, t: Option<Token>, stop_i: usize) {
}
// Some operators are written as words.
if let Token::PreWord(l, s) = &t {
if let Token::Word(l, s) = &t {
if Operator::from_string(s).is_some() {
t = Token::PreOperator(*l, s.clone());
t = Token::Operator(*l, s.clone());
}
}
@ -74,7 +74,7 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
match &mut t {
// If we're already building a number,
// append.
Some(Token::PreQuantity(_, val)) => {
Some(Token::Quantity(_, val)) => {
val.push(if c == ',' {'.'} else {c});
},
@ -82,7 +82,7 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
// previous token and start one.
_ => {
push_token(&mut g, t, i);
t = Some(Token::PreQuantity(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::Quantity(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
},
@ -91,12 +91,12 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
// Can be both a word or a number.
'e' => {
match &mut t {
Some(Token::PreWord(_, val)) => { val.push(c); },
Some(Token::PreQuantity(_, val)) => { val.push(c); },
Some(Token::Word(_, val)) => { val.push(c); },
Some(Token::Quantity(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i);
t = Some(Token::PreWord(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::Word(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
}
@ -106,7 +106,7 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
// or it can specify a negative exponent.
'-' | '+' => {
match &mut t {
Some(Token::PreQuantity(_, val)) => {
Some(Token::Quantity(_, val)) => {
if &val[val.len()-1..] == "e" {
// If the current number ends in an `e`,
// this negative specifies a negative exponent
@ -116,7 +116,7 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
// Otherwise, end the number.
// We probably have a subtraction.
push_token(&mut g, t, i);
t = Some(Token::PreOperator(
t = Some(Token::Operator(
LineLocation{pos: i, len: 1},
String::from(c)
));
@ -126,7 +126,7 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
// This may be a negative or a subtraction
_ => {
push_token(&mut g, t, i);
t = Some(Token::PreOperator(
t = Some(Token::Operator(
LineLocation{pos: i, len: 1},
String::from(c)
));
@ -139,10 +139,10 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
'^'|'!'|'%'|'='
=> {
match &mut t {
Some(Token::PreOperator(_, val)) => { val.push(c); },
Some(Token::Operator(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i);
t = Some(Token::PreOperator(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::Operator(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
},
@ -150,11 +150,11 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
// Group
'(' => {
push_token(&mut g, t, i);
t = Some(Token::PreGroupStart(LineLocation{pos: i, len: 0}));
t = Some(Token::GroupStart(LineLocation{pos: i, len: 0}));
},
')' => {
push_token(&mut g, t, i);
t = Some(Token::PreGroupEnd(LineLocation{pos: i, len: 0}));
t = Some(Token::GroupEnd(LineLocation{pos: i, len: 0}));
},
// Space. Basic seperator.
@ -166,11 +166,11 @@ pub fn tokenize(input: &String) -> VecDeque<Token> {
// Word
_ => {
match &mut t {
Some(Token::PreWord(_, val)) => { val.push(c); },
Some(Token::Word(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i);
t = Some(Token::PreWord(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::Word(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
}

View File

@ -20,7 +20,7 @@ fn treeify_binary(
if i == 0 {
// This binary operator is at the end of an expression.
let l = match this {
Token::PreOperator(l, _) => l,
Token::Operator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -32,7 +32,7 @@ fn treeify_binary(
&g_inner[i-1]
} else {
let l = match this {
Token::PreOperator(l, _) => l,
Token::Operator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -44,7 +44,7 @@ fn treeify_binary(
&g_inner[i+1]
} else {
let l = match this {
Token::PreOperator(l, _) => l,
Token::Operator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -55,7 +55,7 @@ fn treeify_binary(
if let Token::PreOperator(l, s) = left {
if let Token::Operator(l, s) = left {
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
let o = o.unwrap();
@ -74,7 +74,7 @@ fn treeify_binary(
}
}
if let Token::PreOperator(l, s) = right {
if let Token::Operator(l, s) = right {
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
let o = o.unwrap();
@ -96,7 +96,7 @@ fn treeify_binary(
// This operator
let this_op = {
let Token::PreOperator(l, s) = this else {panic!()};
let Token::Operator(l, s) = this else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap()
@ -104,14 +104,14 @@ fn treeify_binary(
// The operators contesting our arguments
let left_op = if i > 1 {
let Token::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let Token::Operator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
} else { None };
let right_op = if i < g_inner.len()-2 {
let Token::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let Token::Operator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
@ -127,11 +127,11 @@ fn treeify_binary(
let this_pre = g_inner.remove(i-1).unwrap();
let right_pre = g_inner.remove(i-1).unwrap();
let left: Expression; let right: Expression;
if let Token::PreGroup(_, _) = right_pre { right = treeify(right_pre, context)?; } else {right = right_pre.to_expression(context)?;}
if let Token::PreGroup(_, _) = left_pre { left = treeify(left_pre, context)?; } else {left = left_pre.to_expression(context)?;}
if let Token::Group(_, _) = right_pre { right = treeify(right_pre, context)?; } else {right = right_pre.to_expression(context)?;}
if let Token::Group(_, _) = left_pre { left = treeify(left_pre, context)?; } else {left = left_pre.to_expression(context)?;}
let o = {
let Token::PreOperator(_, s) = this_pre else {panic!()};
let Token::Operator(_, s) = this_pre else {panic!()};
let o = Operator::from_string(&s);
if o.is_none() { panic!() }
o.unwrap()
@ -141,7 +141,7 @@ fn treeify_binary(
new_token_args.push_back(left);
new_token_args.push_back(right);
g_inner.insert(i-1, Token::Container(o.into_expression(new_token_args)));
g_inner.insert(i-1, Token::Container(Expression::Operator(o, new_token_args)));
return Ok(true);
} else {
@ -164,7 +164,7 @@ fn treeify_unary(
&g_inner[i-1]
} else {
let l = match this {
Token::PreOperator(l, _) => l,
Token::Operator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -176,7 +176,7 @@ fn treeify_unary(
&g_inner[i+1]
} else {
let l = match this {
Token::PreOperator(l, _) => l,
Token::Operator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -194,7 +194,7 @@ fn treeify_unary(
}
if prev.is_some() {
if let Token::PreOperator(_,_) = prev.unwrap() {
if let Token::Operator(_,_) = prev.unwrap() {
} else {
return Err((
*this.get_line_location(),
@ -203,7 +203,7 @@ fn treeify_unary(
}
}
if let Token::PreOperator(l, _) = next {
if let Token::Operator(l, _) = next {
let tl = *this.get_line_location();
return Err((
LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
@ -214,7 +214,7 @@ fn treeify_unary(
// This operator
let this_op = {
let Token::PreOperator(l, s) = this else {panic!()};
let Token::Operator(l, s) = this else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap()
@ -223,14 +223,14 @@ fn treeify_unary(
// The operator contesting our argument
let next_op = if left_associative {
if i > 1 {
let Token::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let Token::Operator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
} else { None }
} else {
if i < g_inner.len()-2 {
let Token::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let Token::Operator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
@ -245,10 +245,10 @@ fn treeify_unary(
} else {
next_pre = g_inner.remove(i).unwrap();
}
if let Token::PreGroup(_, _) = next_pre { next = treeify(next_pre, context)?; } else { next = next_pre.to_expression(context)? }
if let Token::Group(_, _) = next_pre { next = treeify(next_pre, context)?; } else { next = next_pre.to_expression(context)? }
let o = {
let Token::PreOperator(_, s) = this_pre else {panic!()};
let Token::Operator(_, s) = this_pre else {panic!()};
let o = Operator::from_string(&s);
if o.is_none() { panic!() }
o.unwrap()
@ -258,9 +258,9 @@ fn treeify_unary(
new_token_args.push_back(next);
if left_associative {
g_inner.insert(i-1, Token::Container(o.into_expression(new_token_args)));
g_inner.insert(i-1, Token::Container(Expression::Operator(o, new_token_args)));
} else {
g_inner.insert(i, Token::Container(o.into_expression(new_token_args)));
g_inner.insert(i, Token::Container(Expression::Operator(o, new_token_args)));
}
return Ok(true);
@ -279,7 +279,7 @@ pub fn treeify(
) -> Result<Expression, (LineLocation, ParserError)> {
let g_inner: &mut VecDeque<Token> = match g {
Token::PreGroup(_, ref mut x) => x,
Token::Group(_, ref mut x) => x,
_ => panic!()
};
@ -297,10 +297,10 @@ pub fn treeify(
let i = j as usize;
// Convert preoperators
// Convert operators
// If not an operator, move on.
let this_op = match &g_inner[i] {
Token::PreOperator(l, s) => {
Token::Operator(l, s) => {
let o = Operator::from_string(&s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap()
@ -342,10 +342,10 @@ pub fn treeify(
let g = g_inner.pop_front().unwrap();
return match g {
// Catch edge cases
Token::PreOperator(l, _) => {
Token::Operator(l, _) => {
Err((l, ParserError::Syntax))
},
Token::PreGroup(_,_) => {
Token::Group(_,_) => {
treeify(g, context)
},