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 Expr::as_integer and Expr::as_member_access #5742

Merged
merged 3 commits into from
Aug 16, 2024
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
33 changes: 33 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"expr_as_function_call" => expr_as_function_call(arguments, return_type, location),
"expr_as_if" => expr_as_if(arguments, return_type, location),
"expr_as_index" => expr_as_index(arguments, return_type, location),
"expr_as_integer" => expr_as_integer(arguments, return_type, location),
"expr_as_member_access" => expr_as_member_access(arguments, return_type, location),
"expr_as_unary_op" => expr_as_unary_op(arguments, return_type, location),
"expr_as_tuple" => expr_as_tuple(arguments, return_type, location),
"is_unconstrained" => Ok(Value::Bool(true)),
Expand Down Expand Up @@ -386,7 +388,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 391 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 391 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 @@ -839,6 +841,37 @@
})
}

// fn as_integer(self) -> Option<(Field, bool)>
fn expr_as_integer(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
expr_as(arguments, return_type.clone(), location, |expr| {
if let ExpressionKind::Literal(Literal::Integer(field, sign)) = expr {
Some(Value::Tuple(vec![Value::Field(field), Value::Bool(sign)]))
} else {
None
}
})
}

// fn as_member_access(self) -> Option<(Expr, Quoted)>
fn expr_as_member_access(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
expr_as(arguments, return_type, location, |expr| {
if let ExpressionKind::MemberAccess(member_access) = expr {
let tokens = Rc::new(vec![Token::Ident(member_access.rhs.0.contents.clone())]);
Some(Value::Tuple(vec![Value::Expr(member_access.lhs.kind), Value::Quoted(tokens)]))
} else {
None
}
})
}

// fn as_unary_op(self) -> Option<(UnaryOp, Expr)>
fn expr_as_unary_op(
arguments: Vec<(Value, Location)>,
Expand Down
6 changes: 6 additions & 0 deletions noir_stdlib/src/meta/expr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use crate::meta::op::UnaryOp;
use crate::meta::op::BinaryOp;

impl Expr {
#[builtin(expr_as_integer)]
fn as_integer(self) -> Option<(Field, bool)> {}

#[builtin(expr_as_binary_op)]
fn as_binary_op(self) -> Option<(Expr, BinaryOp, Expr)> {}

Expand All @@ -18,6 +21,9 @@ impl Expr {
#[builtin(expr_as_index)]
fn as_index(self) -> Option<(Expr, Expr)> {}

#[builtin(expr_as_member_access)]
fn as_member_access(self) -> Option<(Expr, Quoted)> {}

#[builtin(expr_as_tuple)]
fn as_tuple(self) -> Option<[Expr]> {}

Expand Down
12 changes: 12 additions & 0 deletions test_programs/compile_success_empty/comptime_exp/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ fn main() {
assert(get_binary_op(quote { x >> y }).is_shift_right());
assert(get_binary_op(quote { x << y }).is_shift_left());
assert(get_binary_op(quote { x % y }).is_modulo());

// Check Expr::as_integer
let expr = quote { 1 }.as_expr().unwrap();
assert_eq((1, false), expr.as_integer().unwrap());

let expr = quote { -2 }.as_expr().unwrap();
assert_eq((2, true), expr.as_integer().unwrap());

// Check Expr::as_member_access
let expr = quote { foo.bar }.as_expr().unwrap();
let (_, name) = expr.as_member_access().unwrap();
assert_eq(name, quote { bar });
}
}

Expand Down
Loading