add ranges

This commit is contained in:
SimonFJ20 2023-03-16 08:48:58 +01:00
parent f6dd337ba1
commit b6e33d3810
5 changed files with 44 additions and 7 deletions

View File

@ -46,6 +46,14 @@ pub enum Expr {
left: Box<Node<Expr>>, left: Box<Node<Expr>>,
right: Box<Node<Expr>>, right: Box<Node<Expr>>,
}, },
RangeExclusive {
begin: Box<Node<Expr>>,
end: Box<Node<Expr>>,
},
RangeInclusive {
begin: Box<Node<Expr>>,
end: Box<Node<Expr>>,
},
Assign { Assign {
assign_type: AssignType, assign_type: AssignType,

View File

@ -236,6 +236,8 @@ impl<'a> Lexer<'a> {
self.step(); self.step();
if !self.done() && self.current() == '.' { if !self.done() && self.current() == '.' {
self.step_and_token(TokenType::DotDotDot, start) self.step_and_token(TokenType::DotDotDot, start)
} else if !self.done() && self.current() == '=' {
self.step_and_token(TokenType::DotDotEqual, start)
} else { } else {
self.token(TokenType::DotDot, start) self.token(TokenType::DotDot, start)
} }

View File

@ -21,7 +21,7 @@ fn main() {
// }); // });
// println!("]"); // println!("]");
let text2 = "[1, 2, ...a]"; let text2 = "[1, 1..=2, ...a]";
let lexer2 = Lexer::new(text2); let lexer2 = Lexer::new(text2);
println!("tokens = ["); println!("tokens = [");
lexer2.for_each(|token| { lexer2.for_each(|token| {

View File

@ -315,11 +315,11 @@ where
} }
fn parse_prec_lt_lte_gt_gte_in(&mut self) -> Node<Expr> { fn parse_prec_lt_lte_gt_gte_in(&mut self) -> Node<Expr> {
let mut left = self.parse_prec_add_subtract(); let mut left = self.parse_prec_range();
while !self.done() { while !self.done() {
if self.current_is(TokenType::LessThan) { if self.current_is(TokenType::LessThan) {
self.step(); self.step();
let right = self.parse_prec_add_subtract(); let right = self.parse_prec_range();
left = self.node(Expr::Binary { left = self.node(Expr::Binary {
binary_type: BinaryType::LT, binary_type: BinaryType::LT,
left: Box::new(left), left: Box::new(left),
@ -327,7 +327,7 @@ where
}); });
} else if self.current_is(TokenType::GreaterThan) { } else if self.current_is(TokenType::GreaterThan) {
self.step(); self.step();
let right = self.parse_prec_add_subtract(); let right = self.parse_prec_range();
left = self.node(Expr::Binary { left = self.node(Expr::Binary {
binary_type: BinaryType::GT, binary_type: BinaryType::GT,
left: Box::new(left), left: Box::new(left),
@ -335,7 +335,7 @@ where
}); });
} else if self.current_is(TokenType::LessThanEqual) { } else if self.current_is(TokenType::LessThanEqual) {
self.step(); self.step();
let right = self.parse_prec_add_subtract(); let right = self.parse_prec_range();
left = self.node(Expr::Binary { left = self.node(Expr::Binary {
binary_type: BinaryType::LTE, binary_type: BinaryType::LTE,
left: Box::new(left), left: Box::new(left),
@ -343,7 +343,7 @@ where
}); });
} else if self.current_is(TokenType::GreaterThanEqual) { } else if self.current_is(TokenType::GreaterThanEqual) {
self.step(); self.step();
let right = self.parse_prec_add_subtract(); let right = self.parse_prec_range();
left = self.node(Expr::Binary { left = self.node(Expr::Binary {
binary_type: BinaryType::GTE, binary_type: BinaryType::GTE,
left: Box::new(left), left: Box::new(left),
@ -351,7 +351,7 @@ where
}); });
} else if self.current_is(TokenType::In) { } else if self.current_is(TokenType::In) {
self.step(); self.step();
let right = self.parse_prec_add_subtract(); let right = self.parse_prec_range();
left = self.node(Expr::Binary { left = self.node(Expr::Binary {
binary_type: BinaryType::In, binary_type: BinaryType::In,
left: Box::new(left), left: Box::new(left),
@ -364,6 +364,32 @@ where
left left
} }
fn parse_prec_range(&mut self) -> Node<Expr> {
let begin = self.parse_prec_add_subtract();
if self.done() {
return begin;
}
match self.current().token_type {
TokenType::DotDot => {
self.step();
let end = self.parse_prec_add_subtract();
self.node(Expr::RangeExclusive {
begin: Box::new(begin),
end: Box::new(end),
})
}
TokenType::DotDotEqual => {
self.step();
let end = self.parse_prec_add_subtract();
self.node(Expr::RangeExclusive {
begin: Box::new(begin),
end: Box::new(end),
})
}
_ => begin,
}
}
fn parse_prec_add_subtract(&mut self) -> Node<Expr> { fn parse_prec_add_subtract(&mut self) -> Node<Expr> {
let mut left = self.parse_prec_multiply_divide_modulo(); let mut left = self.parse_prec_multiply_divide_modulo();
while !self.done() { while !self.done() {

View File

@ -75,6 +75,7 @@ pub enum TokenType {
Semicolon, Semicolon,
Ampersand, Ampersand,
DotDot, DotDot,
DotDotEqual,
DotDotDot, DotDotDot,
EqualLessThan, EqualLessThan,
} }