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

feat: add FunctionDef::body #5825

Merged
merged 55 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
4267688
Add `FunctionDef::body`
asterite Aug 26, 2024
dae279a
WIP: add aztec_macro_add_context example
asterite Aug 26, 2024
1c50a48
Be able to turn ExpressionKind into tokens
asterite Aug 26, 2024
9440f9d
Be able to turn StatementKind into tokens
asterite Aug 26, 2024
6b4404a
`Quoted::as_expr` now works with statements and lvalue
asterite Aug 26, 2024
c7b13fc
Map call arguments
asterite Aug 26, 2024
4fd315a
Add a TODO
asterite Aug 26, 2024
ed93bac
clippy
asterite Aug 26, 2024
af11fc6
Add a TODO
asterite Aug 26, 2024
fa9d119
Move test program
asterite Aug 26, 2024
8d63ff2
Revert "Move test program"
asterite Aug 26, 2024
16d3b9e
Renames
asterite Aug 27, 2024
83248f9
Let `FunctionDef::body` return a single expression
asterite Aug 27, 2024
0a05fc8
Use `map`
asterite Aug 27, 2024
f5dc0f5
Add `Expr::quoted`
asterite Aug 27, 2024
269075c
Put semicolons in block expression
asterite Aug 27, 2024
93954e1
Rename test program
asterite Aug 27, 2024
0403574
Rename `aztec` to `inject_context`
asterite Aug 27, 2024
85d7fb3
Make `_context` the first argument
asterite Aug 27, 2024
32d411e
Let `println` work well with resolved and interned values
asterite Aug 27, 2024
4491a5e
Intern LValue too
asterite Aug 27, 2024
49613a3
Unwrap interned values in `expr_as`
asterite Aug 27, 2024
4158665
`Expr::map` for method call
asterite Aug 27, 2024
d133943
Add a test to map an integer
asterite Aug 27, 2024
06031f3
`Expr::map` for binary op
asterite Aug 27, 2024
c01b1bc
Intern UnresolvedTypeData
asterite Aug 27, 2024
618eac5
`Expr::map` for comptime
asterite Aug 27, 2024
bd37b78
Expr::map for function call
asterite Aug 27, 2024
986ff46
Expr::map for if
asterite Aug 27, 2024
e911da0
Expr::map for index
asterite Aug 27, 2024
a0df942
Expr::map for member access
asterite Aug 27, 2024
5e5c4b6
Quoted is a type
asterite Aug 27, 2024
b136e05
Expr::map for repeated array element
asterite Aug 27, 2024
f0b8471
Expr::map for repeated element slice
asterite Aug 27, 2024
62f3d36
Expr::map for slice
asterite Aug 27, 2024
850eaa0
Expr::map for tuple
asterite Aug 27, 2024
faad8c8
Expr::map for unary op
asterite Aug 27, 2024
86e3791
Expr::map for unsafe
asterite Aug 27, 2024
1fbe2b6
Add docs for Expr functions
asterite Aug 27, 2024
bb65ee3
Add docs for operators
asterite Aug 27, 2024
4eba2f8
Fix docs
asterite Aug 27, 2024
b2420dd
Merge branch 'master' into ab/aztec-macro-add-context
asterite Aug 27, 2024
16e3f1d
Remove TODO
asterite Aug 28, 2024
3cdbeda
map -> mutate
asterite Aug 28, 2024
61b8d07
map -> mutate in test program
asterite Aug 28, 2024
dff525a
Put parenthesis around operands
asterite Aug 28, 2024
36b825e
Let `FunctionDef::set_body` take an `Expr`
asterite Aug 28, 2024
e58b761
clippy
asterite Aug 28, 2024
e9b98af
Use zeroed for unreachable branches
asterite Aug 28, 2024
222e681
Use `crate::` inside std
asterite Aug 28, 2024
d27c93d
Let `Expr::mutate` always recur
asterite Aug 28, 2024
882f647
Update docs
asterite Aug 28, 2024
8781b14
Use `map`
asterite Aug 28, 2024
e99b17a
Have to call `f(self)` for expressions that don't have sub-expressions
asterite Aug 28, 2024
59cc511
resolve -> remove_interned_in
asterite Aug 28, 2024
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
10 changes: 8 additions & 2 deletions aztec_macros/src/utils/parse_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@
StatementKind::For(for_loop_statement) => empty_for_loop_statement(for_loop_statement),
StatementKind::Comptime(statement) => empty_statement(statement),
StatementKind::Semi(expression) => empty_expression(expression),
StatementKind::Break | StatementKind::Continue | StatementKind::Error => (),
StatementKind::Break
| StatementKind::Continue
| StatementKind::Resolved(_)
| StatementKind::Error => (),
}
}

