Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ impl Lexer {
tok = Token::Bang;
}
},
'/' => tok = Token::Slash,
'/' => {
if self.peek_char() == '/' {
self.eat_complete_line();
tok = Token::Comment;
} else {
tok = Token::Slash;
}
},
'*' => tok = Token::Asterisk,
'<' => tok = Token::Lt,
'>' => tok = Token::Gt,
Expand Down Expand Up @@ -117,6 +124,12 @@ impl Lexer {
self.read_char();
}
}

fn eat_complete_line(&mut self) {
while self.ch != '\n' && self.ch != '\0' {
self.read_char();
}
}
}

/// Helper method to check if a char is a letter
Expand Down
2 changes: 2 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Token {
If, // if
Else, // else
Return, // return

Comment, // //
}

pub fn lookup_ident(ident: &str) -> Token {
Expand Down
10 changes: 9 additions & 1 deletion tests/lexer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ fn test_next_token() {
x + y
};

// One line comment

let result = add(five, ten);
!-/*5;
5 < 10 > 5;

// Multi-line
// comment
if (5 < 10) {
return true;
} else {
Expand All @@ -25,7 +29,7 @@ fn test_next_token() {

10 == 10;
10 != 9;
";
// Comment at the end";

let tests = [
Token::Let,
Expand Down Expand Up @@ -53,6 +57,7 @@ fn test_next_token() {
Token::Ident("y".to_string()),
Token::Rbrace,
Token::Semicolon,
Token::Comment,
Token::Let,
Token::Ident("result".to_string()),
Token::Assign,
Expand All @@ -75,6 +80,8 @@ fn test_next_token() {
Token::Gt,
Token::Int("5".to_string()),
Token::Semicolon,
Token::Comment,
Token::Comment,
Token::If,
Token::Lparen,
Token::Int("5".to_string()),
Expand All @@ -100,6 +107,7 @@ fn test_next_token() {
Token::Noteq,
Token::Int("9".to_string()),
Token::Semicolon,
Token::Comment,
Token::Eof,
];

Expand Down