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

@ -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(());