implement comments

This commit is contained in:
Lukáš Hozda 2017-10-28 12:39:29 +02:00
parent 5bfee9688e
commit b8157a1121
2 changed files with 39 additions and 1 deletions

View File

@ -353,7 +353,36 @@ impl<'a> Iterator for TokenIterator<'a> {
'+' => return Some(Token::Plus), '+' => return Some(Token::Plus),
'-' => return Some(Token::Minus), '-' => return Some(Token::Minus),
'*' => return Some(Token::Multiply), '*' => return Some(Token::Multiply),
'/' => return Some(Token::Divide), '/' => {
match self.char_stream.peek() {
Some(&'/') => {
self.char_stream.next();
while let Some(c) = self.char_stream.next() {
if c == '\n' { break; }
}
}
Some(&'*') => {
let mut level = 1;
self.char_stream.next();
while let Some(c) = self.char_stream.next() {
match c {
'/' => if let Some('*') = self.char_stream.next() {
level+=1;
}
'*' => if let Some('/') = self.char_stream.next() {
level-=1;
}
_ => (),
}
if level == 0 {
break;
}
}
}
_ => return Some(Token::Divide),
}
}
';' => return Some(Token::Semicolon), ';' => return Some(Token::Semicolon),
':' => return Some(Token::Colon), ':' => return Some(Token::Colon),
',' => return Some(Token::Comma), ',' => return Some(Token::Comma),

View File

@ -401,3 +401,12 @@ fn test_array_with_structs() {
assert!(false); assert!(false);
} }
} }
#[test]
fn test_comments() {
let mut engine = Engine::new();
assert!(engine.eval::<i64>("let x = 5; x // I am a single line comment, yay!").is_ok());
assert!(engine.eval::<i64>("let /* I am a multiline comment, yay! */ x = 5; x").is_ok());
}