Added support for unary +

pull/2/head
Mark 2023-04-06 09:51:14 -07:00
parent 0e693ffcd6
commit f112326620
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 15 additions and 10 deletions

View File

@ -8,7 +8,6 @@ This is nowhere near complete. Stay tuned.
# TODO # TODO
## Before For 1.0 release ## Before For 1.0 release
- `+` as a unary operator
- Commands - Commands
- Documentation (usage) - Documentation (usage)
- Documentation (comments) - Documentation (comments)

View File

@ -7,13 +7,12 @@ use crate::parser::ParserError;
use crate::tokens::Operator; use crate::tokens::Operator;
// Turn `-` into `neg`, fn lookback_signs(
// put `neg`s inside numbers.
fn lookback_negatives(
g: &mut VecDeque<PreToken> g: &mut VecDeque<PreToken>
) -> Result<(), (LineLocation, ParserError)> { ) -> Result<(), (LineLocation, ParserError)> {
// Convert `-` operators to `neg` operators // Convert `-` operators to `neg` operators
// Delete `+`s that mean "positive" instead of "add"
let mut i: usize = 0; let mut i: usize = 0;
while i < g.len() { while i < g.len() {
if i == 0 { if i == 0 {
@ -23,7 +22,7 @@ fn lookback_negatives(
=> { => {
if o == "-" { if o == "-" {
g.insert(i, PreToken::PreOperator(*l, String::from("neg"))); g.insert(i, PreToken::PreOperator(*l, String::from("neg")));
} else { g.insert(i, a); } } else if o != "+" { g.insert(i, a); }
}, },
_ => { g.insert(i, a); } _ => { g.insert(i, a); }
}; };
@ -46,6 +45,10 @@ fn lookback_negatives(
} { } {
g.insert(i-1, PreToken::PreOperator(*l, String::from("neg"))); g.insert(i-1, PreToken::PreOperator(*l, String::from("neg")));
g.insert(i-1, a); g.insert(i-1, a);
} else if sb == "+" {
g.insert(i-1, a);
i -= 1; // g is now shorter, we don't need to advance i.
// This nullifies the i += 1 at the end of the loop.
} else { g.insert(i-1, b); g.insert(i-1, a); } } else { g.insert(i-1, b); g.insert(i-1, a); }
}, },
@ -104,7 +107,7 @@ fn lookback(
g: &mut VecDeque<PreToken> g: &mut VecDeque<PreToken>
) -> Result<(), (LineLocation, ParserError)> { ) -> Result<(), (LineLocation, ParserError)> {
lookback_negatives(g)?; lookback_signs(g)?;
let mut i: usize = 0; let mut i: usize = 0;

View File

@ -155,8 +155,10 @@ pub fn parse(
let tokens = tokenize(s); let tokens = tokenize(s);
let (_, tokens) = find_subs(tokens); let (_, tokens) = find_subs(tokens);
let g = groupify(tokens)?; let g = groupify(tokens)?;
println!("{:#?}", g);
let g = treeify(g)?; let g = treeify(g)?;
return Ok(g); return Ok(g);
} }

View File

@ -103,7 +103,7 @@ pub(in crate::parser) fn tokenize(input: &String) -> VecDeque<PreToken> {
// The minus sign also needs special treatment. // The minus sign also needs special treatment.
// It can be the `neg` operator, the `minus` operator, // It can be the `neg` operator, the `minus` operator,
// or it can specify a negative exponent. // or it can specify a negative exponent.
'-' => { '-' | '+' => {
match &mut t { match &mut t {
Some(PreToken::PreNumber(_, val)) => { Some(PreToken::PreNumber(_, val)) => {
if &val[val.len()-1..] == "e" { if &val[val.len()-1..] == "e" {
@ -117,16 +117,17 @@ pub(in crate::parser) fn tokenize(input: &String) -> VecDeque<PreToken> {
push_token(&mut g, t, i); push_token(&mut g, t, i);
t = Some(PreToken::PreOperator( t = Some(PreToken::PreOperator(
LineLocation{pos: i, len: 1}, LineLocation{pos: i, len: 1},
String::from("-") String::from(c)
)); ));
} }
}, },
// This may be a negative or a subtraction
_ => { _ => {
push_token(&mut g, t, i); push_token(&mut g, t, i);
t = Some(PreToken::PreOperator( t = Some(PreToken::PreOperator(
LineLocation{pos: i, len: 1}, LineLocation{pos: i, len: 1},
String::from("-") String::from(c)
)); ));
} }
}; };
@ -134,7 +135,7 @@ pub(in crate::parser) fn tokenize(input: &String) -> VecDeque<PreToken> {
// Operator // Operator
'*'|'×'|'/'|'÷'| '*'|'×'|'/'|'÷'|
'+'|'^'|'!'|'%' '^'|'!'|'%'
=> { => {
match &mut t { match &mut t {
Some(PreToken::PreOperator(_, val)) => { val.push(c); }, Some(PreToken::PreOperator(_, val)) => { val.push(c); },