Skip to content

Commit 0af5b6c

Browse files
committed
Enable decimal printing for integers
1 parent 70ccb7e commit 0af5b6c

File tree

1 file changed

+27
-12
lines changed
  • compiler/noirc_printable_type/src

1 file changed

+27
-12
lines changed

compiler/noirc_printable_type/src/lib.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,24 @@ fn fetch_printable_type(
168168
fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
169169
let mut output = String::new();
170170
match (value, typ) {
171-
(
172-
PrintableValue::Field(f),
173-
PrintableType::Field
174-
// TODO(#2401): We should print the sign for these and probably print normal integers instead of field strings
175-
| PrintableType::SignedInteger { .. }
176-
| PrintableType::UnsignedInteger { .. },
177-
) => {
171+
(PrintableValue::Field(f), PrintableType::Field) => {
178172
output.push_str(&format_field_string(*f));
179173
}
174+
(PrintableValue::Field(f), PrintableType::UnsignedInteger { width }) => {
175+
let uint_cast = f.to_u128() & ((1 << width) - 1); // Retain the lower 'width' bits
176+
output.push_str(&uint_cast.to_string());
177+
}
178+
(PrintableValue::Field(f), PrintableType::SignedInteger { width }) => {
179+
let mut uint = f.to_u128(); // Interpret as uint
180+
181+
// Extract sign relative to width of input
182+
if (uint >> (width - 1)) == 1 {
183+
output.push('-');
184+
uint = (uint ^ ((1 << width) - 1)) + 1; // Two's complement relative to width of input
185+
}
186+
187+
output.push_str(&uint.to_string());
188+
}
180189
(PrintableValue::Field(f), PrintableType::Boolean) => {
181190
if f.is_one() {
182191
output.push_str("true");
@@ -187,8 +196,11 @@ fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
187196
(PrintableValue::Vec(vector), PrintableType::Array { typ, .. }) => {
188197
output.push('[');
189198
let mut values = vector.iter().peekable();
190-
while let Some(value) = values.next() {
191-
output.push_str(&format!("{}", PrintableValueDisplay::Plain(value.clone(), *typ.clone())));
199+
while let Some(value) = values.next() {
200+
output.push_str(&format!(
201+
"{}",
202+
PrintableValueDisplay::Plain(value.clone(), *typ.clone())
203+
));
192204
if values.peek().is_some() {
193205
output.push_str(", ");
194206
}
@@ -204,9 +216,12 @@ fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
204216
output.push_str(&format!("{name} {{ "));
205217

206218
let mut fields = fields.iter().peekable();
207-
while let Some((key, field_type)) = fields.next() {
219+
while let Some((key, field_type)) = fields.next() {
208220
let value = &map[key];
209-
output.push_str(&format!("{key}: {}", PrintableValueDisplay::Plain(value.clone(), field_type.clone())));
221+
output.push_str(&format!(
222+
"{key}: {}",
223+
PrintableValueDisplay::Plain(value.clone(), field_type.clone())
224+
));
210225
if fields.peek().is_some() {
211226
output.push_str(", ");
212227
}
@@ -215,7 +230,7 @@ fn to_string(value: &PrintableValue, typ: &PrintableType) -> Option<String> {
215230
output.push_str(" }");
216231
}
217232

218-
_ => return None
233+
_ => return None,
219234
};
220235

221236
Some(output)

0 commit comments

Comments
 (0)