use core::cell::RefCell;
use crate::instruction::Label;
use alloc::rc::Rc;
#[derive(Debug, Clone)]
pub struct CompileContextMetadata {
is_outer_context: bool,
next_label: Rc<RefCell<Label>>,
}
impl CompileContextMetadata {
pub fn outer() -> Self {
CompileContextMetadata {
is_outer_context: true,
next_label: Rc::new(RefCell::new(Label(0))),
}
}
pub fn is_outer_context(&self) -> bool {
self.is_outer_context
}
pub fn inner(&self) -> Self {
CompileContextMetadata {
is_outer_context: false,
next_label: Rc::clone(&self.next_label),
}
}
pub fn next_label(&self) -> Label {
let label = self.next_label.borrow().0;
*self.next_label.borrow_mut() = Label(label + 1);
Label(label)
}
}