Skip to content

Commit

Permalink
Enable decimal printing for integers
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 29, 2023
1 parent 2b0fce2 commit 985f23b
Showing 1 changed file with 27 additions and 12 deletions.
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 {
output.push('-');
uint = (uint ^ ((1 << width) - 1)) + 1; // Two's complement relative to width of input
}

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

0 comments on commit 985f23b

Please sign in to comment.