Expand Down Expand Up @@ -271,12 +274,15 @@
ExpressionKind::Unsafe(block_expression, _span) => {
empty_block_expression(block_expression);
}
ExpressionKind::Quote(..) | ExpressionKind::Resolved(_) | ExpressionKind::Error => (),
ExpressionKind::AsTraitPath(path) => {
empty_unresolved_type(&mut path.typ);
empty_path(&mut path.trait_path);
empty_ident(&mut path.impl_item);
}
ExpressionKind::Quote(..)
| ExpressionKind::Resolved(_)
| ExpressionKind::ResolvedQuoted(_)
| ExpressionKind::Error => (),
}
}

Expand Down Expand Up @@ -397,9 +403,9 @@
}

fn empty_unresolved_trait_constraints(
unresolved_trait_constriants: &mut [UnresolvedTraitConstraint],

Check warning on line 406 in aztec_macros/src/utils/parse_utils.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (constriants)
) {
for trait_constraint in unresolved_trait_constriants.iter_mut() {

Check warning on line 408 in aztec_macros/src/utils/parse_utils.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (constriants)
empty_unresolved_trait_constraint(trait_constraint);
}
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ast::{
};
use crate::hir::def_collector::errors::DefCollectorErrorKind;
use crate::macros_api::StructId;
use crate::node_interner::{ExprId, QuotedTypeId};
use crate::node_interner::{ExprId, QuotedExprId, QuotedTypeId};
use crate::token::{Attributes, FunctionAttribute, Token, Tokens};
use crate::{Kind, Type};
use acvm::{acir::AcirField, FieldElement};
Expand Down Expand Up @@ -43,6 +43,7 @@ pub enum ExpressionKind {
// code. It is used to translate function values back into the AST while
// guaranteeing they have the same instantiated type and definition id without resolving again.
Resolved(ExprId),
ResolvedQuoted(QuotedExprId),
asterite marked this conversation as resolved.
Show resolved Hide resolved
Error,
}

Expand Down Expand Up @@ -603,6 +604,7 @@ impl Display for ExpressionKind {
Unsafe(block, _) => write!(f, "unsafe {block}"),
Error => write!(f, "Error"),
Resolved(_) => write!(f, "?Resolved"),
ResolvedQuoted(_) => write!(f, "?ResolvedQuoted"),
Unquote(expr) => write!(f, "$({expr})"),
Quote(tokens) => {
let tokens = vecmap(&tokens.0, ToString::to_string);
Expand Down
6 changes: 6 additions & 0 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{
use crate::elaborator::types::SELF_TYPE_NAME;
use crate::lexer::token::SpannedToken;
use crate::macros_api::{SecondaryAttribute, UnresolvedTypeData};
use crate::node_interner::QuotedStatementId;
use crate::parser::{ParserError, ParserErrorReason};
use crate::token::Token;

Expand Down Expand Up @@ -45,6 +46,7 @@ pub enum StatementKind {
Comptime(Box<Statement>),
// This is an expression with a trailing semi-colon
Semi(Expression),
Resolved(QuotedStatementId),
// This statement is the result of a recovered parse error.
// To avoid issuing multiple errors in later steps, it should
// be skipped in any future analysis if possible.
Expand Down Expand Up @@ -97,6 +99,9 @@ impl StatementKind {
// A semicolon on a for loop is optional and does nothing
StatementKind::For(_) => self,

// No semicolon needed for a resolved statement
StatementKind::Resolved(_) => self,

StatementKind::Expression(expr) => {
match (&expr.kind, semi, last_statement_in_block) {
// Semicolons are optional for these expressions
Expand Down Expand Up @@ -777,6 +782,7 @@ impl Display for StatementKind {
StatementKind::Continue => write!(f, "continue"),
StatementKind::Comptime(statement) => write!(f, "comptime {}", statement.kind),
StatementKind::Semi(semi) => write!(f, "{semi};"),
StatementKind::Resolved(_) => write!(f, "(resolved);"),
StatementKind::Error => write!(f, "Error"),
}
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ impl<'context> Elaborator<'context> {
self.elaborate_unsafe_block(block_expression)
}
ExpressionKind::Resolved(id) => return (id, self.interner.id_type(id)),
ExpressionKind::ResolvedQuoted(id) => {
let expr_kind = self.interner.get_quoted_expr(id);
let expr = Expression::new(expr_kind.clone(), expr.span);
return self.elaborate_expression(expr);
}
ExpressionKind::Error => (HirExpression::Error, Type::Error),
ExpressionKind::Unquote(_) => {
self.push_err(ResolverError::UnquoteUsedOutsideQuote { span: expr.span });
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ impl<'context> Elaborator<'context> {
let (expr, _typ) = self.elaborate_expression(expr);
(HirStatement::Semi(expr), Type::Unit)
}
StatementKind::Resolved(id) => {
let kind = self.interner.get_quoted_statement(id);
let statement = Statement { kind: kind.clone(), span: statement.span };
self.elaborate_statement_value(statement)
asterite marked this conversation as resolved.
Show resolved Hide resolved
}
StatementKind::Error => (HirStatement::Error, Type::Error),
}
}
Expand Down
28 changes: 25 additions & 3 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
mutate_func_meta_type, parse, parse_tokens, replace_func_meta_parameters,
replace_func_meta_return_type,
};
use chumsky::{prelude::choice, Parser};
use im::Vector;
use iter_extended::{try_vecmap, vecmap};
use noirc_errors::Location;
Expand Down Expand Up @@ -80,6 +81,7 @@
"expr_is_break" => expr_is_break(arguments, location),
"expr_is_continue" => expr_is_continue(arguments, location),
"is_unconstrained" => Ok(Value::Bool(true)),
"function_def_body" => function_def_body(interner, arguments, location),
"function_def_name" => function_def_name(interner, arguments, location),
"function_def_parameters" => function_def_parameters(interner, arguments, location),
"function_def_return_type" => function_def_return_type(interner, arguments, location),
Expand Down Expand Up @@ -361,10 +363,14 @@
) -> IResult<Value> {
let argument = check_one_argument(arguments, location)?;

let expr = parse(argument, parser::expression(), "an expression").ok();
let value = expr.map(|expr| Value::expression(expr.kind));
let expr_parser = parser::expression().map(|expr| Value::expression(expr.kind));
let statement_parser = parser::fresh_statement().map(Value::statement);
let lvalue_parser = parser::lvalue(parser::expression()).map(Value::lvalue);
let parser = choice((expr_parser, statement_parser, lvalue_parser));

option(return_type, value)
let expr = parse(argument, parser, "an expression").ok();

option(return_type, expr)
}

// fn as_module(quoted: Quoted) -> Option<Module>
Expand Down Expand Up @@ -413,7 +419,7 @@
let argument = check_one_argument(arguments, location)?;
let typ = parse(argument, parser::parse_type(), "a type")?;
let typ =
interpreter.elaborate_item(interpreter.current_function, |elab| elab.resolve_type(typ));

Check warning on line 422 in compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)

Check warning on line 422 in compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)
Ok(Value::Type(typ))
}

Expand Down Expand Up @@ -1272,6 +1278,22 @@
option(return_type, option_value)
}

// fn body(self) -> [Expr]
asterite marked this conversation as resolved.
Show resolved Hide resolved
fn function_def_body(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let func_id = get_function_def(self_argument)?;
let func_meta = interner.function_meta(&func_id);
if let FunctionBody::Unresolved(_, block_expr, _) = &func_meta.function_body {
Ok(block_expression_to_value(block_expr.clone()))
} else {
Err(InterpreterError::FunctionAlreadyResolved { location })
}
}

// fn name(self) -> Quoted
fn function_def_name(
interner: &NodeInterner,
Expand Down
6 changes: 6 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
Value::Expr(ExprValue::Statement(statement))
}

pub(crate) fn lvalue(lvaue: LValue) -> Self {

Check warning on line 88 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
Value::Expr(ExprValue::LValue(lvaue))

Check warning on line 89 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
}

pub(crate) fn get_type(&self) -> Cow<Type> {
Expand Down Expand Up @@ -417,6 +417,12 @@
let token = match self {
Value::Quoted(tokens) => return Ok(unwrap_rc(tokens)),
Value::Type(typ) => Token::QuotedType(interner.push_quoted_type(typ)),
Value::Expr(ExprValue::Expression(expr)) => {
Token::QuotedExpr(interner.push_quoted_expr(expr))
}
Value::Expr(ExprValue::Statement(statement)) => {
Token::QuotedStatement(interner.push_quoted_statement(statement))
}
other => Token::UnquoteMarker(other.into_hir_expression(interner, location)?),
};
Ok(vec![token])
Expand Down
19 changes: 17 additions & 2 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{fmt, iter::Map, vec::IntoIter};

use crate::{
lexer::errors::LexerErrorKind,
node_interner::{ExprId, QuotedTypeId},
node_interner::{ExprId, QuotedExprId, QuotedStatementId, QuotedTypeId},
};

/// Represents a token in noir's grammar - a word, number,
Expand All @@ -28,6 +28,8 @@ pub enum BorrowedToken<'input> {
BlockComment(&'input str, Option<DocStyle>),
Quote(&'input Tokens),
QuotedType(QuotedTypeId),
QuotedExpr(QuotedExprId),
QuotedStatement(QuotedStatementId),
/// <
Less,
/// <=
Expand Down Expand Up @@ -134,6 +136,10 @@ pub enum Token {
/// to avoid having to tokenize it, re-parse it, and re-resolve it which
/// may change the underlying type.
QuotedType(QuotedTypeId),
/// Similar to QuotedType but for `ExpressionKind`.
QuotedExpr(QuotedExprId),
/// Similar to QuotedType but for `StatementKind`.
QuotedStatement(QuotedStatementId),
/// <
Less,
/// <=
Expand Down Expand Up @@ -233,6 +239,8 @@ pub fn token_to_borrowed_token(token: &Token) -> BorrowedToken<'_> {
Token::BlockComment(ref s, _style) => BorrowedToken::BlockComment(s, *_style),
Token::Quote(stream) => BorrowedToken::Quote(stream),
Token::QuotedType(id) => BorrowedToken::QuotedType(*id),
Token::QuotedExpr(id) => BorrowedToken::QuotedExpr(*id),
Token::QuotedStatement(id) => BorrowedToken::QuotedStatement(*id),
Token::IntType(ref i) => BorrowedToken::IntType(i.clone()),
Token::Less => BorrowedToken::Less,
Token::LessEqual => BorrowedToken::LessEqual,
Expand Down Expand Up @@ -353,8 +361,9 @@ impl fmt::Display for Token {
}
write!(f, "}}")
}
// Quoted types only have an ID so there is nothing to display
// Quoted types and exprs only have an ID so there is nothing to display
Token::QuotedType(_) => write!(f, "(type)"),
Token::QuotedExpr(_) | Token::QuotedStatement(_) => write!(f, "(expr)"),
Token::IntType(ref i) => write!(f, "{i}"),
Token::Less => write!(f, "<"),
Token::LessEqual => write!(f, "<="),
Expand Down Expand Up @@ -407,6 +416,8 @@ pub enum TokenKind {
Attribute,
Quote,
QuotedType,
QuotedExpr,
QuotedStatement,
UnquoteMarker,
}

Expand All @@ -420,6 +431,8 @@ impl fmt::Display for TokenKind {
TokenKind::Attribute => write!(f, "attribute"),
TokenKind::Quote => write!(f, "quote"),
TokenKind::QuotedType => write!(f, "quoted type"),
TokenKind::QuotedExpr => write!(f, "quoted expr"),
TokenKind::QuotedStatement => write!(f, "quoted statement"),
TokenKind::UnquoteMarker => write!(f, "macro result"),
}
}
Expand All @@ -439,6 +452,8 @@ impl Token {
Token::UnquoteMarker(_) => TokenKind::UnquoteMarker,
Token::Quote(_) => TokenKind::Quote,
Token::QuotedType(_) => TokenKind::QuotedType,
Token::QuotedExpr(_) => TokenKind::QuotedExpr,
Token::QuotedStatement(_) => TokenKind::QuotedStatement,
tok => TokenKind::Token(tok.clone()),
}
}
Expand Down
32 changes: 32 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use petgraph::prelude::NodeIndex as PetGraphIndex;
use rustc_hash::FxHashMap as HashMap;

use crate::ast::ExpressionKind;
use crate::ast::Ident;
use crate::ast::StatementKind;
use crate::graph::CrateId;
use crate::hir::comptime;
use crate::hir::def_collector::dc_crate::CompilationError;
Expand Down Expand Up @@ -208,7 +210,13 @@
/// the actual type since types do not implement Send or Sync.
quoted_types: noirc_arena::Arena<Type>,

/// Similar to `quoted_types` but for ExpressionKind.
quoted_exprs: noirc_arena::Arena<ExpressionKind>,

/// Similar to `quoted_types` but for StatementKind.
quoted_statements: noirc_arena::Arena<StatementKind>,
asterite marked this conversation as resolved.
Show resolved Hide resolved

/// Determins whether to run in LSP mode. In LSP mode references are tracked.

Check warning on line 219 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Determins)
pub(crate) lsp_mode: bool,

/// Store the location of the references in the graph.
Expand Down Expand Up @@ -573,6 +581,12 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct QuotedTypeId(noirc_arena::Index);

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct QuotedExprId(noirc_arena::Index);

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct QuotedStatementId(noirc_arena::Index);

impl Default for NodeInterner {
fn default() -> Self {
NodeInterner {
Expand Down Expand Up @@ -610,6 +624,8 @@
type_alias_ref: Vec::new(),
type_ref_locations: Vec::new(),
quoted_types: Default::default(),
quoted_exprs: Default::default(),
quoted_statements: Default::default(),
lsp_mode: false,
location_indices: LocationIndices::default(),
reference_graph: petgraph::graph::DiGraph::new(),
Expand Down Expand Up @@ -2017,6 +2033,22 @@
&self.quoted_types[id.0]
}

pub fn push_quoted_expr(&mut self, expr: ExpressionKind) -> QuotedExprId {
QuotedExprId(self.quoted_exprs.insert(expr))
}

pub fn get_quoted_expr(&self, id: QuotedExprId) -> &ExpressionKind {
&self.quoted_exprs[id.0]
}

pub fn push_quoted_statement(&mut self, statement: StatementKind) -> QuotedStatementId {
QuotedStatementId(self.quoted_statements.insert(statement))
}

pub fn get_quoted_statement(&self, id: QuotedStatementId) -> &StatementKind {
&self.quoted_statements[id.0]
}

/// Returns the type of an operator (which is always a function), along with its return type.
pub fn get_infix_operator_type(
&self,
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use noirc_errors::Span;
pub use parser::path::path_no_turbofish;
pub use parser::traits::trait_bound;
pub use parser::{
block, expression, fresh_statement, parse_program, parse_type, pattern, top_level_items,
block, expression, fresh_statement, lvalue, parse_program, parse_type, pattern, top_level_items,
};

#[derive(Debug, Clone)]
Expand Down
15 changes: 13 additions & 2 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

use chumsky::prelude::*;
use iter_extended::vecmap;
use lalrpop_util::lalrpop_mod;

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)
use noirc_errors::{Span, Spanned};

mod assertion;
Expand All @@ -65,7 +65,7 @@
pub(super) mod traits;
mod types;

// synthesized by LALRPOP

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (LALRPOP)
lalrpop_mod!(pub noir_parser);

#[cfg(test)]
Expand All @@ -73,7 +73,9 @@

use literals::literal;
use path::{maybe_empty_path, path};
use primitives::{dereference, ident, negation, not, nothing, right_shift_operator, token_kind};
use primitives::{
dereference, ident, negation, not, nothing, resolved_expr, right_shift_operator, token_kind,
};
use traits::where_clause;

/// Entry function for the parser - also handles lexing internally.
Expand Down Expand Up @@ -487,6 +489,7 @@
continue_statement(),
return_statement(expr_parser.clone()),
comptime_statement(expr_parser.clone(), expr_no_constructors, statement),
resolved_statement(),
expr_parser.map(StatementKind::Expression),
))
})
Expand Down Expand Up @@ -526,6 +529,13 @@
keyword(Keyword::Comptime).ignore_then(comptime_statement).map(StatementKind::Comptime)
}

