From 2d112abfba0aa30e1d6ebec8375f71a20694fea6 Mon Sep 17 00:00:00 2001 From: KPMGE Date: Mon, 25 Mar 2024 21:18:23 -0300 Subject: [PATCH] :sparkles: add support for comments --- src/lexer.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index 61c8fbc..6c95dd9 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -46,13 +46,19 @@ impl Lexer { '}' => Token::RightBrace, '+' => Token::Plus, '-' => Token::Minus, - '*' => Token::Asterisk, '<' => Token::LessThan, '>' => Token::GreaterThan, ',' => Token::Comma, - '/' => Token::Slash, ';' => Token::Semicolon, '"' => Token::String(self.read_string()), + '*' => Token::Asterisk, + '/' => { + if self.peek_char(self.read_position).unwrap() == '*' { + self.skip_comments(); + return self.next_token(); + } + Token::Slash + } '=' => match self.peek_char(self.read_position) { Some('=') => { self.read_char(); @@ -169,6 +175,19 @@ impl Lexer { break; } } + + fn skip_comments(&mut self) { + while let (Some(ch), Some(next_ch)) = + (self.current_char, self.peek_char(self.read_position)) + { + if ch == '*' && next_ch == '/' { + break; + } + self.read_char(); + } + self.read_char(); + self.read_char(); + } } trait IsLetter {