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_unary #5731

Merged
merged 2 commits into from
Aug 15, 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
42 changes: 40 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

use crate::{
ast::{
ExpressionKind, FunctionKind, FunctionReturnType, IntegerBitSize, Literal, UnresolvedType,
UnresolvedTypeData, Visibility,
ExpressionKind, FunctionKind, FunctionReturnType, IntegerBitSize, Literal, UnaryOp,
UnresolvedType, UnresolvedTypeData, Visibility,
},
hir::comptime::{errors::IResult, value::add_token_spans, InterpreterError, Value},
hir_def::function::FunctionBody,
Expand Down Expand Up @@ -51,6 +51,7 @@
"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_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)),
"function_def_name" => function_def_name(interner, arguments, location),
Expand Down Expand Up @@ -384,7 +385,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 388 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 388 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 @@ -837,6 +838,43 @@
})
}

// fn as_unary_op(self) -> Option<(UnaryOp, Expr)>
fn expr_as_unary_op(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
expr_as(arguments, return_type.clone(), location, |expr| {
if let ExpressionKind::Prefix(prefix_expr) = expr {
let option_type = extract_option_generic_type(return_type);
let Type::Tuple(mut tuple_types) = option_type else {
panic!("Expected the return type option generic arg to be a tuple");
};
assert_eq!(tuple_types.len(), 2);

tuple_types.pop().unwrap();
let unary_op_type = tuple_types.pop().unwrap();

// These values should match the values used in noir_stdlib/src/meta/op.nr
let unary_op_value = match prefix_expr.operator {
UnaryOp::Minus => 0_u128,
UnaryOp::Not => 1_u128,
UnaryOp::MutableReference => 2_u128,
UnaryOp::Dereference { .. } => 3_u128,
};

let mut fields = HashMap::default();
fields.insert(Rc::new("op".to_string()), Value::Field(unary_op_value.into()));

let unary_op = Value::Struct(fields, unary_op_type);
let rhs = Value::Expr(prefix_expr.rhs.kind);
Some(Value::Tuple(vec![unary_op, rhs]))
} else {
None
}
})
}

// fn as_tuple(self) -> Option<[Expr]>
fn expr_as_tuple(
arguments: Vec<(Value, Location)>,
Expand Down
4 changes: 4 additions & 0 deletions noir_stdlib/src/meta/expr.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::option::Option;
use crate::meta::op::UnaryOp;

impl Expr {
#[builtin(expr_as_bool)]
Expand All @@ -15,4 +16,7 @@ impl Expr {

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

#[builtin(expr_as_unary_op)]
fn as_unary_op(self) -> Option<(UnaryOp, Expr)> {}
}
1 change: 1 addition & 0 deletions noir_stdlib/src/meta/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::hash::poseidon2::Poseidon2Hasher;
mod expr;
mod function_def;
mod module;
mod op;
mod struct_def;
mod trait_constraint;
mod trait_def;
Expand Down
21 changes: 21 additions & 0 deletions noir_stdlib/src/meta/op.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
struct UnaryOp {
op: Field
}

impl UnaryOp {
fn is_minus(self) -> bool {
self.op == 0
}

fn is_not(self) -> bool {
self.op == 1
}

fn is_mutable_reference(self) -> bool {
self.op == 2
}

fn is_dereference(self) -> bool {
self.op == 3
}
}
17 changes: 17 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 @@ -34,5 +34,22 @@ fn main() {

let expr = quote { true }.as_expr().unwrap();
assert_eq(expr.as_bool().unwrap(), true);

// Check Expr::as_unary_op
let expr = quote { -x }.as_expr().unwrap();
let (op, _) = expr.as_unary_op().unwrap();
assert(op.is_minus());

let expr = quote { !x }.as_expr().unwrap();
let (op, _) = expr.as_unary_op().unwrap();
assert(op.is_not());

let expr = quote { &mut x }.as_expr().unwrap();
let (op, _) = expr.as_unary_op().unwrap();
assert(op.is_mutable_reference());

let expr = quote { *x }.as_expr().unwrap();
let (op, _) = expr.as_unary_op().unwrap();
assert(op.is_dereference());
}
}
Loading