typos in comments

This commit is contained in:
Tristan 2025-05-10 12:17:41 -04:00
parent 997adc7350
commit 9dbbf7741e

View File

@ -1,5 +1,5 @@
use crate::lexer::token::Token; use crate::lexer::token::Token;
use crate::parser::ast::{Expression, Statement, Literal, Operator}; // Added Literal here use crate::parser::ast::{Expression, Statement, Literal, Operator};
use crate::lexer::Lexer; use crate::lexer::Lexer;
pub struct Parser { pub struct Parser {
@ -7,7 +7,6 @@ pub struct Parser {
current: usize, current: usize,
} }
// mostly all written months ago
#[allow(dead_code)] #[allow(dead_code)]
impl Parser { impl Parser {
@ -24,27 +23,17 @@ impl Parser {
return None; return None;
} }
// Initializer: Could be variable declaration or expression statement
let initializer = if self.match_token(Token::Semicolon) { let initializer = if self.match_token(Token::Semicolon) {
None None
} else if self.match_token(Token::Let) { } else if self.match_token(Token::Let) {
// Need to handle declaration specifically if wanted, or parse as statement Some(Box::new(self.parse_variable_declaration()?))
Some(Box::new(self.parse_variable_declaration()?)) // Assuming Let was consumed
} else { } else {
// Parse as expression statement, then expect semicolon
let expr_stmt = self.parse_expression_statement()?; let expr_stmt = self.parse_expression_statement()?;
Some(Box::new(expr_stmt)) Some(Box::new(expr_stmt))
}; };
// Semicolon already consumed if initializer was None or handled by expr_stmt
// Condition: Must be an expression
let condition = if self.check(&Token::Semicolon) { let condition = if self.check(&Token::Semicolon) {
// No condition (treat as true) - maybe represent with a literal true?
// For now, let's require a condition or handle absence later
eprintln!("Error: For loop condition is required (for now)."); eprintln!("Error: For loop condition is required (for now).");
return None; return None;
// TODO: Handle absent condition -> treat as true
// Some(Expression::Literal(Literal::Boolean(true)))
} else { } else {
self.parse_expression()? self.parse_expression()?
}; };
@ -54,9 +43,8 @@ impl Parser {
return None; return None;
} }
// Increment: Must be an expression (or None)
let increment = if self.check(&Token::RightParen) { let increment = if self.check(&Token::RightParen) {
None // No increment expression None
} else { } else {
Some(self.parse_expression()?) Some(self.parse_expression()?)
}; };
@ -66,12 +54,10 @@ impl Parser {
return None; return None;
} }
// Body
let body = Box::new(self.parse_statement()?); let body = Box::new(self.parse_statement()?);
eprintln!("Warning: For statement AST structure might need review."); eprintln!("Warning: For statement AST structure might need review.");
// Some(Statement::ForStatement(initializer, condition, increment, body)) None
None // Temporarily disable until AST/logic is solid for for-loops
} }
@ -460,8 +446,7 @@ impl Parser {
} }
fn current_token(&self) -> &Token { fn current_token(&self) -> &Token {
// Handle potential index out of bounds if current somehow exceeds length self.tokens.get(self.current).unwrap_or(&Token::EOF)
self.tokens.get(self.current).unwrap_or(&Token::EOF) // Return EOF if out of bounds
} }
fn peek(&self) -> &Token { fn peek(&self) -> &Token {
@ -472,7 +457,6 @@ impl Parser {
if !self.is_at_end() { if !self.is_at_end() {
self.current += 1; self.current += 1;
} }
// Return the token *before* the increment (the one just consumed)
self.previous_token() self.previous_token()
} }
@ -526,7 +510,7 @@ impl Parser {
(Token::Module, Token::Module) => true, (Token::Module, Token::Module) => true,
(Token::Import, Token::Import) => true, (Token::Import, Token::Import) => true,
(Token::EOF, Token::EOF) => true, (Token::EOF, Token::EOF) => true,
// Add other simple tokens here... // Add others
(t1, t2) => std::mem::discriminant(t1) == std::mem::discriminant(t2) (t1, t2) => std::mem::discriminant(t1) == std::mem::discriminant(t2)
}; };
@ -539,7 +523,6 @@ impl Parser {
} }
fn previous_token(&self) -> &Token { fn previous_token(&self) -> &Token {
// Handle edge case where current is 0
if self.current == 0 { if self.current == 0 {
self.tokens.get(0).unwrap_or(&Token::EOF) self.tokens.get(0).unwrap_or(&Token::EOF)
} else { } else {
@ -548,14 +531,12 @@ impl Parser {
} }
// 5-7-25 // 5-7-25
// parse_program in Parser: a cleaner way to handle parsing multiple statements
pub fn parse_program(&mut self) -> Vec<Statement> { pub fn parse_program(&mut self) -> Vec<Statement> {
let mut statements = Vec::new(); let mut statements = Vec::new();
while !self.is_at_end_of_significant_tokens() { while !self.is_at_end_of_significant_tokens() {
match self.parse_statement() { match self.parse_statement() {
Some(stmt) => statements.push(stmt), Some(stmt) => statements.push(stmt),
None => { None => {
// Handle error: could not parse statement
eprintln!("Error: Could not parse statement."); eprintln!("Error: Could not parse statement.");
break; break;
} }