Skip to content

Commit

Permalink
Refactor ForeignCalls println to print with bool
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 30, 2023
1 parent 0cad9aa commit 20aa1b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ impl<'interner> Monomorphizer<'interner> {

if let ast::Expression::Ident(ident) = original_func.as_ref() {
if let Definition::Oracle(name) = &ident.definition {
if name.as_str() == "println" {
if name.as_str() == "print" {
// Oracle calls are required to be wrapped in an unconstrained function
// Thus, the only argument to the `println` oracle is expected to always be an ident
self.append_printable_type_info(&hir_arguments[0], &mut arguments);
Expand Down
12 changes: 8 additions & 4 deletions noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ mod option;
mod string;
mod test;
// Oracle calls are required to be wrapped in an unconstrained function
// Thus, the only argument to the `println` oracle is expected to always be an ident
#[oracle(println)]
unconstrained fn println_oracle<T>(_input: T) {}
// Thus, the only argument to the `println` oracle is expected to always be an ident
#[oracle(print)]
unconstrained fn print_oracle<T>(_input: T, _with_newline: bool) {}

unconstrained pub fn print<T>(input: T) {
print_oracle(input, false);
}

unconstrained pub fn println<T>(input: T) {
println_oracle(input);
print_oracle(input, true);
}

#[foreign(recursive_aggregation)]
Expand Down
18 changes: 10 additions & 8 deletions tooling/nargo/src/ops/foreign_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait ForeignCallExecutor {
/// This enumeration represents the Brillig foreign calls that are natively supported by nargo.
/// After resolution of a foreign call, nargo will restart execution of the ACVM
pub(crate) enum ForeignCall {
Println,
Print,
CreateMock,
SetMockParams,
SetMockReturns,
Expand All @@ -31,7 +31,7 @@ impl std::fmt::Display for ForeignCall {
impl ForeignCall {
pub(crate) fn name(&self) -> &'static str {
match self {
ForeignCall::Println => "println",
ForeignCall::Print => "print",
ForeignCall::CreateMock => "create_mock",
ForeignCall::SetMockParams => "set_mock_params",
ForeignCall::SetMockReturns => "set_mock_returns",
Expand All @@ -42,7 +42,7 @@ impl ForeignCall {

pub(crate) fn lookup(op_name: &str) -> Option<ForeignCall> {
match op_name {
"println" => Some(ForeignCall::Println),
"print" => Some(ForeignCall::Print),
"create_mock" => Some(ForeignCall::CreateMock),
"set_mock_params" => Some(ForeignCall::SetMockParams),
"set_mock_returns" => Some(ForeignCall::SetMockReturns),
Expand Down Expand Up @@ -92,7 +92,7 @@ pub struct DefaultForeignCallExecutor {
last_mock_id: usize,
/// The registered mocks
mocked_responses: Vec<MockedCall>,
/// Whether to print [`ForeignCall::Println`] output.
/// Whether to print [`ForeignCall::Print`] output.
show_output: bool,
}

Expand Down Expand Up @@ -120,9 +120,11 @@ impl DefaultForeignCallExecutor {
decode_string_value(&fields)
}

fn execute_println(foreign_call_inputs: &[ForeignCallParam]) -> Result<(), ForeignCallError> {
fn execute_print(foreign_call_inputs: &[ForeignCallParam]) -> Result<(), ForeignCallError> {
let display_values: PrintableValueDisplay = foreign_call_inputs.try_into()?;
println!("{display_values}");
let skip_newline = foreign_call_inputs[1].unwrap_value().is_zero();
print!("{display_values}{}", if skip_newline { "" } else { "\n" });

Ok(())
}
}
Expand All @@ -134,9 +136,9 @@ impl ForeignCallExecutor for DefaultForeignCallExecutor {
) -> Result<ForeignCallResult, ForeignCallError> {
let foreign_call_name = foreign_call.function.as_str();
match ForeignCall::lookup(foreign_call_name) {
Some(ForeignCall::Println) => {
Some(ForeignCall::Print) => {
if self.show_output {
Self::execute_println(&foreign_call.inputs)?;
Self::execute_print(&foreign_call.inputs)?;
}
Ok(ForeignCallResult { values: vec![] })
}
Expand Down

0 comments on commit 20aa1b2

Please sign in to comment.