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!(); }
},
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),
)?;
}

View File

@ -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!(); }

View File

@ -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<Token>) -> Result<Token, (LineLocation, Parser
},
Token::PreGroupEnd(l) => {
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 {

View File

@ -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!()

View File

@ -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
));