Skip to content

Commit

Permalink
feat: add Expr::as_bool (#5729)
Browse files Browse the repository at this point in the history
# Description

## Problem

Part of #5668

## Summary

Just one method because while doing it I got an error trying to compare
two bools at comptime, and I found that that case was missing.

## 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 15, 2024
1 parent f57a7b2 commit ca75cc2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
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 @@ -897,6 +897,7 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
(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 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
(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 rustc_hash::FxHashMap as HashMap;

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 @@ 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_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 @@ -753,6 +754,21 @@ fn zeroed(return_type: Type) -> IResult<Value> {
}
}

// 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);
}
}

0 comments on commit ca75cc2

Please sign in to comment.