From f626ff8e2bb879fd27d6f46de019ee0c5000d779 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 16 Aug 2024 15:05:04 -0300 Subject: [PATCH 1/3] Add `Expr::as_any_integer` --- .../src/hir/comptime/interpreter/builtin.rs | 16 ++++++++++++++++ noir_stdlib/src/meta/expr.nr | 3 +++ .../comptime_exp/src/main.nr | 7 +++++++ 3 files changed, 26 insertions(+) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 3449d402fbc..703f315677e 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -47,6 +47,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "array_as_str_unchecked" => array_as_str_unchecked(interner, arguments, location), "array_len" => array_len(interner, arguments, location), "as_slice" => as_slice(interner, arguments, location), + "expr_as_any_integer" => expr_as_any_integer(arguments, return_type, location), "expr_as_binary_op" => expr_as_binary_op(arguments, return_type, location), "expr_as_bool" => expr_as_bool(arguments, return_type, location), "expr_as_function_call" => expr_as_function_call(arguments, return_type, location), @@ -756,6 +757,21 @@ fn zeroed(return_type: Type) -> IResult { } } +// fn as_any_integer(self) -> Option<(Field, bool)> +fn expr_as_any_integer( + arguments: Vec<(Value, Location)>, + return_type: Type, + location: Location, +) -> IResult { + 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_bool(self) -> Option fn expr_as_bool( arguments: Vec<(Value, Location)>, diff --git a/noir_stdlib/src/meta/expr.nr b/noir_stdlib/src/meta/expr.nr index 2c25e20fe70..8e5ad47a4d8 100644 --- a/noir_stdlib/src/meta/expr.nr +++ b/noir_stdlib/src/meta/expr.nr @@ -3,6 +3,9 @@ use crate::meta::op::UnaryOp; use crate::meta::op::BinaryOp; impl Expr { + #[builtin(expr_as_any_integer)] + fn as_any_integer(self) -> Option<(Field, bool)> {} + #[builtin(expr_as_binary_op)] fn as_binary_op(self) -> Option<(Expr, BinaryOp, Expr)> {} diff --git a/test_programs/compile_success_empty/comptime_exp/src/main.nr b/test_programs/compile_success_empty/comptime_exp/src/main.nr index 4149beacece..ad5493c1444 100644 --- a/test_programs/compile_success_empty/comptime_exp/src/main.nr +++ b/test_programs/compile_success_empty/comptime_exp/src/main.nr @@ -59,6 +59,13 @@ 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_any_integer + let expr = quote { 1 }.as_expr().unwrap(); + assert_eq((1, false), expr.as_any_integer().unwrap()); + + let expr = quote { -2 }.as_expr().unwrap(); + assert_eq((2, true), expr.as_any_integer().unwrap()); } } From 01687e9101caa2b599e623af49120fbaa1faff37 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 16 Aug 2024 15:09:25 -0300 Subject: [PATCH 2/3] Add `Expr::as_member_access` --- .../src/hir/comptime/interpreter/builtin.rs | 17 +++++++++++++++++ noir_stdlib/src/meta/expr.nr | 3 +++ .../comptime_exp/src/main.nr | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 703f315677e..496f28a2f95 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -53,6 +53,7 @@ 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_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)), @@ -855,6 +856,22 @@ fn expr_as_index( }) } +// fn as_member_access(self) -> Option<(Expr, Quoted)> +fn expr_as_member_access( + arguments: Vec<(Value, Location)>, + return_type: Type, + location: Location, +) -> IResult { + 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)>, diff --git a/noir_stdlib/src/meta/expr.nr b/noir_stdlib/src/meta/expr.nr index 8e5ad47a4d8..9f3bba30247 100644 --- a/noir_stdlib/src/meta/expr.nr +++ b/noir_stdlib/src/meta/expr.nr @@ -21,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]> {} diff --git a/test_programs/compile_success_empty/comptime_exp/src/main.nr b/test_programs/compile_success_empty/comptime_exp/src/main.nr index ad5493c1444..b5ee0bed7fc 100644 --- a/test_programs/compile_success_empty/comptime_exp/src/main.nr +++ b/test_programs/compile_success_empty/comptime_exp/src/main.nr @@ -66,6 +66,11 @@ fn main() { let expr = quote { -2 }.as_expr().unwrap(); assert_eq((2, true), expr.as_any_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 }); } } From ebfe7a473e82f1bf820de0c624d491177dd5a95e Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 16 Aug 2024 15:24:58 -0300 Subject: [PATCH 3/3] as_any_integer -> as_integer --- .../src/hir/comptime/interpreter/builtin.rs | 32 +++++++++---------- noir_stdlib/src/meta/expr.nr | 4 +-- .../comptime_exp/src/main.nr | 6 ++-- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 496f28a2f95..cec4b1fd434 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -47,12 +47,12 @@ impl<'local, 'context> Interpreter<'local, 'context> { "array_as_str_unchecked" => array_as_str_unchecked(interner, arguments, location), "array_len" => array_len(interner, arguments, location), "as_slice" => as_slice(interner, arguments, location), - "expr_as_any_integer" => expr_as_any_integer(arguments, return_type, location), "expr_as_binary_op" => expr_as_binary_op(arguments, return_type, location), "expr_as_bool" => expr_as_bool(arguments, return_type, location), "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), @@ -758,21 +758,6 @@ fn zeroed(return_type: Type) -> IResult { } } -// fn as_any_integer(self) -> Option<(Field, bool)> -fn expr_as_any_integer( - arguments: Vec<(Value, Location)>, - return_type: Type, - location: Location, -) -> IResult { - 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_bool(self) -> Option fn expr_as_bool( arguments: Vec<(Value, Location)>, @@ -856,6 +841,21 @@ 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 { + 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)>, diff --git a/noir_stdlib/src/meta/expr.nr b/noir_stdlib/src/meta/expr.nr index 9f3bba30247..55c49531017 100644 --- a/noir_stdlib/src/meta/expr.nr +++ b/noir_stdlib/src/meta/expr.nr @@ -3,8 +3,8 @@ use crate::meta::op::UnaryOp; use crate::meta::op::BinaryOp; impl Expr { - #[builtin(expr_as_any_integer)] - fn as_any_integer(self) -> Option<(Field, bool)> {} + #[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)> {} diff --git a/test_programs/compile_success_empty/comptime_exp/src/main.nr b/test_programs/compile_success_empty/comptime_exp/src/main.nr index b5ee0bed7fc..7b25dcb5097 100644 --- a/test_programs/compile_success_empty/comptime_exp/src/main.nr +++ b/test_programs/compile_success_empty/comptime_exp/src/main.nr @@ -60,12 +60,12 @@ fn main() { assert(get_binary_op(quote { x << y }).is_shift_left()); assert(get_binary_op(quote { x % y }).is_modulo()); - // Check Expr::as_any_integer + // Check Expr::as_integer let expr = quote { 1 }.as_expr().unwrap(); - assert_eq((1, false), expr.as_any_integer().unwrap()); + assert_eq((1, false), expr.as_integer().unwrap()); let expr = quote { -2 }.as_expr().unwrap(); - assert_eq((2, true), expr.as_any_integer().unwrap()); + assert_eq((2, true), expr.as_integer().unwrap()); // Check Expr::as_member_access let expr = quote { foo.bar }.as_expr().unwrap();