use std::fmt::Display;
use crate::object::ClosureFunction;
use super::super::bytecode::errors::FrameError;
use super::super::bytecode::instruction::Instruction;
pub struct Frame {
cf: ClosureFunction,
bp: usize,
ic: usize,
}
impl Frame {
pub fn new(fun: ClosureFunction, base_ptr: usize) -> Self {
Self {
cf: fun,
bp: base_ptr,
ic: 0,
}
}
pub fn rext_instruction(&self) -> Result<Instruction, FrameError> {
Ok(self.cf.fun.instructions.read_instruction(self.ic)?)
}
pub fn set_ic(&mut self, tgt: usize) {
self.ic = tgt
}
pub fn add_ic(&mut self, offset: usize) {
self.ic += offset
}
pub fn ic(&self) -> usize {
#![allow(dead_code)]
self.ic
}
pub fn bp(&self) -> usize {
self.bp
}
pub fn is_runnable(&self) -> bool {
self.ic < self.cf.fun.instructions.length()
}
pub fn get_closure_ref(&self) -> &ClosureFunction {
&self.cf
}
}
impl Display for Frame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut buf = String::new();
buf += &format!("<FRAME> IC : {}, BP : {}\n", self.ic, self.bp);
buf += "\nINSTRUCTIONS\n";
buf += &self.cf.fun.instructions.to_string_with_highlight(self.ic);
f.write_str(buf.as_str())
}
}