pub(super) fn resolved_statement() -> impl NoirParser<StatementKind> {
token_kind(TokenKind::QuotedStatement).map(|token| match token {
Token::QuotedStatement(id) => StatementKind::Resolved(id),
_ => unreachable!("token_kind(QuotedStatement) guarantees we parse a quoted statement"),
})
}

/// Comptime in an expression position only accepts entire blocks
fn comptime_expr<'a, S>(statement: S) -> impl NoirParser<ExpressionKind> + 'a
where
Expand Down Expand Up @@ -642,7 +652,7 @@
Index(Expression, Span),
}

fn lvalue<'a, P>(expr_parser: P) -> impl NoirParser<LValue> + 'a
pub fn lvalue<'a, P>(expr_parser: P) -> impl NoirParser<LValue> + 'a
where
P: ExprParser + 'a,
{
Expand Down Expand Up @@ -1154,6 +1164,7 @@
literal(),
as_trait_path(parse_type()).map(ExpressionKind::AsTraitPath),
macro_quote_marker(),
resolved_expr(),
))
.map_with_span(Expression::new)
.or(parenthesized(expr_parser.clone()).map_with_span(|sub_expr, span| {
Expand Down
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ pub(super) fn macro_quote_marker() -> impl NoirParser<ExpressionKind> {
})
}

pub(super) fn resolved_expr() -> impl NoirParser<ExpressionKind> {
token_kind(TokenKind::QuotedExpr).map(|token| match token {
Token::QuotedExpr(id) => ExpressionKind::ResolvedQuoted(id),
_ => unreachable!("token_kind(QuotedExpr) guarantees we parse a quoted expr"),
})
}

#[cfg(test)]
mod test {
use crate::parser::parser::{
Expand Down
Loading
Loading