From 3e1fa97ce840b0383022e75c15c355ee694c1a1c Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 25 Mar 2023 09:54:07 -0700 Subject: [PATCH] Cleanup --- src/main.rs | 5 ++--- src/parser.rs | 22 +++++++--------------- src/parser/groupify.rs | 14 ++++---------- src/parser/tokenize.rs | 5 ++--- src/parser/treeify.rs | 20 ++++++++------------ 5 files changed, 23 insertions(+), 43 deletions(-) diff --git a/src/main.rs b/src/main.rs index e37c381..4c60520 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,12 +90,11 @@ fn main() -> Result<(), std::io::Error> { } else { panic!(); } }, Err((l, e)) => { - let LineLocation{pos, len} = l; write!( stdout, "{}{}{} {e:?}{}\r\n", color::Fg(color::Red), - " ".repeat(pos + 4), - "^".repeat(len), + " ".repeat(l.pos + 4), + "^".repeat(l.len), color::Fg(color::Reset), )?; } diff --git a/src/parser.rs b/src/parser.rs index d4d61ec..7ce0c88 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -118,9 +118,8 @@ impl Token { for i in v.iter() { let j = i.as_number(); if let Token::Number(l, v) = j { - let LineLocation{pos, len} = l; - if new_pos == 0 {new_pos = pos}; - new_len = new_len + len; + if new_pos == 0 {new_pos = l.pos}; + new_len = new_len + l.len; sum += v; } else { panic!(); @@ -140,9 +139,8 @@ impl Token { for i in v.iter() { let j = i.as_number(); if let Token::Number(l, v) = j { - let LineLocation{pos, len} = l; - if new_pos == 0 {new_pos = pos}; - new_len = new_len + len; + if new_pos == 0 {new_pos = l.pos}; + new_len = new_len + l.len; prod *= v; } else { panic!(); @@ -162,10 +160,8 @@ impl Token { if let Token::Number(la, va) = a { if let Token::Number(lb, vb) = b { - let LineLocation{pos: posa, ..} = la; - let LineLocation{pos: posb, len: lenb} = lb; Token::Number( - LineLocation { pos: posa, len: posb - posa + lenb }, + LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len }, va/vb ) } else { panic!(); } @@ -179,10 +175,8 @@ impl Token { if let Token::Number(la, va) = a { if let Token::Number(lb, vb) = b { - let LineLocation{pos: posa, ..} = la; - let LineLocation{pos: posb, len: lenb} = lb; Token::Number( - LineLocation { pos: posa, len: posb - posa + lenb }, + LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len }, va%vb ) } else { panic!(); } @@ -196,10 +190,8 @@ impl Token { if let Token::Number(la, va) = a { if let Token::Number(lb, vb) = b { - let LineLocation{pos: posa, ..} = la; - let LineLocation{pos: posb, len: lenb} = lb; Token::Number( - LineLocation { pos: posa, len: posb - posa + lenb }, + LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len }, va.powf(vb) ) } else { panic!(); } diff --git a/src/parser/groupify.rs b/src/parser/groupify.rs index 80668b0..45058a4 100644 --- a/src/parser/groupify.rs +++ b/src/parser/groupify.rs @@ -31,9 +31,8 @@ fn lookback( (Token::Constant(_,_,_), Token::Constant(l,_,_)) => { g.push_back(a); - let LineLocation { pos: i, .. } = l; g.push_back(Token::PreOperator( - LineLocation{pos: i-1, len: 0}, + LineLocation{pos: l.pos-1, len: 0}, Operator::ImplicitMultiply )); g.push_back(b); @@ -42,10 +41,8 @@ fn lookback( // The following are syntax errors (Token::Number(la, _), Token::Number(lb,_)) => { - let LineLocation { pos: posa, .. } = *la; - let LineLocation { pos: posb, len: lenb } = *lb; return Err(( - LineLocation{pos: posa, len: posb - posa + lenb}, + LineLocation{pos: la.pos, len: lb.pos - la.pos + lb.len}, ParserError::Syntax )); } @@ -106,12 +103,9 @@ pub fn p_groupify(mut g: VecDeque) -> Result { - let LineLocation{pos: posa, ..} = *l_now; - let LineLocation{pos: posb, len: lenb} = l; - let l = LineLocation { - pos: posa, - len: lenb + posb - posa + pos: l_now.pos, + len: l.len + l.pos - l_now.pos }; if i_level == 0 { diff --git a/src/parser/tokenize.rs b/src/parser/tokenize.rs index fef7524..a08debb 100644 --- a/src/parser/tokenize.rs +++ b/src/parser/tokenize.rs @@ -15,10 +15,9 @@ fn update_line_location(mut t: Token, stop_i: usize) -> Token { Token::PreNumber(ref mut l, _) | Token::PreWord(ref mut l, _) => { - let LineLocation{pos, .. } = l; *l = LineLocation{ - pos: *pos, - len: stop_i - *pos, + pos: l.pos, + len: stop_i - l.pos, }; }, _ => panic!() diff --git a/src/parser/treeify.rs b/src/parser/treeify.rs index 565c230..67c90af 100644 --- a/src/parser/treeify.rs +++ b/src/parser/treeify.rs @@ -75,10 +75,9 @@ fn treeify_binary( => { // Binary and right-unary operators cannot // follow a binary operator. - let LineLocation { pos: posa, .. } = *this.get_line_location(); - let LineLocation { pos: posb, len: lenb } = *l; + let tl = *this.get_line_location(); return Err(( - LineLocation{pos: posa, len: posb - posa + lenb}, + LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len}, ParserError::Syntax )); }, @@ -172,10 +171,9 @@ fn treeify_unaryleft( => { // Binary and right-unary operators cannot // follow a binary operator. - let LineLocation { pos: posa, .. } = *this.get_line_location(); - let LineLocation { pos: posb, len: lenb } = *l; + let tl = *this.get_line_location(); return Err(( - LineLocation{pos: posa, len: posb - posa + lenb}, + LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len}, ParserError::Syntax )); }, @@ -261,10 +259,9 @@ fn treeify_unaryright( match o { // Left unary operators Operator::Negative => { - let LineLocation { pos: posa, .. } = *this.get_line_location(); - let LineLocation { pos: posb, len: lenb } = *l; + let tl = *this.get_line_location(); return Err(( - LineLocation{pos: posa, len: posb - posa + lenb}, + LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len}, ParserError::Syntax )); }, @@ -279,10 +276,9 @@ fn treeify_unaryright( } if let Token::PreOperator(l, _) = left { - let LineLocation { pos: posa, .. } = *this.get_line_location(); - let LineLocation { pos: posb, len: lenb } = *l; + let tl = *this.get_line_location(); return Err(( - LineLocation{pos: posa, len: posb - posa + lenb}, + LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len}, ParserError::Syntax ));