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_bool #5729

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
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
}
} else {
let name = self.elaborator.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

Expand Down Expand Up @@ -897,6 +897,7 @@
(Value::U16(lhs), Value::U16(rhs)) => Ok(Value::Bool(lhs == rhs)),
(Value::U32(lhs), Value::U32(rhs)) => Ok(Value::Bool(lhs == rhs)),
(Value::U64(lhs), Value::U64(rhs)) => Ok(Value::Bool(lhs == rhs)),
(Value::Bool(lhs), Value::Bool(rhs)) => Ok(Value::Bool(lhs == rhs)),
(lhs, rhs) => make_error(self, lhs, rhs, "=="),
},
BinaryOpKind::NotEqual => match (lhs, rhs) {
Expand All @@ -909,6 +910,7 @@
(Value::U16(lhs), Value::U16(rhs)) => Ok(Value::Bool(lhs != rhs)),
(Value::U32(lhs), Value::U32(rhs)) => Ok(Value::Bool(lhs != rhs)),
(Value::U64(lhs), Value::U64(rhs)) => Ok(Value::Bool(lhs != rhs)),
(Value::Bool(lhs), Value::Bool(rhs)) => Ok(Value::Bool(lhs != rhs)),
(lhs, rhs) => make_error(self, lhs, rhs, "!="),
},
BinaryOpKind::Less => match (lhs, rhs) {
Expand Down
18 changes: 17 additions & 1 deletion compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use crate::{
ast::{
ExpressionKind, FunctionKind, FunctionReturnType, IntegerBitSize, UnresolvedType,
ExpressionKind, FunctionKind, FunctionReturnType, IntegerBitSize, Literal, UnresolvedType,
UnresolvedTypeData, Visibility,
},
hir::comptime::{errors::IResult, value::add_token_spans, InterpreterError, Value},
Expand Down Expand Up @@ -47,6 +47,7 @@
"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_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),
Expand Down Expand Up @@ -383,7 +384,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 387 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 387 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 @@ -753,6 +754,21 @@
}
}

// fn as_bool(self) -> Option<bool>
fn expr_as_bool(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
expr_as(arguments, return_type, location, |expr| {
if let ExpressionKind::Literal(Literal::Bool(bool)) = expr {
Some(Value::Bool(bool))
} else {
None
}
})
}

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

impl Expr {
#[builtin(expr_as_bool)]
fn as_bool(self) -> Option<bool> {}

#[builtin(expr_as_function_call)]
fn as_function_call(self) -> Option<(Expr, [Expr])> {}

Expand Down
7 changes: 7 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 @@ -27,5 +27,12 @@ fn main() {
// Check parenthesized expression is automatically unwrapped
let expr = quote { ((if 1 { 2 })) }.as_expr().unwrap();
assert(expr.as_if().is_some());

// Check Expr::as_bool
let expr = quote { false }.as_expr().unwrap();
assert(expr.as_bool().unwrap() == false);

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