Skip to content

Commit

Permalink
Add single else statement to the ast
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Sep 30, 2023
1 parent 043f366 commit bc09159
Show file tree
Hide file tree
Showing 2 changed files with 648 additions and 11 deletions.
14 changes: 9 additions & 5 deletions parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,26 @@ impl Parser {
orelse = Some(if_value);
}
}

if self.at(Kind::Else) {
self.bump(Kind::Else);
let mut single_else_body: Option<Vec<Statement>> = None;
if self.eat(Kind::Else) {
self.expect(Kind::Colon)?;
let else_body = self.parse_suite()?;
if let Some(val) = &mut orelse {
val.update_orelse(else_body);
} else {
single_else_body = Some(else_body);
}
}
};

// if we had any else or elif statements, we need to wrap them in a vec
// otherwise we just return an empty vec as the else block of the if statement
let or_else_vec = if let Some(val) = orelse {
vec![Statement::IfStatement(val)]
} else {
vec![]
match single_else_body {
Some(val) => val,
None => vec![],
}
};

// There can be a dedent after the if block
Expand Down
Loading

0 comments on commit bc09159

Please sign in to comment.