Reworked lookback

pull/2/head
Mark 2023-04-01 16:43:58 -07:00
parent e0210a8f09
commit bb200548f1
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 134 additions and 111 deletions

View File

@ -10,21 +10,27 @@ use crate::tokens::Operator;
fn lookback( fn lookback(
g: &mut VecDeque<PreToken> g: &mut VecDeque<PreToken>
) -> Result<(), (LineLocation, ParserError)> { ) -> Result<(), (LineLocation, ParserError)> {
if g.len() == 1 {
let a: PreToken = g.pop_back().unwrap(); for i in 0..g.len() {
println!("{i} {:?}", g);
if i < 1 {
let a: PreToken = g.remove(i).unwrap();
match &a { match &a {
PreToken::PreOperator(l,o) PreToken::PreOperator(l,o)
=> { => {
if o == "-" { if o == "-" {
g.push_back(PreToken::PreOperator(*l, String::from("neg"))); g.insert(i, PreToken::PreOperator(*l, String::from("neg")));
} else { g.push_back(a); } } else { g.insert(i, a); }
}, },
_ => { g.push_back(a); } _ => { g.insert(i, a); }
}; };
} else { } else {
let b: PreToken = g.pop_back().unwrap(); let a: PreToken = g.remove(i-1).unwrap();
let a: PreToken = g.pop_back().unwrap(); let b: PreToken = g.remove(i-1).unwrap();
match (&a, &b) { match (&a, &b) {
// Insert ImplicitMultiply // Insert ImplicitMultiply
@ -37,12 +43,14 @@ fn lookback(
| (PreToken::PreWord(_,_), PreToken::PreNumber(l,_)) | (PreToken::PreWord(_,_), PreToken::PreNumber(l,_))
| (PreToken::PreWord(_,_), PreToken::PreWord(l,_)) | (PreToken::PreWord(_,_), PreToken::PreWord(l,_))
=> { => {
g.push_back(a); let loc = LineLocation{pos: l.pos-1, len: 0};
g.push_back(PreToken::PreOperator(
LineLocation{pos: l.pos-1, len: 0}, g.insert(i-1, b);
g.insert(i-1, PreToken::PreOperator(
loc,
String::from("i*") String::from("i*")
)); ));
g.push_back(b); g.insert(i-1, a);
}, },
// The following are syntax errors // The following are syntax errors
@ -65,46 +73,56 @@ fn lookback(
!o.as_ref().unwrap().is_left_associative() !o.as_ref().unwrap().is_left_associative()
) )
} { } {
g.push_back(a); g.insert(i-1, PreToken::PreOperator(*l, String::from("neg")));
g.push_back(PreToken::PreOperator(*l, String::from("neg"))); g.insert(i-1, a);
} else { g.push_back(a); g.push_back(b); } } else { g.insert(i-1, b); g.insert(i-1, a); }
} }
// Insert implicit multiplications for unary operators // Insert implicit multiplications for right-unary operators
(PreToken::PreNumber(_,_), PreToken::PreOperator(l,s)) (PreToken::PreNumber(_,_), PreToken::PreOperator(l,s))
| (PreToken::PreGroup(_,_), PreToken::PreOperator(l,s)) | (PreToken::PreGroup(_,_), PreToken::PreOperator(l,s))
| (PreToken::PreWord(_,_), PreToken::PreOperator(l,s)) | (PreToken::PreWord(_,_), PreToken::PreOperator(l,s))
=> { => {
let o = Operator::from_string(s); let o = Operator::from_string(s);
g.push_back(a); let loc = LineLocation{pos: l.pos-1, len: 0};
g.insert(i-1, b);
if o.is_some() { if o.is_some() {
let o = o.unwrap(); let o = o.unwrap();
if (!o.is_binary()) && (!o.is_left_associative()) { if (!o.is_binary()) && (!o.is_left_associative()) {
g.push_back(PreToken::PreOperator( g.insert(i-1, PreToken::PreOperator(
LineLocation{pos: l.pos-1, len: 0}, loc,
String::from("i*")
));
} else if (!o.is_binary()) && o.is_left_associative() {
g.insert(i-1, PreToken::PreOperator(
loc,
String::from("i*") String::from("i*")
)); ));
} }
} }
g.push_back(b); g.insert(i-1, a);
}, },
// Insert implicit multiplications for left-unary operators.
(PreToken::PreOperator(_,s), PreToken::PreNumber(l,_)) (PreToken::PreOperator(_,s), PreToken::PreNumber(l,_))
| (PreToken::PreOperator(_,s), PreToken::PreGroup(l,_)) | (PreToken::PreOperator(_,s), PreToken::PreGroup(l,_))
| (PreToken::PreOperator(_,s), PreToken::PreWord(l,_)) | (PreToken::PreOperator(_,s), PreToken::PreWord(l,_))
=> { => {
let o = Operator::from_string(s); let o = Operator::from_string(s);
g.push_back(a); let loc = LineLocation{pos: l.pos-1, len: 0};
g.insert(i-1, b);
if o.is_some() { if o.is_some() {
let o = o.unwrap(); let o = o.unwrap();
if (!o.is_binary()) && o.is_left_associative() { if (!o.is_binary()) && o.is_left_associative() {
g.push_back(PreToken::PreOperator( g.insert(i-1, PreToken::PreOperator(
LineLocation{pos: l.pos-1, len: 0}, loc,
String::from("i*") String::from("i*")
)); ));
} }
} }
g.push_back(b); g.insert(i-1, a);
}, },
// This shouldn't ever happen. // This shouldn't ever happen.
@ -117,6 +135,10 @@ fn lookback(
=> panic!() => panic!()
} }
}; };
}
println!("{:?}", g);
return Ok(()); return Ok(());
} }
@ -155,16 +177,15 @@ pub(in crate::parser) fn groupify(
i_level -= 1; i_level -= 1;
let (_, v) = levels.pop().unwrap(); let (_, mut v) = levels.pop().unwrap();
let (_, v_now) = levels.last_mut().unwrap(); let (_, v_now) = levels.last_mut().unwrap();
lookback(&mut v)?;
v_now.push_back(PreToken::PreGroup(l, v)); v_now.push_back(PreToken::PreGroup(l, v));
lookback(v_now)?;
}, },
_ => { _ => {
v_now.push_back(t); v_now.push_back(t);
lookback(v_now)?;
} }
} }
@ -180,16 +201,18 @@ pub(in crate::parser) fn groupify(
// Auto-close parenthesis // Auto-close parenthesis
while levels.len() != 1 { while levels.len() != 1 {
let (l, v) = levels.pop().unwrap(); let (l, mut v) = levels.pop().unwrap();
let (_, v_now) = levels.last_mut().unwrap(); let (_, v_now) = levels.last_mut().unwrap();
if v.len() == 0 { return Err((l, ParserError::EmptyGroup)) } if v.len() == 0 { return Err((l, ParserError::EmptyGroup)) }
lookback(&mut v)?;
v_now.push_back(PreToken::PreGroup(l, v)); v_now.push_back(PreToken::PreGroup(l, v));
lookback(v_now)?;
} }
let (_, v) = levels.pop().unwrap(); let (_, mut v) = levels.pop().unwrap();
lookback(&mut v)?;
return Ok(PreToken::PreGroup(LineLocation{pos:0, len:0}, v)); return Ok(PreToken::PreGroup(LineLocation{pos:0, len:0}, v));
} }