Skip to content

Commit

Permalink
Extend ForeignCalls with print method
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 25, 2023
1 parent 70ccb7e commit 1add7ae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,8 @@ 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" {
let name_str = name.as_str();
if (name_str == "println") | (name_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: 11 additions & 1 deletion noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ mod compat;
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
// Thus, the only argument to the `println` oracle is expected to always be an ident
#[oracle(println)]
unconstrained fn println_oracle<T>(_input: T) {}

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

#[oracle(print)]
unconstrained fn print_oracle<T>(_input: T) {}

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

#[foreign(recursive_aggregation)]
pub fn verify_proof<N>(
_verification_key: [Field],
Expand All @@ -36,10 +44,12 @@ pub fn verify_proof<N>(
_key_hash: Field,
_input_aggregation_object: [Field; N]
) -> [Field; N] {}

// Asserts that the given value is known at compile-time.
// Useful for debugging for-loop bounds.
#[builtin(assert_constant)]
pub fn assert_constant<T>(_x: T) {}

// from_field and as_field are private since they are not valid for every type.
// `as` should be the default for users to cast between primitive types, and in the future
// traits can be used to work with generic types.
Expand Down
15 changes: 15 additions & 0 deletions tooling/nargo/src/ops/foreign_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub trait ForeignCallExecutor {
/// After resolution of a foreign call, nargo will restart execution of the ACVM
pub(crate) enum ForeignCall {
Println,
Print,
Sequence,
ReverseSequence,
CreateMock,
Expand All @@ -35,6 +36,7 @@ impl ForeignCall {
pub(crate) fn name(&self) -> &'static str {
match self {
ForeignCall::Println => "println",
ForeignCall::Print => "print",
ForeignCall::Sequence => "get_number_sequence",
ForeignCall::ReverseSequence => "get_reverse_number_sequence",
ForeignCall::CreateMock => "create_mock",
Expand All @@ -48,6 +50,7 @@ impl ForeignCall {
pub(crate) fn lookup(op_name: &str) -> Option<ForeignCall> {
match op_name {
"println" => Some(ForeignCall::Println),
"print" => Some(ForeignCall::Print),
"get_number_sequence" => Some(ForeignCall::Sequence),
"get_reverse_number_sequence" => Some(ForeignCall::ReverseSequence),
"create_mock" => Some(ForeignCall::CreateMock),
Expand Down Expand Up @@ -132,6 +135,12 @@ impl DefaultForeignCallExecutor {
println!("{display_values}");
Ok(())
}

fn execute_print(foreign_call_inputs: &[ForeignCallParam]) -> Result<(), ForeignCallError> {
let display_values: PrintableValueDisplay = foreign_call_inputs.try_into()?;
print!("{display_values}");
Ok(())
}
}

impl ForeignCallExecutor for DefaultForeignCallExecutor {
Expand All @@ -147,6 +156,12 @@ impl ForeignCallExecutor for DefaultForeignCallExecutor {
}
Ok(ForeignCallResult { values: vec![] })
}
Some(ForeignCall::Print) => {
if self.show_output {
Self::execute_print(&foreign_call.inputs)?;
}
Ok(ForeignCallResult { values: vec![] })
}
Some(ForeignCall::Sequence) => {
let sequence_length: u128 =
foreign_call.inputs[0].unwrap_value().to_field().to_u128();
Expand Down

0 comments on commit 1add7ae

Please sign in to comment.