mirror of
https://github.com/guilhermewerner/machine
synced 2025-06-16 21:24:18 +00:00
Update heap, stack and opcodes
This commit is contained in:
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user