Skip to content

Commit

Permalink
feat: add Expr::as_any_integer and Expr::as_member_access (#5742)
Browse files Browse the repository at this point in the history
# Description

## Problem

Part of #5668

## Summary

## Additional Context

## Documentation

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [x] **[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
asterite authored Aug 16, 2024
1 parent a32f62a commit 6266755
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"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 @@ -839,6 +841,37 @@ fn expr_as_index(
})
}

// 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

0 comments on commit 6266755

Please sign in to comment.