Skip to content

Commit

Permalink
chore: Parse macros (#5229)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

## Summary\*

To try to make it easier to review I'm breaking the parser related
changes here out #5203.

## Additional Context

It turns out `parser/types.rs` was never in our module tree. I've added
it and removed the duplicate parsers for parsing types that were in
`parser/parser.rs`. They've had some changes since the copies in
`parser/types.rs` were first changed so I've updated those as well.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
jfecher authored Jun 12, 2024
1 parent 802cd95 commit 939bad2
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 299 deletions.
7 changes: 6 additions & 1 deletion aztec_macros/src/utils/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ pub fn method_call(
object,
method_name: ident(method_name),
arguments,
is_macro_call: false,
generics: None,
})))
}

pub fn call(func: Expression, arguments: Vec<Expression>) -> Expression {
expression(ExpressionKind::Call(Box::new(CallExpression { func: Box::new(func), arguments })))
expression(ExpressionKind::Call(Box::new(CallExpression {
func: Box::new(func),
is_macro_call: false,
arguments,
})))
}

pub fn pattern(name: &str) -> Pattern {
Expand Down
41 changes: 27 additions & 14 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum ExpressionKind {
Tuple(Vec<Expression>),
Lambda(Box<Lambda>),
Parenthesized(Box<Expression>),
Quote(BlockExpression),
Quote(BlockExpression, Span),
Comptime(BlockExpression, Span),

// This variant is only emitted when inlining the result of comptime
Expand Down Expand Up @@ -179,19 +179,21 @@ impl Expression {

pub fn member_access_or_method_call(
lhs: Expression,
(rhs, args): UnaryRhsMemberAccess,
rhs: UnaryRhsMemberAccess,
span: Span,
) -> Expression {
let kind = match args {
None => ExpressionKind::MemberAccess(Box::new(MemberAccessExpression { lhs, rhs })),
Some((generics, arguments)) => {
ExpressionKind::MethodCall(Box::new(MethodCallExpression {
object: lhs,
method_name: rhs,
generics,
arguments,
}))
let kind = match rhs.method_call {
None => {
let rhs = rhs.method_or_field;
ExpressionKind::MemberAccess(Box::new(MemberAccessExpression { lhs, rhs }))
}
Some(method_call) => ExpressionKind::MethodCall(Box::new(MethodCallExpression {
object: lhs,
method_name: rhs.method_or_field,
generics: method_call.turbofish,
arguments: method_call.args,
is_macro_call: method_call.macro_call,
})),
};
Expression::new(kind, span)
}
Expand All @@ -206,7 +208,12 @@ impl Expression {
Expression::new(kind, span)
}

pub fn call(lhs: Expression, arguments: Vec<Expression>, span: Span) -> Expression {
pub fn call(
lhs: Expression,
is_macro_call: bool,
arguments: Vec<Expression>,
span: Span,
) -> Expression {
// Need to check if lhs is an if expression since users can sequence if expressions
// with tuples without calling them. E.g. `if c { t } else { e }(a, b)` is interpreted
// as a sequence of { if, tuple } rather than a function call. This behavior matches rust.
Expand All @@ -224,7 +231,11 @@ impl Expression {
],
})
} else {
ExpressionKind::Call(Box::new(CallExpression { func: Box::new(lhs), arguments }))
ExpressionKind::Call(Box::new(CallExpression {
func: Box::new(lhs),
is_macro_call,
arguments,
}))
};
Expression::new(kind, span)
}
Expand Down Expand Up @@ -447,6 +458,7 @@ pub enum ArrayLiteral {
pub struct CallExpression {
pub func: Box<Expression>,
pub arguments: Vec<Expression>,
pub is_macro_call: bool,
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand All @@ -456,6 +468,7 @@ pub struct MethodCallExpression {
/// Method calls have an optional list of generics if the turbofish operator was used
pub generics: Option<Vec<UnresolvedType>>,
pub arguments: Vec<Expression>,
pub is_macro_call: bool,
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -535,7 +548,7 @@ impl Display for ExpressionKind {
}
Lambda(lambda) => lambda.fmt(f),
Parenthesized(sub_expr) => write!(f, "({sub_expr})"),
Quote(block) => write!(f, "quote {block}"),
Quote(block, _) => write!(f, "quote {block}"),
Comptime(block, _) => write!(f, "comptime {block}"),
Error => write!(f, "Error"),
Resolved(_) => write!(f, "?Resolved"),
Expand Down
12 changes: 10 additions & 2 deletions compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,16 @@ pub struct UnresolvedType {
}

/// Type wrapper for a member access
pub(crate) type UnaryRhsMemberAccess =
(Ident, Option<(Option<Vec<UnresolvedType>>, Vec<Expression>)>);
pub struct UnaryRhsMemberAccess {
pub method_or_field: Ident,
pub method_call: Option<UnaryRhsMethodCall>,
}

pub struct UnaryRhsMethodCall {
pub turbofish: Option<Vec<UnresolvedType>>,
pub macro_call: bool,
pub args: Vec<Expression>,
}

/// The precursor to TypeExpression, this is the type that the parser allows
/// to be used in the length position of an array type. Only constants, variables,
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ impl ForRange {
object: Expression::new(array_ident.clone(), array_span),
method_name: Ident::new("len".to_string(), array_span),
generics: None,
is_macro_call: false,
arguments: vec![],
}));
let end_range = Expression::new(end_range, array_span);
Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ fn build_assign_var_stmt(var_id: SourceVarId, expr: ast::Expression) -> ast::Sta
),
span,
}),
is_macro_call: false,
arguments: vec![uint_expr(var_id.0 as u128, span), expr],
}));
ast::Statement { kind: ast::StatementKind::Semi(ast::Expression { kind, span }), span }
Expand All @@ -599,6 +600,7 @@ fn build_drop_var_stmt(var_id: SourceVarId, span: Span) -> ast::Statement {
),
span,
}),
is_macro_call: false,
arguments: vec![uint_expr(var_id.0 as u128, span)],
}));
ast::Statement { kind: ast::StatementKind::Semi(ast::Expression { kind, span }), span }
Expand Down Expand Up @@ -626,6 +628,7 @@ fn build_assign_member_stmt(
),
span,
}),
is_macro_call: false,
arguments: [
vec![uint_expr(var_id.0 as u128, span)],
vec![expr.clone()],
Expand All @@ -649,6 +652,7 @@ fn build_debug_call_stmt(fname: &str, fn_id: DebugFnId, span: Span) -> ast::Stat
),
span,
}),
is_macro_call: false,
arguments: vec![uint_expr(fn_id.0 as u128, span)],
}));
ast::Statement { kind: ast::StatementKind::Semi(ast::Expression { kind, span }), span }
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'context> Elaborator<'context> {
ExpressionKind::Tuple(tuple) => self.elaborate_tuple(tuple),
ExpressionKind::Lambda(lambda) => self.elaborate_lambda(*lambda),
ExpressionKind::Parenthesized(expr) => return self.elaborate_expression(*expr),
ExpressionKind::Quote(quote) => self.elaborate_quote(quote),
ExpressionKind::Quote(quote, _) => self.elaborate_quote(quote),
ExpressionKind::Comptime(comptime, _) => {
return self.elaborate_comptime_block(comptime, expr.span)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ impl<'a> Resolver<'a> {
ExpressionKind::Parenthesized(sub_expr) => return self.resolve_expression(*sub_expr),

// The quoted expression isn't resolved since we don't want errors if variables aren't defined
ExpressionKind::Quote(block) => HirExpression::Quote(block),
ExpressionKind::Quote(block, _) => HirExpression::Quote(block),
ExpressionKind::Comptime(block, _) => {
HirExpression::Comptime(self.resolve_block(block))
}
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl<'a> Lexer<'a> {
Some('}') => self.single_char_token(Token::RightBrace),
Some('[') => self.single_char_token(Token::LeftBracket),
Some(']') => self.single_char_token(Token::RightBracket),
Some('$') => self.single_char_token(Token::DollarSign),
Some('"') => self.eat_string_literal(),
Some('f') => self.eat_format_string_or_alpha_numeric(),
Some('r') => self.eat_raw_string_or_alpha_numeric(),
Expand Down
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub enum BorrowedToken<'input> {
Semicolon,
/// !
Bang,
/// $
DollarSign,
/// =
Assign,
#[allow(clippy::upper_case_acronyms)]
Expand Down Expand Up @@ -179,6 +181,8 @@ pub enum Token {
Bang,
/// =
Assign,
/// $
DollarSign,
#[allow(clippy::upper_case_acronyms)]
EOF,

Expand Down Expand Up @@ -238,6 +242,7 @@ pub fn token_to_borrowed_token(token: &Token) -> BorrowedToken<'_> {
Token::Semicolon => BorrowedToken::Semicolon,
Token::Assign => BorrowedToken::Assign,
Token::Bang => BorrowedToken::Bang,
Token::DollarSign => BorrowedToken::DollarSign,
Token::EOF => BorrowedToken::EOF,
Token::Invalid(c) => BorrowedToken::Invalid(*c),
Token::Whitespace(ref s) => BorrowedToken::Whitespace(s),
Expand Down Expand Up @@ -349,6 +354,7 @@ impl fmt::Display for Token {
Token::Semicolon => write!(f, ";"),
Token::Assign => write!(f, "="),
Token::Bang => write!(f, "!"),
Token::DollarSign => write!(f, "$"),
Token::EOF => write!(f, "end of input"),
Token::Invalid(c) => write!(f, "{c}"),
Token::Whitespace(ref s) => write!(f, "{s}"),
Expand Down Expand Up @@ -840,6 +846,7 @@ pub enum Keyword {
Crate,
Dep,
Else,
Expr,
Field,
Fn,
For,
Expand Down Expand Up @@ -884,6 +891,7 @@ impl fmt::Display for Keyword {
Keyword::Crate => write!(f, "crate"),
Keyword::Dep => write!(f, "dep"),
Keyword::Else => write!(f, "else"),
Keyword::Expr => write!(f, "Expr"),
Keyword::Field => write!(f, "Field"),
Keyword::Fn => write!(f, "fn"),
Keyword::For => write!(f, "for"),
Expand Down Expand Up @@ -931,6 +939,7 @@ impl Keyword {
"crate" => Keyword::Crate,
"dep" => Keyword::Dep,
"else" => Keyword::Else,
"Expr" => Keyword::Expr,
"Field" => Keyword::Field,
"fn" => Keyword::Fn,
"for" => Keyword::For,
Expand Down
Loading

0 comments on commit 939bad2

Please sign in to comment.