rename comparison tokens

This commit is contained in:
nora 2021-10-31 01:37:48 +02:00
parent ce9050e8a2
commit 06ac1bb2e7
2 changed files with 21 additions and 33 deletions

View file

@ -86,13 +86,13 @@ pub enum TokenType<'code> {
/// != /// !=
BangEqual, BangEqual,
/// > /// >
GreaterThan, Greater,
/// < /// <
LessThan, Less,
/// >= /// >=
GreaterThanEqual, GreaterEqual,
/// <= /// <=
LessThanEqual, LessEqual,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -204,18 +204,13 @@ impl<'code> Iterator for Lexer<'code> {
'>' => { '>' => {
break self.maybe_next_char( break self.maybe_next_char(
'=', '=',
TokenType::GreaterThanEqual, TokenType::GreaterEqual,
TokenType::GreaterThan, TokenType::Greater,
start, start,
); );
} }
'<' => { '<' => {
break self.maybe_next_char( break self.maybe_next_char('=', TokenType::LessEqual, TokenType::Less, start);
'=',
TokenType::LessThanEqual,
TokenType::LessThan,
start,
);
} }
'"' => { '"' => {
let mut buffer = String::new(); let mut buffer = String::new();
@ -426,10 +421,7 @@ mod test {
#[test] #[test]
fn smiley_face() { fn smiley_face() {
lex_test( lex_test(">>.<<", vec![Greater, Greater, Dot, Less, Less])
">>.<<",
vec![GreaterThan, GreaterThan, Dot, LessThan, LessThan],
)
} }
#[test] #[test]
@ -437,12 +429,12 @@ mod test {
lex_test( lex_test(
">= <= == < < >=", ">= <= == < < >=",
vec![ vec![
GreaterThanEqual, GreaterEqual,
LessThanEqual, LessEqual,
EqualEqual, EqualEqual,
LessThan, Less,
LessThan, Less,
GreaterThanEqual, GreaterEqual,
], ],
) )
} }

View file

@ -86,22 +86,22 @@ impl<'code> Parser<'code> {
fn comparison(&mut self) -> ParseResult<'code, Expr> { fn comparison(&mut self) -> ParseResult<'code, Expr> {
let lhs = self.term()?; let lhs = self.term()?;
match self.peek().map(|token| &token.kind) { match self.peek().map(|token| &token.kind) {
Some(TokenType::GreaterThan) => { Some(TokenType::Greater) => {
let _ = self.next(); let _ = self.next();
let rhs = self.term()?; let rhs = self.term()?;
self.binary_op(lhs, BinaryOpKind::Greater, rhs) self.binary_op(lhs, BinaryOpKind::Greater, rhs)
} }
Some(TokenType::GreaterThanEqual) => { Some(TokenType::GreaterEqual) => {
let _ = self.next(); let _ = self.next();
let rhs = self.term()?; let rhs = self.term()?;
self.binary_op(lhs, BinaryOpKind::GreaterEqual, rhs) self.binary_op(lhs, BinaryOpKind::GreaterEqual, rhs)
} }
Some(TokenType::LessThan) => { Some(TokenType::Less) => {
let _ = self.next(); let _ = self.next();
let rhs = self.term()?; let rhs = self.term()?;
self.binary_op(lhs, BinaryOpKind::Less, rhs) self.binary_op(lhs, BinaryOpKind::Less, rhs)
} }
Some(TokenType::LessThanEqual) => { Some(TokenType::LessEqual) => {
let _ = self.next(); let _ = self.next();
let rhs = self.term()?; let rhs = self.term()?;
self.binary_op(lhs, BinaryOpKind::LessEqual, rhs) self.binary_op(lhs, BinaryOpKind::LessEqual, rhs)
@ -327,17 +327,13 @@ mod test {
#[test] #[test]
fn greater() { fn greater() {
test_literal_bin_op( test_literal_bin_op(TokenType::Greater, BinaryOpKind::Greater, parse_comparison);
TokenType::GreaterThan,
BinaryOpKind::Greater,
parse_comparison,
);
} }
#[test] #[test]
fn greater_equal() { fn greater_equal() {
test_literal_bin_op( test_literal_bin_op(
TokenType::GreaterThanEqual, TokenType::GreaterEqual,
BinaryOpKind::GreaterEqual, BinaryOpKind::GreaterEqual,
parse_comparison, parse_comparison,
); );
@ -345,13 +341,13 @@ mod test {
#[test] #[test]
fn less() { fn less() {
test_literal_bin_op(TokenType::LessThan, BinaryOpKind::Less, parse_comparison); test_literal_bin_op(TokenType::Less, BinaryOpKind::Less, parse_comparison);
} }
#[test] #[test]
fn less_equal() { fn less_equal() {
test_literal_bin_op( test_literal_bin_op(
TokenType::LessThanEqual, TokenType::LessEqual,
BinaryOpKind::LessEqual, BinaryOpKind::LessEqual,
parse_comparison, parse_comparison,
); );