Skip to content

Commit

Permalink
feat: add number()
Browse files Browse the repository at this point in the history
  • Loading branch information
c-kirkeby committed Sep 21, 2023
1 parent 79c1e5b commit f0bcd45
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Scanner {
b' ' | b'\r' | b'\t' => (),
b'\n' => self.line += 1,
b'"' => self.string()?,
b'0'..=b'9' => self.number()?,
_ => bail!("Unexpected character on line {}", self.line),
}
Ok(())
Expand Down Expand Up @@ -134,6 +135,13 @@ impl Scanner {
return self.source[self.current];
}

fn peek_next(&self) -> u8 {
if self.current + 1 >= self.source.len() {
return b'\0';
}
return self.source[self.current + 1];
}

fn string(&mut self) -> Result<()> {
while self.peek() != b'"' && !self.is_at_end() {
if self.peek() == b'\n' {
Expand All @@ -152,6 +160,32 @@ impl Scanner {
self.add_token(TokenType::String, Some(Literal::String(value.to_vec())));
Ok(())
}

fn is_digit(c: u8) -> bool {
c >= b'0' && c <= b'9'
}

fn number(&mut self) -> Result<()> {
while Self::is_digit(self.peek()) {
self.advance();
}

if self.peek() == b'.' && Self::is_digit(self.peek_next()) {
self.advance();

while Self::is_digit(self.peek()) {
self.advance();
}
}

self.add_token(
TokenType::Number,
Some(Literal::Number(
String::from_utf8_lossy(&self.source[self.start..self.current]).parse::<f64>()?,
)),
);
Ok(())
}
}

#[cfg(test)]
Expand Down Expand Up @@ -182,4 +216,29 @@ mod tests {
);
Ok(())
}

#[test]
fn test_scan_tokens_number() -> Result<()> {
let mut scanner = Scanner::new("123.45\n321".to_string());
scanner.scan_tokens()?;
assert_eq!(
scanner.tokens,
vec![
Token::new(
TokenType::Number,
String::from("123.45"),
Some(Literal::Number(123.45)),
1
),
Token::new(
TokenType::Number,
String::from("321"),
Some(Literal::Number(321.0)),
2
),
Token::new(TokenType::EOF, String::from(""), None, 2)
]
);
Ok(())
}
}
1 change: 1 addition & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt;
#[allow(dead_code)]
pub enum Literal {
String(Vec<u8>),
Number(f64),
None,
}

Expand Down

0 comments on commit f0bcd45

Please sign in to comment.