mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 12:22:42 +00:00
chapter 8 done
This commit is contained in:
parent
f56fcc4a8b
commit
956c4d0f28
13 changed files with 570 additions and 177 deletions
|
|
@ -71,7 +71,7 @@ impl Lexer {
|
|||
me.scan_token();
|
||||
}
|
||||
|
||||
me.tokens.push(Token::new(TokenType::EOF, "".to_owned(), me.code_pos));
|
||||
me.tokens.push(Token::new(TokenType::EOF, me.code_pos));
|
||||
|
||||
if me.errors.is_empty() {
|
||||
Ok(me.tokens)
|
||||
|
|
@ -234,7 +234,7 @@ impl Lexer {
|
|||
fn push_token(&mut self, token_type: TokenType) {
|
||||
let lexeme: String = self.source[self.start..self.current].iter().collect();
|
||||
|
||||
self.tokens.push(Token::new(token_type, lexeme, self.code_pos));
|
||||
self.tokens.push(Token::new(token_type, self.code_pos));
|
||||
}
|
||||
|
||||
fn try_parse_string(&mut self) -> Option<TokenType> {
|
||||
|
|
|
|||
|
|
@ -27,33 +27,25 @@ pub enum TokenType {
|
|||
}
|
||||
|
||||
pub struct Token {
|
||||
token_type: TokenType,
|
||||
lexeme: String,
|
||||
|
||||
code_pos: CodePos,
|
||||
pub token_type: TokenType,
|
||||
// pub lexeme: String,
|
||||
pub code_pos: CodePos,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn new(token_type: TokenType, lexeme: String, pos: CodePos) -> Self {
|
||||
pub fn new(token_type: TokenType, pos: CodePos) -> Self {
|
||||
Token {
|
||||
token_type,
|
||||
lexeme: lexeme,
|
||||
// lexeme,
|
||||
code_pos: pos,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn token_type(&self) -> &TokenType {
|
||||
&self.token_type
|
||||
}
|
||||
|
||||
pub fn code_pos(&self) -> CodePos {
|
||||
self.code_pos
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Token {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "<{:?}> (\"{}\")", self.token_type, self.lexeme)
|
||||
write!(f, "<{:?}>", self.token_type)
|
||||
// write!(f, "<{:?}> (\"{}\")", self.token_type, self.lexeme)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue