diff --git a/src/main.rs b/src/main.rs index 1afbbf7..91f98e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,4 +65,4 @@ fn run( _script: String ) -> i32 { // https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust // Pause : -// http://www.craftinginterpreters.com/scanning.html#regular-languages-and-expressions +// http://www.craftinginterpreters.com/scanning.html#lexical-errors diff --git a/src/rlox.rs b/src/rlox.rs index 5bce136..4bb3780 100644 --- a/src/rlox.rs +++ b/src/rlox.rs @@ -1,5 +1,6 @@ mod token_type; mod token; +mod scanner; pub struct RLox { pub had_error: bool, diff --git a/src/rlox/scanner.rs b/src/rlox/scanner.rs new file mode 100644 index 0000000..36519b6 --- /dev/null +++ b/src/rlox/scanner.rs @@ -0,0 +1,59 @@ +use super::token_type::TokenType; +use super::token::Token; + +struct Scanner { + source: Vec, + tokens: Vec, + + start: usize, + current: usize, + line: u32, +} + +impl Scanner { + fn scan_tokens(&mut self) { + while (!self.is_at_end()) { + self.start = self.current; + self.scan_token(); + } + + // Ajout d'un token final quand il n'y a plus rien à parser + self.tokens.push(Token{ token_type: TokenType::Eof, lexeme: String::from(""), literal: String::from(""), line: self.line } ); + } + + fn is_at_end(&self) -> bool { + self.current>= self.source.len() + } + + fn scan_token(&mut self) { + let c = self.advance(); + match c { + '(' => self.add_simple_token( TokenType::LeftParen ), + ')' => self.add_simple_token( TokenType::RightParen ), + '{' => self.add_simple_token( TokenType::LeftBrace ), + '}' => self.add_simple_token( TokenType::RightBrace ), + ',' => self.add_simple_token( TokenType::Comma ), + '.' => self.add_simple_token( TokenType::Dot ), + '-' => self.add_simple_token( TokenType::Minus ), + '+' => self.add_simple_token( TokenType::Plus ), + ';' => self.add_simple_token( TokenType::Semicolon ), + '*' => self.add_simple_token( TokenType::Star ), + _ => () + } + } + + fn advance(&mut self) -> char { + self.current += 1; + self.source[self.current] + } + + fn add_simple_token(&mut self, t: TokenType) { + self.add_token(t, String::from("")); + } + + fn add_token(&mut self, t: TokenType, l: String) { + let text = self.source[self.start..self.current].iter().collect(); + self.tokens.push(Token{ token_type: t, lexeme: text, literal: l, line: self.line } ); + } + +} \ No newline at end of file diff --git a/src/rlox/token.rs b/src/rlox/token.rs index 5eb1bbf..644c5ca 100644 --- a/src/rlox/token.rs +++ b/src/rlox/token.rs @@ -1,11 +1,11 @@ use super::token_type::TokenType; #[derive(Debug)] -struct Token { - token_type: TokenType, - lexeme: String, - literal: String, - line: u32, +pub struct Token { + pub token_type: TokenType, + pub lexeme: String, + pub literal: String, + pub line: u32, } impl std::fmt::Display for Token {