Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: allow multiple conditions in if, while and do..while #50

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/parser/internal/statement/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::tree::utils::CommaSeparated;
pub fn if_statement(state: &mut State) -> ParseResult<IfStatement> {
let comments = state.iterator.comments();
let r#if = utils::skip_keyword(state, TokenKind::If)?;
let condition = expression::create(state)?;

let conditions = utils::comma_separated(state, &expression::create, TokenKind::LeftBrace)?;
let statement = block::block_statement(state)?;

let mut elseifs: Vec<IfElseIfStatement> = vec![];
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn if_statement(state: &mut State) -> ParseResult<IfStatement> {
Ok(IfStatement {
comments,
r#if,
condition,
conditions,
block: statement,
elseifs,
r#else,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/internal/statement/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn do_while_statement(state: &mut State) -> ParseResult<DoWhileStatement> {
r#do: utils::skip_keyword(state, TokenKind::Do)?,
block: block::block_statement(state)?,
r#while: utils::skip_keyword(state, TokenKind::While)?,
condition: expression::create(state)?,
conditions: utils::comma_separated(state, &expression::create, TokenKind::SemiColon)?,
semicolon: utils::skip_semicolon(state)?,
})
}
Expand All @@ -241,7 +241,7 @@ pub fn while_statement(state: &mut State) -> ParseResult<WhileStatement> {
Ok(WhileStatement {
comments: state.iterator.comments(),
r#while: utils::skip_keyword(state, TokenKind::While)?,
condition: expression::create(state)?,
conditions: utils::comma_separated(state, &expression::create, TokenKind::LeftBrace)?,
block: block::block_statement(state)?,
})
}
Expand Down
9 changes: 7 additions & 2 deletions src/tree/statement/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::tree::Node;
pub struct IfStatement {
pub comments: CommentGroup,
pub r#if: Keyword,
pub condition: Expression,
pub conditions: CommaSeparated<Expression>,
pub block: BlockStatement,
pub elseifs: Vec<IfElseIfStatement>,
pub r#else: Option<IfElseStatement>,
Expand Down Expand Up @@ -86,7 +86,12 @@ impl Node for IfStatement {
}

fn children(&self) -> Vec<&dyn Node> {
let mut children: Vec<&dyn Node> = vec![&self.r#if, &self.condition, &self.block];
let mut children: Vec<&dyn Node> = vec![&self.r#if, &self.block];

for condition in &self.conditions.inner {
children.push(condition);
}

for elseif in &self.elseifs {
children.push(elseif);
}
Expand Down
20 changes: 16 additions & 4 deletions src/tree/statement/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct DoWhileStatement {
pub r#do: Keyword,
pub block: BlockStatement,
pub r#while: Keyword,
pub condition: Expression,
pub conditions: CommaSeparated<Expression>,
pub semicolon: usize,
}

Expand All @@ -101,7 +101,7 @@ pub struct DoWhileStatement {
pub struct WhileStatement {
pub comments: CommentGroup,
pub r#while: Keyword,
pub condition: Expression,
pub conditions: CommaSeparated<Expression>,
pub block: BlockStatement,
}

Expand Down Expand Up @@ -372,7 +372,13 @@ impl Node for DoWhileStatement {
}

fn children(&self) -> Vec<&dyn Node> {
vec![&self.r#do, &self.block, &self.r#while, &self.condition]
let mut children: Vec<&dyn Node> = vec![&self.r#do, &self.block, &self.r#while];

for condition in &self.conditions.inner {
children.push(condition);
}

children
}

fn get_description(&self) -> String {
Expand All @@ -394,7 +400,13 @@ impl Node for WhileStatement {
}

fn children(&self) -> Vec<&dyn Node> {
vec![&self.r#while, &self.condition, &self.block]
let mut children: Vec<&dyn Node> = vec![&self.r#while, &self.block];

for condition in &self.conditions.inner {
children.push(condition);
}

children
}

fn get_description(&self) -> String {
Expand Down
59 changes: 32 additions & 27 deletions tests/samples/0015/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,45 @@ DefinitionTree {
value: "if",
position: 32,
},
condition: Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 35,
expression: ComparisonOperation(
LessThan {
conditions: CommaSeparated {
inner: [
Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left: Variable(
Variable {
position: 36,
name: "$n",
},
),
less_than: 39,
right: Literal(
Integer(
LiteralInteger {
comments: CommentGroup {
comments: [],
},
value: "2",
position: 41,
left_parenthesis: 35,
expression: ComparisonOperation(
LessThan {
comments: CommentGroup {
comments: [],
},
),
left: Variable(
Variable {
position: 36,
name: "$n",
},
),
less_than: 39,
right: Literal(
Integer(
LiteralInteger {
comments: CommentGroup {
comments: [],
},
value: "2",
position: 41,
},
),
),
},
),
right_parenthesis: 42,
},
),
right_parenthesis: 42,
},
),
],
commas: [],
},
block: BlockStatement {
comments: CommentGroup {
comments: [],
Expand Down
31 changes: 18 additions & 13 deletions tests/samples/0016/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,26 @@ DefinitionTree {
value: "if",
position: 28,
},
condition: Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 30,
expression: Variable(
Variable {
position: 31,
name: "$foo",
conditions: CommaSeparated {
inner: [
Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 30,
expression: Variable(
Variable {
position: 31,
name: "$foo",
},
),
right_parenthesis: 35,
},
),
right_parenthesis: 35,
},
),
],
commas: [],
},
block: BlockStatement {
comments: CommentGroup {
comments: [],
Expand Down
31 changes: 18 additions & 13 deletions tests/samples/0017/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,26 @@ DefinitionTree {
value: "if",
position: 28,
},
condition: Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 30,
expression: Variable(
Variable {
position: 31,
name: "$foo",
conditions: CommaSeparated {
inner: [
Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 30,
expression: Variable(
Variable {
position: 31,
name: "$foo",
},
),
right_parenthesis: 35,
},
),
right_parenthesis: 35,
},
),
],
commas: [],
},
block: BlockStatement {
comments: CommentGroup {
comments: [],
Expand Down
31 changes: 18 additions & 13 deletions tests/samples/0026/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,26 @@ DefinitionTree {
value: "while",
position: 36,
},
condition: Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 42,
expression: Variable(
Variable {
position: 43,
name: "$a",
conditions: CommaSeparated {
inner: [
Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 42,
expression: Variable(
Variable {
position: 43,
name: "$a",
},
),
right_parenthesis: 45,
},
),
right_parenthesis: 45,
},
),
],
commas: [],
},
semicolon: 46,
},
),
Expand Down
62 changes: 36 additions & 26 deletions tests/samples/0054/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,26 @@ DefinitionTree {
value: "if",
position: 28,
},
condition: Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 31,
expression: Variable(
Variable {
position: 32,
name: "$foo",
conditions: CommaSeparated {
inner: [
Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 31,
expression: Variable(
Variable {
position: 32,
name: "$foo",
},
),
right_parenthesis: 36,
},
),
right_parenthesis: 36,
},
),
],
commas: [],
},
block: BlockStatement {
comments: CommentGroup {
comments: [],
Expand Down Expand Up @@ -92,21 +97,26 @@ DefinitionTree {
value: "if",
position: 52,
},
condition: Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 55,
expression: Variable(
Variable {
position: 56,
name: "$bar",
conditions: CommaSeparated {
inner: [
Parenthesized(
ParenthesizedExpression {
comments: CommentGroup {
comments: [],
},
left_parenthesis: 55,
expression: Variable(
Variable {
position: 56,
name: "$bar",
},
),
right_parenthesis: 60,
},
),
right_parenthesis: 60,
},
),
],
commas: [],
},
block: BlockStatement {
comments: CommentGroup {
comments: [],
Expand Down
Loading