From 0e3f1c26d211ab9a572a6232a3812c618fc0c065 Mon Sep 17 00:00:00 2001 From: Sander Hautvast Date: Fri, 24 Jan 2020 12:54:40 +0100 Subject: [PATCH] the scanner now recognizes single character tokens like plus and minus --- src/scanner.rs | 33 ++++++++++++++++++++++++++++++--- src/tokens.rs | 14 +++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/scanner.rs b/src/scanner.rs index 5027994..c6c57fa 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -53,13 +53,40 @@ impl Scanner<'_> { } ///scans the source, character by character - fn scan_token(&mut self) {} + fn scan_token(&mut self) { + let next_char = self.advance(); + + // determine what to do with every character + match next_char { + '(' => self.add_token(LEFTPAREN), + ')' => self.add_token(RIGHTPAREN), + '{' => self.add_token(LEFTBRACE), + '}' => self.add_token(RIGHTBRACE), + ',' => self.add_token(COMMA), + '.' => self.add_token(DOT), + '-' => self.add_token(MINUS), + '+' => self.add_token(PLUS), + ';' => self.add_token(SEMICOLON), + '*' => self.add_token(STAR), + _ => {} + } + } + + /// advance (consume) one character and return that + fn advance(&mut self) -> char { + self.current += 1; + self.source[self.current - 1..self.current].chars().next().unwrap() + } /// adds a token of the given type - fn add_token(&mut self, token_type: TokenType) {} + fn add_token(&mut self, token_type: TokenType) { + let text = &self.source[self.start..self.current]; + let token = Token { token_type: token_type, lexeme: text, literal: Box::new(""), line: self.line }; + self.tokens.push(token); + } /// returns true iff the end of the source has been reached fn is_at_end(&self) -> bool { - true + self.current >= self.source.len() } } diff --git a/src/tokens.rs b/src/tokens.rs index dbeced7..1b47446 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -33,5 +33,17 @@ impl fmt::Debug for Token<'_> { #[derive(Debug, Clone, Copy)] pub enum TokenType { - EOF + // Single-character tokens. + LEFTPAREN, // ( + RIGHTPAREN, // ) + LEFTBRACE, // [ + RIGHTBRACE, // ] + COMMA, // , + DOT, // . + MINUS, // - + PLUS, // + + SEMICOLON, // ; + STAR, // * + + EOF // end of file } \ No newline at end of file