Skip to content

Commit

Permalink
chore(fmt): format parentheses expression (#3132)
Browse files Browse the repository at this point in the history
  • Loading branch information
kek kek kek authored Oct 12, 2023
1 parent 45cbd24 commit 692aa0d
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>),
Parenthesized(Box<Expression>),
Error,
}

Expand Down Expand Up @@ -479,6 +480,7 @@ impl Display for ExpressionKind {
write!(f, "({})", elements.join(", "))
}
Lambda(lambda) => lambda.fmt(f),
Parenthesized(subexpr) => write!(f, "({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::Parenthesized(subexpr) => return self.resolve_expression(*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| {
Expression::new(ExpressionKind::Parenthesized(subexpr.into()), span)
}))
.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 @@ -95,6 +95,7 @@ impl FmtVisitor<'_> {
literal.to_string()
}
},
ExpressionKind::Parenthesized(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 parenthesized() {
value + (x as Field)
}

fn parenthesized() {
(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 parenthesized() {
value + ( x as Field )
}

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

0 comments on commit 692aa0d

Please sign in to comment.