Skip to content

Commit

Permalink
chore(fmt): format parentheses expression
Browse files Browse the repository at this point in the history
  • Loading branch information
f01dab1e committed Oct 12, 2023
1 parent 88682da commit 6c26eed
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum ExpressionKind {
Variable(Path),
Tuple(Vec<Expression>),
Lambda(Box<Lambda>),
Parentheses(Box<Expression>),
Error,
}

Expand Down Expand Up @@ -479,6 +480,7 @@ impl Display for ExpressionKind {
write!(f, "({})", elements.join(", "))
}
Lambda(lambda) => lambda.fmt(f),
Parentheses(subexpr) => write!(f, "({subexpr})"),

Check warning on line 483 in compiler/noirc_frontend/src/ast/expression.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)

Check warning on line 483 in compiler/noirc_frontend/src/ast/expression.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
Error => write!(f, "Error"),
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ impl<'a> Resolver<'a> {
captures: lambda_context.captures,
})
}),
ExpressionKind::Parentheses(subexpr) => return self.resolve_expression(*subexpr),

Check warning on line 1263 in compiler/noirc_frontend/src/hir/resolution/resolver.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)

Check warning on line 1263 in compiler/noirc_frontend/src/hir/resolution/resolver.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
};

let expr_id = self.interner.push_expr(hir_expr);
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,9 @@ where
literal(),
))
.map_with_span(Expression::new)
.or(parenthesized(expr_parser.clone()))
.or(parenthesized(expr_parser.clone()).map_with_span(|subexpr, span| {

Check warning on line 1557 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
Expression::new(ExpressionKind::Parentheses(subexpr.into()), span)

Check warning on line 1558 in compiler/noirc_frontend/src/parser/parser.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (subexpr)
}))
.or(tuple(expr_parser))
.labelled(ParsingRuleLabel::Atom)
}
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl FmtVisitor<'_> {
literal.to_string()
}
},
ExpressionKind::Parentheses(subexpr) => format!("({})", self.format_expr(*subexpr)),
// TODO:
_expr => slice!(self, span.start(), span.end()).to_string(),
}
Expand Down
8 changes: 8 additions & 0 deletions tooling/nargo_fmt/tests/expected/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ fn test() {
// 324
34
}

fn parentheses() {
value + (x as Field)
}

fn parentheses() {
(i as u8) + (j as u8) + (k as u8) + x + y + z
}
8 changes: 8 additions & 0 deletions tooling/nargo_fmt/tests/input/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,11 @@ fn test() {
// 324
34
}

fn parentheses() {
value + ( x as Field )
}

fn parentheses() {
( i as u8 ) + ( j as u8 ) + ( k as u8 ) + x + y + z
}

0 comments on commit 6c26eed

Please sign in to comment.