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: Format strings for prints #1952

Merged
merged 67 commits into from
Aug 1, 2023
Merged

feat: Format strings for prints #1952

merged 67 commits into from
Aug 1, 2023

Conversation

vezenovm
Copy link
Contributor

@vezenovm vezenovm commented Jul 17, 2023

Description

Enables printing multiple different types in the same statement and string as well as a new fmtstr type. A fmtstr can be specified in the same way as a normal string it just should be prepended with an f character:

    let fmt_str = f"i: {i}, j: {j}";
    std::println(fmt_str);

    let s = myStruct { y: x, x: y };
    std::println(s);

    std::println(f"i: {i}, s: {s}");

    std::println(x);
    std::println([x, y]);
    
    let foo = fooStruct { my_struct: s, foo: 15 };
    std::println(f"s: {s}, foo: {foo}");

Problem*

Resolves this comment: #1917 (comment)

Summary*

A new format string type was introduced in the frontend and all the relevant passes. Auser can invoke string interpolation by prepending a string literal with the f character. Comparisons between format strings is currently not enabled as they are only really used for printing. This can be easily changed though.

This code:

let string = "i: {i}, j: {j}";
std::println(string);

// A `fmtstr` lets you easily perform string interpolation.
let fmt_str: fmtstr<14, (Field, Field)> = f"i: {i}, j: {j}";
let fmt_str = string_identity(fmt_str);
std::println(fmt_str);

let fmt_str_no_type = f"i: {i}, j: {j}";
std::println(fmt_str_no_type);

let fmt_str_generic = string_with_generic(fmt_str_no_type);
std::println(fmt_str_generic);

fn string_identity(string: str<14>) -> str<14> {
    string
}

fn string_with_generic<T>(string: T) -> T {
    string
}

will output this:

"i: {i}, j: {j}"
i: "0x05", j: "0x0a"
i: "0x05", j: "0x0a"
i: "0x05", j: "0x0a"

The debug_logs test under nargo_cli/tests/test_data_ssa_refactor will give this output:

"i: {i}, j: {j}"
i: "0x05", j: "0x0a"
i: "0x05", j: "0x0a"
i: "0x05", j: "0x0a"
{"x":"0x0a","y":"0x05"}
randomstring"0x05""0x05"
i: "0x05", s: {"x":"0x0a","y":"0x05"}
"0x05"
["0x05","0x0a"]
s: {"x":"0x0a","y":"0x05"}, foo: {"foo":"0x0a","my_struct":{"x":"0x05","y":"0x0a"}}
x: 0, y: 1
s1: {"x":"0x0a","y":"0x05"}, s2: {"x":"0x1e","y":"0x0a"}
foo1: {"foo":"0x0f","my_struct":{"x":"0x0a","y":"0x05"}}, foo2: {"foo":"0x1e","my_struct":{"x":"0x0f","y":"0x0a"}}

Ideally the same string interpolation strategy can be used for a format function that returns a string slice once we can handle slices returned from unconstrained functions inside of ACIR.

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@vezenovm vezenovm requested a review from jfecher August 1, 2023 13:26
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still some final changes before we merge

