pull/2/head
Mark 2023-03-25 09:54:07 -07:00
parent 2bdf983909
commit 3e1fa97ce8
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
5 changed files with 23 additions and 43 deletions

View File

@ -90,12 +90,11 @@ fn main() -> Result<(), std::io::Error> {
} else { panic!(); } } else { panic!(); }
}, },
Err((l, e)) => { Err((l, e)) => {
let LineLocation{pos, len} = l;
write!( write!(
stdout, "{}{}{} {e:?}{}\r\n", stdout, "{}{}{} {e:?}{}\r\n",
color::Fg(color::Red), color::Fg(color::Red),
" ".repeat(pos + 4), " ".repeat(l.pos + 4),
"^".repeat(len), "^".repeat(l.len),
color::Fg(color::Reset), color::Fg(color::Reset),
)?; )?;
} }

View File

@ -118,9 +118,8 @@ impl Token {
for i in v.iter() { for i in v.iter() {
let j = i.as_number(); let j = i.as_number();
if let Token::Number(l, v) = j { if let Token::Number(l, v) = j {
let LineLocation{pos, len} = l; if new_pos == 0 {new_pos = l.pos};
if new_pos == 0 {new_pos = pos}; new_len = new_len + l.len;
new_len = new_len + len;
sum += v; sum += v;
} else { } else {
panic!(); panic!();
@ -140,9 +139,8 @@ impl Token {
for i in v.iter() { for i in v.iter() {
let j = i.as_number(); let j = i.as_number();
if let Token::Number(l, v) = j { if let Token::Number(l, v) = j {
let LineLocation{pos, len} = l; if new_pos == 0 {new_pos = l.pos};
if new_pos == 0 {new_pos = pos}; new_len = new_len + l.len;
new_len = new_len + len;
prod *= v; prod *= v;
} else { } else {
panic!(); panic!();
@ -162,10 +160,8 @@ impl Token {
if let Token::Number(la, va) = a { if let Token::Number(la, va) = a {
if let Token::Number(lb, vb) = b { if let Token::Number(lb, vb) = b {
let LineLocation{pos: posa, ..} = la;
let LineLocation{pos: posb, len: lenb} = lb;
Token::Number( Token::Number(
LineLocation { pos: posa, len: posb - posa + lenb }, LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len },
va/vb va/vb
) )
} else { panic!(); } } else { panic!(); }
@ -179,10 +175,8 @@ impl Token {
if let Token::Number(la, va) = a { if let Token::Number(la, va) = a {
if let Token::Number(lb, vb) = b { if let Token::Number(lb, vb) = b {
let LineLocation{pos: posa, ..} = la;
let LineLocation{pos: posb, len: lenb} = lb;
Token::Number( Token::Number(
LineLocation { pos: posa, len: posb - posa + lenb }, LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len },
va%vb va%vb
) )
} else { panic!(); } } else { panic!(); }
@ -196,10 +190,8 @@ impl Token {
if let Token::Number(la, va) = a { if let Token::Number(la, va) = a {
if let Token::Number(lb, vb) = b { if let Token::Number(lb, vb) = b {
let LineLocation{pos: posa, ..} = la;
let LineLocation{pos: posb, len: lenb} = lb;
Token::Number( Token::Number(
LineLocation { pos: posa, len: posb - posa + lenb }, LineLocation { pos: la.pos, len: lb.pos - la.pos + lb.len },
va.powf(vb) va.powf(vb)
) )
} else { panic!(); } } else { panic!(); }

View File

@ -31,9 +31,8 @@ fn lookback(
(Token::Constant(_,_,_), Token::Constant(l,_,_)) (Token::Constant(_,_,_), Token::Constant(l,_,_))
=> { => {
g.push_back(a); g.push_back(a);
let LineLocation { pos: i, .. } = l;
g.push_back(Token::PreOperator( g.push_back(Token::PreOperator(
LineLocation{pos: i-1, len: 0}, LineLocation{pos: l.pos-1, len: 0},
Operator::ImplicitMultiply Operator::ImplicitMultiply
)); ));
g.push_back(b); g.push_back(b);
@ -42,10 +41,8 @@ fn lookback(
// The following are syntax errors // The following are syntax errors
(Token::Number(la, _), Token::Number(lb,_)) (Token::Number(la, _), Token::Number(lb,_))
=> { => {
let LineLocation { pos: posa, .. } = *la;
let LineLocation { pos: posb, len: lenb } = *lb;
return Err(( return Err((
LineLocation{pos: posa, len: posb - posa + lenb}, LineLocation{pos: la.pos, len: lb.pos - la.pos + lb.len},
ParserError::Syntax ParserError::Syntax
)); ));
} }
@ -106,12 +103,9 @@ pub fn p_groupify(mut g: VecDeque<Token>) -> Result<Token, (LineLocation, Parser
}, },
Token::PreGroupEnd(l) => { Token::PreGroupEnd(l) => {
let LineLocation{pos: posa, ..} = *l_now;
let LineLocation{pos: posb, len: lenb} = l;
let l = LineLocation { let l = LineLocation {
pos: posa, pos: l_now.pos,
len: lenb + posb - posa len: l.len + l.pos - l_now.pos
}; };
if i_level == 0 { if i_level == 0 {

View File

@ -15,10 +15,9 @@ fn update_line_location(mut t: Token, stop_i: usize) -> Token {
Token::PreNumber(ref mut l, _) | Token::PreNumber(ref mut l, _) |
Token::PreWord(ref mut l, _) Token::PreWord(ref mut l, _)
=> { => {
let LineLocation{pos, .. } = l;
*l = LineLocation{ *l = LineLocation{
pos: *pos, pos: l.pos,
len: stop_i - *pos, len: stop_i - l.pos,
}; };
}, },
_ => panic!() _ => panic!()

View File

@ -75,10 +75,9 @@ fn treeify_binary(
=> { => {
// Binary and right-unary operators cannot // Binary and right-unary operators cannot
// follow a binary operator. // follow a binary operator.
let LineLocation { pos: posa, .. } = *this.get_line_location(); let tl = *this.get_line_location();
let LineLocation { pos: posb, len: lenb } = *l;
return Err(( return Err((
LineLocation{pos: posa, len: posb - posa + lenb}, LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
ParserError::Syntax ParserError::Syntax
)); ));
}, },
@ -172,10 +171,9 @@ fn treeify_unaryleft(
=> { => {
// Binary and right-unary operators cannot // Binary and right-unary operators cannot
// follow a binary operator. // follow a binary operator.
let LineLocation { pos: posa, .. } = *this.get_line_location(); let tl = *this.get_line_location();
let LineLocation { pos: posb, len: lenb } = *l;
return Err(( return Err((
LineLocation{pos: posa, len: posb - posa + lenb}, LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
ParserError::Syntax ParserError::Syntax
)); ));
}, },
@ -261,10 +259,9 @@ fn treeify_unaryright(
match o { match o {
// Left unary operators // Left unary operators
Operator::Negative => { Operator::Negative => {
let LineLocation { pos: posa, .. } = *this.get_line_location(); let tl = *this.get_line_location();
let LineLocation { pos: posb, len: lenb } = *l;
return Err(( return Err((
LineLocation{pos: posa, len: posb - posa + lenb}, LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
ParserError::Syntax ParserError::Syntax
)); ));
}, },
@ -279,10 +276,9 @@ fn treeify_unaryright(
} }
if let Token::PreOperator(l, _) = left { if let Token::PreOperator(l, _) = left {
let LineLocation { pos: posa, .. } = *this.get_line_location(); let tl = *this.get_line_location();
let LineLocation { pos: posb, len: lenb } = *l;
return Err(( return Err((
LineLocation{pos: posa, len: posb - posa + lenb}, LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
ParserError::Syntax ParserError::Syntax
)); ));