updated a bunch of stuff

This commit is contained in:
Moritz Gmeiner 2024-09-01 19:16:30 +02:00
commit 67bb5fe8fd
24 changed files with 683 additions and 702 deletions

View file

@ -129,6 +129,11 @@ impl Lexer {
// line comment
// advance until either source is empty or newline if found
while !self.source_is_empty() && self.advance() != '\n' {}
let comment: Box<str> =
self.source[self.start + 2..self.current].iter().collect();
self.push_token(TokenType::Comment(comment));
} else if self.consume('*') {
// block comment
@ -164,6 +169,12 @@ impl Lexer {
self.advance();
}
let comment: Box<str> = self.source[self.start + 2..self.current - 2]
.iter()
.collect();
self.push_token(TokenType::Comment(comment));
} else {
self.push_token(Slash)
}
@ -237,10 +248,13 @@ impl Lexer {
}
}
let string_literal = self.source[self.start + 1..self.current - 1].iter().collect();
let string_literal: Box<str> = self.source[self.start + 1..self.current - 1]
.iter()
.collect();
// Some(TokenType::String(Box::new(string_literal)))
self.tokens.push(Token::new_string(string_literal, self.code_pos));
self.tokens
.push(Token::new_string(string_literal, self.code_pos));
}
fn try_parse_number(&mut self) {
@ -290,8 +304,12 @@ impl Lexer {
}
fn try_parse_identifier(&mut self) {
let is_alpha_num_underscore =
|c: Option<char>| c.map_or(false, |c| matches!(c, '0'..='9' | 'A'..='Z' | '_' | 'a'..='z'));
let is_alpha_num_underscore = |c: Option<char>| {
c.map_or(
false,
|c| matches!(c, '0'..='9' | 'A'..='Z' | '_' | 'a'..='z'),
)
};
while is_alpha_num_underscore(self.peek()) {
self.advance();
@ -304,11 +322,12 @@ impl Lexer {
.cloned()
.unwrap_or(TokenType::Identifier(Box::new(lexeme))); */
if let Some(&token_type) = KEYWORDS.get(&lexeme) {
if let Some(token_type) = KEYWORDS.get(&lexeme) {
// Token::new(token_type, self.code_pos)
self.push_token(token_type);
self.push_token(token_type.clone());
} else {
self.tokens.push(Token::new_identifier(lexeme, self.code_pos));
self.tokens
.push(Token::new_identifier(lexeme, self.code_pos));
}
// Some(token_type)