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

Add Display trait to Memory struct #812

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Changes from 4 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
46 changes: 46 additions & 0 deletions src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use felt::Felt;
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
fmt::{Display, Formatter},
};

pub struct ValidationRule(
Expand Down Expand Up @@ -300,6 +301,29 @@ impl Memory {
}
}

impl Display for Memory {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
writeln!(f, "temp_memory {{")?;
Copy link
Member

Choose a reason for hiding this comment

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

We could avoid making distinctions between real and temporary memory and just pritnt them out as a single memory. Temporary segments can be identified by their negative index

for (i, segment) in self.temp_data.iter().enumerate() {
for (j, cell) in segment.iter().enumerate() {
if let Some(cell) = cell {
writeln!(f, " ({i},{j}) : {cell}")?;
Copy link
Member

Choose a reason for hiding this comment

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

Remember that temporary memory segment indexes start from -1, but we store them from 0 (cuz vec). So when printing out the temporary addresses we should be printing -(i + 1) instead of i

}
}
}
writeln!(f, "}}")?;
writeln!(f, "real_memory {{")?;
for (i, segment) in self.data.iter().enumerate() {
for (j, cell) in segment.iter().enumerate() {
if let Some(cell) = cell {
writeln!(f, " ({i},{j}) : {cell}")?;
}
}
}
writeln!(f, "}}")
Copy link
Member

Choose a reason for hiding this comment

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

These symbols make it look like a Debug rather than a Display, we could avoid them

}
}

pub(crate) trait RelocateValue<'a, Input: 'a, Output: 'a> {
fn relocate_value(&self, value: Input) -> Output;
}
Expand Down Expand Up @@ -1225,6 +1249,28 @@ mod memory_tests {
assert!(memory.temp_data.is_empty());
}

#[test]
fn test_memory_display() {
let mut memory = memory![
((0, 0), 1),
((0, 1), (-1, 0)),
((0, 2), 3),
((1, 0), (-1, 1)),
((1, 1), 5),
((1, 2), (-1, 2))
];

memory.temp_data = vec![vec![
mayberelocatable!(-1, 0).into(),
mayberelocatable!(8).into(),
mayberelocatable!(9).into(),
]];

assert_eq!(
format!("{}", memory),
"temp_memory {\n (0,0) : -1:0\n (0,1) : 8\n (0,2) : 9\n}\nreal_memory {\n (0,0) : 1\n (0,1) : -1:0\n (0,2) : 3\n (1,0) : -1:1\n (1,1) : 5\n (1,2) : -1:2\n}\n");
}

#[test]
fn relocate_memory_into_existing_segment_temporary_values_in_temporary_memory() {
let mut memory = memory![
Expand Down