sierra-vm 0.1.2

Library crate for the SierraVM
Documentation
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> {
    /// Write to a register
    /// 
    /// # Arguments
    /// * `reg` - Register to write to
    /// * `value` - The value to write
    /// 
    /// # Examples
    /// ```rust
    /// mem.write_reg(DST, 2)
    /// ```
    pub fn write_reg(&mut self, reg: usize, value: Value) {
        self.registers[reg] = value
    }

    /// Write to the heap
    /// 
    /// # Arguments
    /// * `addr` - The address to write to
    /// * `value` - The value to write
    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())
    } // THIS WILL BE CHANGED! (partly because idk how allocations should actually work)
}