Update heap, stack and opcodes

This commit is contained in:
Werner
2021-11-29 20:43:59 -03:00
parent 03e43f39ce
commit 38236f0ad5
10 changed files with 396 additions and 180 deletions

View File

@ -1,50 +1,33 @@
use crate::Types::*;
use std::mem;
use crate::Frame;
pub struct Stack {
storage: Vec<u8>,
capacity: usize,
frames: Vec<Frame>,
lenght: usize,
}
impl Stack {
pub fn New(size: usize) -> Self {
Self {
storage: Vec::with_capacity(size),
capacity: size,
frames: Vec::with_capacity(size),
lenght: size,
}
}
pub fn IsEmpty(&self) -> bool {
self.frames.is_empty()
}
pub fn Flush(&mut self) {
self.storage.clear();
self.frames.clear();
}
pub fn PushByte(&mut self, item: Byte) {
if self.storage.len() + 1 < self.capacity {
self.storage.push(item);
pub fn Push(&mut self, frame: Frame) {
if self.frames.len() + frame.GetSize() < self.lenght {
self.frames.push(frame);
}
}
pub fn PopByte(&mut self) -> Byte {
self.storage.pop().unwrap_or(0)
}
pub fn PushWord(&mut self, item: Word) {
const BYTES: usize = mem::size_of::<Word>();
if self.storage.len() + BYTES < self.capacity {
self.storage.extend(item);
}
}
pub fn PopWord(&mut self) -> Word {
const BYTES: usize = mem::size_of::<Word>();
let mut buffer = [0; BYTES];
for i in 0..BYTES {
buffer[i] = self.storage.pop().unwrap_or(0);
}
buffer
pub fn Pop(&mut self) -> Frame {
self.frames.pop().unwrap_or(Frame::Null())
}
}