mirror of https://github.com/rm-dr/daisy
Added support for unary +
parent
0e693ffcd6
commit
f112326620
|
@ -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)
|
||||
|
|
|
@ -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<PreToken>
|
||||
) -> 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<PreToken>
|
||||
) -> Result<(), (LineLocation, ParserError)> {
|
||||
|
||||
lookback_negatives(g)?;
|
||||
lookback_signs(g)?;
|
||||
|
||||
|
||||
let mut i: usize = 0;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ pub(in crate::parser) fn tokenize(input: &String) -> VecDeque<PreToken> {
|
|||
// 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<PreToken> {
|
|||
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<PreToken> {
|
|||
|
||||
// Operator
|
||||
'*'|'×'|'/'|'÷'|
|
||||
'+'|'^'|'!'|'%'
|
||||
'^'|'!'|'%'
|
||||
=> {
|
||||
match &mut t {
|
||||
Some(PreToken::PreOperator(_, val)) => { val.push(c); },
|
||||
|
|
Loading…
Reference in New Issue