ft(interpreter): impl push/pop

This commit is contained in:
2025-06-17 10:39:49 +09:00
parent d1ea96edd8
commit 5fab099cd8
5 changed files with 116 additions and 15 deletions

View File

@@ -1,16 +1,47 @@
use crate::operands::{Byte, Displacement, ImmediateOperand, MemoryIndex, Word};
use super::interpreter::InterpreterError;
#[derive(Debug, Clone, Copy)]
pub struct Memory {
memory: [Byte; 65536],
memory: [Byte; Word::MAX as usize],
}
impl Memory {
pub fn new() -> Self {
Self { memory: [0; 65536] }
Self {
memory: [0; Word::MAX as usize],
}
}
// Write an [`ImmediateOperand`] to a memory location indexed by a [`MemoryIndex`].
/// Safely writes a [`Word`] into an index of memory.
pub fn write_raw(&mut self, idx: Word, val: Word) -> Result<(), InterpreterError> {
if idx + 1 > Word::MAX {
return Err(InterpreterError::MemoryOutOfBound(idx));
} else {
let [low, high] = val.to_le_bytes();
self.memory[idx as usize] = low;
self.memory[(idx + 1) as usize] = high;
Ok(())
}
}
/// Safely reads a [`Word`] from an index of memory
pub fn read_raw(&self, idx: Word) -> Result<Word, InterpreterError> {
let b1 = self
.memory
.get(idx as usize)
.ok_or(InterpreterError::MemoryOutOfBound(idx))?
.to_owned();
let b2 = self
.memory
.get((idx + 1) as usize)
.ok_or(InterpreterError::MemoryOutOfBound(idx))?
.to_owned();
Ok(Word::from_be_bytes([b1, b2]))
}
/// Write an [`ImmediateOperand`] to a memory location indexed by a [`MemoryIndex`].
pub fn write(
&mut self,
regs: &crate::interpreter::register::Register,