Fixed negation

pull/2/head
Mark 2023-04-07 08:46:44 -07:00
parent 2d42d7ea80
commit aeacd8746d
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 20 additions and 36 deletions

View File

@ -7,8 +7,6 @@ Roadmap for fixes and features.
- Documentation (usage) - Documentation (usage)
- Documentation (comments) - Documentation (comments)
- Units, unit conversion - Units, unit conversion
- Fix negation: what should `-3^4` or `-x^2` be?
- Social media banner
- Releases - Releases
## General ## General

View File

@ -30,13 +30,13 @@ fn lookback_signs(
} else { } else {
let a: PreToken = g.remove(i-1).unwrap(); let a: PreToken = g.remove(i-1).unwrap();
let b: PreToken = g.remove(i-1).unwrap(); let b: PreToken = g.remove(i-1).unwrap();
match (&a, &b) { match (&a, &b) {
(PreToken::PreOperator(_, sa), PreToken::PreOperator(l,sb)) (PreToken::PreOperator(_, sa), PreToken::PreOperator(l,sb))
=> { => {
if sb == "-" && { if sb == "-" && {
let o = Operator::from_string(sa); let o = Operator::from_string(sa);
o.is_some() && o.is_some() &&
( (
o.as_ref().unwrap().is_binary() || o.as_ref().unwrap().is_binary() ||
@ -59,43 +59,29 @@ fn lookback_signs(
i += 1; i += 1;
} }
// Make negative numbers negative numbers. // Delete consecutive `neg`s
// We don't need `neg` operators in this case. let mut i: usize = 1;
// Note that we read the token array right-to-left, since `neg` is right-associative. while i < g.len() {
let mut i: usize = g.len();
loop {
if i > 1 { i -= 1; } else { break; }
let a: PreToken = g.remove(i-1).unwrap(); let a: PreToken = g.remove(i-1).unwrap();
let b: PreToken = g.remove(i-1).unwrap(); let b: PreToken = g.remove(i-1).unwrap();
match (&a, &b) { match (&a, &b) {
(PreToken::PreOperator(la,sa), PreToken::PreNumber(lb,sb)) (PreToken::PreOperator(_,sa), PreToken::PreOperator(_,sb))
=> { => {
if sa == "neg" { if !((sa == "neg") && (sb == "neg")) {
let first = &sb[0..1]; g.insert(i-1, b);
if first == "-" { g.insert(i-1, a);
// Already negative. Remove the old one. i += 1;
g.insert(i-1, }
PreToken::PreNumber(
LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len },
format!("{}", &sb[1..])
)
);
} else {
// Not negative yet. Add one.
g.insert(i-1,
PreToken::PreNumber(
LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len },
format!("-{sb}")
)
);
}
} else { g.insert(i-1, b); g.insert(i-1, a); }
}, },
_ => { g.insert(i-1, b); g.insert(i-1, a); } _ => {
g.insert(i-1, b);
g.insert(i-1, a);
i += 1;
}
} }
} }
return Ok(()); return Ok(());
@ -115,7 +101,7 @@ fn lookback(
if i >= 1 { if i >= 1 {
let a: PreToken = g.remove(i-1).unwrap(); let a: PreToken = g.remove(i-1).unwrap();
let b: PreToken = g.remove(i-1).unwrap(); let b: PreToken = g.remove(i-1).unwrap();
match (&a, &b) { match (&a, &b) {
// Insert ImplicitMultiply // Insert ImplicitMultiply
(PreToken::PreGroup(_,_), PreToken::PreGroup(l ,_)) (PreToken::PreGroup(_,_), PreToken::PreGroup(l ,_))
@ -136,7 +122,7 @@ fn lookback(
)); ));
g.insert(i-1, a); g.insert(i-1, a);
}, },
// Insert implicit multiplications for right-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))
@ -157,7 +143,7 @@ fn lookback(
} }
g.insert(i-1, a); g.insert(i-1, a);
}, },
// Insert implicit multiplications for left-unary operators. // 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,_))