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 (comments)
- Units, unit conversion
- Fix negation: what should `-3^4` or `-x^2` be?
- Social media banner
- Releases
## General

View File

@ -59,43 +59,29 @@ fn lookback_signs(
i += 1;
}
// Make negative numbers negative numbers.
// We don't need `neg` operators in this case.
// Note that we read the token array right-to-left, since `neg` is right-associative.
let mut i: usize = g.len();
loop {
if i > 1 { i -= 1; } else { break; }
// Delete consecutive `neg`s
let mut i: usize = 1;
while i < g.len() {
let a: PreToken = g.remove(i-1).unwrap();
let b: PreToken = g.remove(i-1).unwrap();
match (&a, &b) {
(PreToken::PreOperator(la,sa), PreToken::PreNumber(lb,sb))
(PreToken::PreOperator(_,sa), PreToken::PreOperator(_,sb))
=> {
if sa == "neg" {
let first = &sb[0..1];
if first == "-" {
// Already negative. Remove the old one.
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); }
if !((sa == "neg") && (sb == "neg")) {
g.insert(i-1, b);
g.insert(i-1, a);
i += 1;
}
},
_ => { g.insert(i-1, b); g.insert(i-1, a); }
_ => {
g.insert(i-1, b);
g.insert(i-1, a);
i += 1;
}
}
}
return Ok(());