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: Implement integer printing #3577

Merged
Merged
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
39 changes: 27 additions & 12 deletions compiler/noirc_printable_type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,24 @@ fn fetch_printable_type(
fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
let mut output = String::new();
match (value, typ) {
(
PrintableValue::Field(f),
PrintableType::Field
// TODO(#2401): We should print the sign for these and probably print normal integers instead of field strings
| PrintableType::SignedInteger { .. }
| PrintableType::UnsignedInteger { .. },
) => {
(PrintableValue::Field(f), PrintableType::Field) => {
output.push_str(&format_field_string(*f));
}
(PrintableValue::Field(f), PrintableType::UnsignedInteger { width }) => {
let uint_cast = f.to_u128() & ((1 << width) - 1); // Retain the lower 'width' bits
output.push_str(&uint_cast.to_string());
}
(PrintableValue::Field(f), PrintableType::SignedInteger { width }) => {
let mut uint = f.to_u128(); // Interpret as uint

// Extract sign relative to width of input
if (uint >> (width - 1)) == 1 {
jfecher marked this conversation as resolved.
Show resolved Hide resolved
output.push('-');
uint = (uint ^ ((1 << width) - 1)) + 1; // Two's complement relative to width of input
jfecher marked this conversation as resolved.
Show resolved Hide resolved
}

output.push_str(&uint.to_string());
}
(PrintableValue::Field(f), PrintableType::Boolean) => {
if f.is_one() {
output.push_str("true");
Expand All @@ -187,8 +196,11 @@ fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
(PrintableValue::Vec(vector), PrintableType::Array { typ, .. }) => {
output.push('[');
let mut values = vector.iter().peekable();
while let Some(value) = values.next() {
output.push_str(&format!("{}", PrintableValueDisplay::Plain(value.clone(), *typ.clone())));
while let Some(value) = values.next() {
output.push_str(&format!(
"{}",
PrintableValueDisplay::Plain(value.clone(), *typ.clone())
));
if values.peek().is_some() {
output.push_str(", ");
}
Expand All @@ -204,9 +216,12 @@ fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
output.push_str(&format!("{name} {{ "));

let mut fields = fields.iter().peekable();
while let Some((key, field_type)) = fields.next() {
while let Some((key, field_type)) = fields.next() {
let value = &map[key];
output.push_str(&format!("{key}: {}", PrintableValueDisplay::Plain(value.clone(), field_type.clone())));
output.push_str(&format!(
"{key}: {}",
PrintableValueDisplay::Plain(value.clone(), field_type.clone())
));
if fields.peek().is_some() {
output.push_str(", ");
}
Expand All @@ -215,7 +230,7 @@ fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
output.push_str(" }");
}

_ => return None
_ => return None,
};

Some(output)
Expand Down
Loading