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

Rename memory fields #36

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions execution-plan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ mod value;
/// KCEP's program memory. A flat, linear list of values.
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct Memory(Vec<Option<Primitive>>);
pub struct Memory {
addresses: Vec<Option<Primitive>>,
}

impl Default for Memory {
fn default() -> Self {
Self(vec![None; 1024])
Self {
addresses: vec![None; 1024],
}
}
}

Expand All @@ -53,16 +57,16 @@ impl From<usize> for Address {
impl Memory {
/// Get a value from KCEP's program memory.
pub fn get(&self, Address(addr): &Address) -> Option<&Primitive> {
self.0[*addr].as_ref()
self.addresses[*addr].as_ref()
}

/// Store a value in KCEP's program memory.
pub fn set(&mut self, Address(addr): Address, value: Primitive) {
// If isn't big enough for this value, double the size of memory until it is.
while addr > self.0.len() {
self.0.extend(vec![None; self.0.len()]);
while addr > self.addresses.len() {
self.addresses.extend(vec![None; self.addresses.len()]);
}
self.0[addr] = Some(value);
self.addresses[addr] = Some(value);
}

/// Store a value value (i.e. a value which takes up multiple addresses in memory).
Expand All @@ -72,7 +76,7 @@ impl Memory {
let parts = composite_value.into_parts().into_iter();
let mut total_addrs = 0;
for (value, addr) in parts.zip(start.0..) {
self.0[addr] = Some(value);
self.addresses[addr] = Some(value);
total_addrs += 1;
}
total_addrs
Expand All @@ -81,7 +85,7 @@ impl Memory {
/// Get a value value (i.e. a value which takes up multiple addresses in memory).
/// Its parts are stored in consecutive memory addresses starting at `start`.
pub fn get_composite<T: Value>(&self, start: Address) -> Result<T> {
let mut values = self.0.iter().skip(start.0).cloned();
let mut values = self.addresses.iter().skip(start.0).cloned();
T::from_parts(&mut values)
}
}
Expand Down
42 changes: 21 additions & 21 deletions execution-plan/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ async fn add_to_composite_value() {
};
let start_addr = Address(0);
mem.set_composite(start_addr, point_before);
assert_eq!(mem.0[0], Some(2.0.into()));
assert_eq!(mem.0[1], Some(3.0.into()));
assert_eq!(mem.0[2], Some(4.0.into()));
assert_eq!(mem.get(&Address(0)), Some(&(2.0.into())));
assert_eq!(mem.get(&Address(1)), Some(&(3.0.into())));
assert_eq!(mem.get(&Address(2)), Some(&(4.0.into())));

let client = test_client().await;
// Update the point's x-value in memory.
Expand Down Expand Up @@ -267,7 +267,7 @@ async fn api_call_draw_cube() {
// The image output was set to addr 99.
// Outputs are two addresses long, addr 99 will store the data format (TAKE_SNAPSHOT)
// and addr 100 will store its first field ('contents', the image bytes).
let Primitive::Bytes(b) = mem.0[100].as_ref().unwrap() else {
let Primitive::Bytes(b) = mem.get(&Address(100)).as_ref().unwrap() else {
panic!("wrong format in memory addr 100");
};
// Visually check that the image is a cube.
Expand Down Expand Up @@ -302,23 +302,23 @@ fn debug_dump_memory(mem: &Memory) -> String {
val_type: &'static str,
value: String,
}
let table_data: Vec<_> = mem
.0
.iter()
.enumerate()
.filter_map(|(i, val)| {
if let Some(val) = val {
let (val_type, value) = val.pretty_print();
Some(MemoryAddr {
index: i,
val_type,
value,
})
} else {
None
}
})
.collect();
let table_data: Vec<_> =
mem.addresses
.iter()
.enumerate()
.filter_map(|(i, val)| {
if let Some(val) = val {
let (val_type, value) = val.pretty_print();
Some(MemoryAddr {
index: i,
val_type,
value,
})
} else {
None
}
})
.collect();
Table::new(table_data).with(Style::sharp()).to_string()
}

Expand Down
Loading