From f11232662039d8e0b6119210788c313479bfd760 Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 6 Apr 2023 09:51:14 -0700 Subject: [PATCH] Added support for unary + --- README.md | 1 - src/parser/groupify.rs | 13 ++++++++----- src/parser/mod.rs | 2 ++ src/parser/tokenize.rs | 9 +++++---- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3f91d1e..071df4d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ This is nowhere near complete. Stay tuned. # TODO ## Before For 1.0 release - - `+` as a unary operator - Commands - Documentation (usage) - Documentation (comments) diff --git a/src/parser/groupify.rs b/src/parser/groupify.rs index 17bfdd0..2fa4d76 100644 --- a/src/parser/groupify.rs +++ b/src/parser/groupify.rs @@ -7,13 +7,12 @@ use crate::parser::ParserError; use crate::tokens::Operator; -// Turn `-` into `neg`, -// put `neg`s inside numbers. -fn lookback_negatives( +fn lookback_signs( g: &mut VecDeque ) -> Result<(), (LineLocation, ParserError)> { // Convert `-` operators to `neg` operators + // Delete `+`s that mean "positive" instead of "add" let mut i: usize = 0; while i < g.len() { if i == 0 { @@ -23,7 +22,7 @@ fn lookback_negatives( => { if o == "-" { 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); } }; @@ -46,6 +45,10 @@ fn lookback_negatives( } { g.insert(i-1, PreToken::PreOperator(*l, String::from("neg"))); 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); } }, @@ -104,7 +107,7 @@ fn lookback( g: &mut VecDeque ) -> Result<(), (LineLocation, ParserError)> { - lookback_negatives(g)?; + lookback_signs(g)?; let mut i: usize = 0; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index eb8b404..7635733 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -155,8 +155,10 @@ pub fn parse( let tokens = tokenize(s); let (_, tokens) = find_subs(tokens); let g = groupify(tokens)?; + println!("{:#?}", g); let g = treeify(g)?; + return Ok(g); } diff --git a/src/parser/tokenize.rs b/src/parser/tokenize.rs index 789676d..969f861 100644 --- a/src/parser/tokenize.rs +++ b/src/parser/tokenize.rs @@ -103,7 +103,7 @@ pub(in crate::parser) fn tokenize(input: &String) -> VecDeque { // The minus sign also needs special treatment. // It can be the `neg` operator, the `minus` operator, // or it can specify a negative exponent. - '-' => { + '-' | '+' => { match &mut t { Some(PreToken::PreNumber(_, val)) => { if &val[val.len()-1..] == "e" { @@ -117,16 +117,17 @@ pub(in crate::parser) fn tokenize(input: &String) -> VecDeque { push_token(&mut g, t, i); t = Some(PreToken::PreOperator( LineLocation{pos: i, len: 1}, - String::from("-") + String::from(c) )); } }, + // This may be a negative or a subtraction _ => { push_token(&mut g, t, i); t = Some(PreToken::PreOperator( LineLocation{pos: i, len: 1}, - String::from("-") + String::from(c) )); } }; @@ -134,7 +135,7 @@ pub(in crate::parser) fn tokenize(input: &String) -> VecDeque { // Operator '*'|'×'|'/'|'÷'| - '+'|'^'|'!'|'%' + '^'|'!'|'%' => { match &mut t { Some(PreToken::PreOperator(_, val)) => { val.push(c); },