use super::value::*;
#[derive(Debug, Clone)]
pub struct Memory<'a> {
pub registers: [Value; 16],
pub call_stack: Vec<&'a str>,
pub heap: Heap
}
impl<'a> Memory<'a> {
pub fn write_reg(&mut self, reg: usize, value: Value) {
self.registers[reg] = value
}
pub fn write_heap(&mut self, _addr: usize, value: Value) {
self.heap.malloc(value.sizeof(), value)
}
}
#[derive(Debug, Clone)]
pub struct Allocation {
pub size: usize,
pub in_use: bool
}
#[derive(Debug, Clone)]
pub struct Heap {
pub heap: Vec<u8>,
pub allocations: Vec<Allocation>
}
impl Heap {
pub fn malloc(&mut self, size: usize, item: Value) {
self.allocations.push(Allocation {
size,
in_use: true
});
self.heap.extend(item.to_bytes())
} }