crates/noirc_frontend/src/parser/parser.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/parser/parser.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/monomorphization/printer.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/monomorphization/mod.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/monomorphization/mod.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/monomorphization/mod.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/ast/mod.rs Outdated Show resolved Hide resolved
crates/noirc_abi/src/input_parser/json.rs Outdated Show resolved Hide resolved
@vezenovm vezenovm requested a review from jfecher August 1, 2023 18:41
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jfecher jfecher added this pull request to the merge queue Aug 1, 2023
Merged via the queue into master with commit 3c82721 Aug 1, 2023
4 checks passed
@jfecher jfecher deleted the mv/format-string branch August 1, 2023 19:14
TomAFrench added a commit that referenced this pull request Aug 2, 2023
* master:
  feat!: Support workspaces and package selection on every nargo command (#1992)
  chore: Make a more clear error for slices passed to std::println (#2113)
  feat: Implement type aliases (#2112)
  feat: Add `Option<T>` to noir stdlib (#1781)
  feat: Format strings for prints  (#1952)
  feat(acir_gen): RecursiveAggregation opcode and updates to black box func call generation (#2097)
  fix: Mutating a variable no longer mutates its copy (#2057)
  fix: Implement `.len()` in Acir-Gen (#2077)
  chore: clippy fixes (#2101)
TomAFrench added a commit that referenced this pull request Aug 2, 2023
* master: (50 commits)
  fix(globals): Accurately filter literals for resolving globals (#2126)
  feat: Optimize away constant calls to black box functions (#1981)
  fix: Rename `Option::value` to `Option::_value` (#2127)
  feat: replace boolean `AND`s with multiplication (#1954)
  chore: create a `const` to hold the panic message (#2122)
  feat: Add support for bitshifts by distances known at runtime (#2072)
  feat: Add additional `BinaryOp` simplifications (#2124)
  fix: flattening pass no longer overwrites previously mapped condition values (#2117)
  chore(noirc_driver): Unify crate preparation (#2119)
  feat!: Support workspaces and package selection on every nargo command (#1992)
  chore: Make a more clear error for slices passed to std::println (#2113)
  feat: Implement type aliases (#2112)
  feat: Add `Option<T>` to noir stdlib (#1781)
  feat: Format strings for prints  (#1952)
  feat(acir_gen): RecursiveAggregation opcode and updates to black box func call generation (#2097)
  fix: Mutating a variable no longer mutates its copy (#2057)
  fix: Implement `.len()` in Acir-Gen (#2077)
  chore: clippy fixes (#2101)
  chore: Update `noir-source-resolver` to v1.1.3 (#1912)
  chore: Document `GeneratedAcir::more_than_eq_comparison` (#2085)
  ...
TomAFrench added a commit that referenced this pull request Aug 2, 2023
* master:
  chore: rename `ssa_refactor` module to `ssa` (#2129)
  chore: Use `--show-output` flag on execution rather than compilation  (#2116)
  fix(globals): Accurately filter literals for resolving globals (#2126)
  feat: Optimize away constant calls to black box functions (#1981)
  fix: Rename `Option::value` to `Option::_value` (#2127)
  feat: replace boolean `AND`s with multiplication (#1954)
  chore: create a `const` to hold the panic message (#2122)
  feat: Add support for bitshifts by distances known at runtime (#2072)
  feat: Add additional `BinaryOp` simplifications (#2124)
  fix: flattening pass no longer overwrites previously mapped condition values (#2117)
  chore(noirc_driver): Unify crate preparation (#2119)
  feat!: Support workspaces and package selection on every nargo command (#1992)
  chore: Make a more clear error for slices passed to std::println (#2113)
  feat: Implement type aliases (#2112)
  feat: Add `Option<T>` to noir stdlib (#1781)
  feat: Format strings for prints  (#1952)
  feat(acir_gen): RecursiveAggregation opcode and updates to black box func call generation (#2097)
  fix: Mutating a variable no longer mutates its copy (#2057)
  fix: Implement `.len()` in Acir-Gen (#2077)
TomAFrench added a commit that referenced this pull request Aug 2, 2023
* master:
  chore: rename `ssa_refactor` module to `ssa` (#2129)
  chore: Use `--show-output` flag on execution rather than compilation  (#2116)
  fix(globals): Accurately filter literals for resolving globals (#2126)
  feat: Optimize away constant calls to black box functions (#1981)
  fix: Rename `Option::value` to `Option::_value` (#2127)
  feat: replace boolean `AND`s with multiplication (#1954)
  chore: create a `const` to hold the panic message (#2122)
  feat: Add support for bitshifts by distances known at runtime (#2072)
  feat: Add additional `BinaryOp` simplifications (#2124)
  fix: flattening pass no longer overwrites previously mapped condition values (#2117)
  chore(noirc_driver): Unify crate preparation (#2119)
  feat!: Support workspaces and package selection on every nargo command (#1992)
  chore: Make a more clear error for slices passed to std::println (#2113)
  feat: Implement type aliases (#2112)
  feat: Add `Option<T>` to noir stdlib (#1781)
  feat: Format strings for prints  (#1952)
  feat(acir_gen): RecursiveAggregation opcode and updates to black box func call generation (#2097)
  fix: Mutating a variable no longer mutates its copy (#2057)
  fix: Implement `.len()` in Acir-Gen (#2077)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants