Skip to content

Commit

Permalink
add check fr break statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser committed Jun 9, 2024
1 parent 89bd538 commit e1f5646
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{
#[derive(Debug)]
pub struct Parser {
tokens: Vec<Token>,
inside_loop: bool,
}

impl Default for Parser {
Expand All @@ -23,7 +24,10 @@ impl Default for Parser {

impl Parser {
pub fn new() -> Self {
Parser { tokens: Vec::new() }
Parser {
tokens: Vec::new(),
inside_loop: false,
}
}

fn not_eof(&self) -> bool {
Expand Down Expand Up @@ -72,6 +76,9 @@ impl Parser {

fn parse_break_declaration(&mut self) -> Stmt {
self.eat();
if !self.inside_loop {
panic!("`break` statement found outside of a loop (while, for, loop) context.")
}
if *self.at() == Token::Symbol(Symbol::Semicolon) {
self.eat();
return Stmt::BreakStmt(None);
Expand Down Expand Up @@ -445,10 +452,14 @@ impl Parser {
Token::Symbol(Symbol::LeftBrace),
"Expected left brace before `while` expression.",
);

let prev_inside_loop = self.inside_loop; // Save previous context.
self.inside_loop = true; // We are now in a loop context. Modify parser.
let mut statements: Vec<Stmt> = Vec::new();
while self.not_eof() && *self.at() != Token::Symbol(Symbol::RightBrace) {
statements.push(self.parse_stmt());
}
self.inside_loop = prev_inside_loop; // Restore previous context
self.expect(
Token::Symbol(Symbol::RightBrace),
"Expected right brace after `while` expression.",
Expand All @@ -461,10 +472,13 @@ impl Parser {
Token::Symbol(Symbol::LeftBrace),
"Expected left brace before `loop` expression.",
);
let prev_inside_loop = self.inside_loop; // Save previous context.
self.inside_loop = true; // We are now in a loop context. Modify parser.
let mut statements: Vec<Stmt> = Vec::new();
while self.not_eof() && *self.at() != Token::Symbol(Symbol::RightBrace) {
statements.push(self.parse_stmt());
}
self.inside_loop = prev_inside_loop; // Restore previous context
self.expect(
Token::Symbol(Symbol::RightBrace),
"Expected right brace after `loop` expression.",
Expand Down

0 comments on commit e1f5646

Please sign in to comment.