forked from Orchid/orchid
New macro system and stdlib additions
This commit is contained in:
@@ -13,7 +13,6 @@ use crate::expr::Expr;
|
||||
pub struct ExprStoreData {
|
||||
exprs: RefCell<HashMap<api::ExprTicket, (u32, Expr)>>,
|
||||
parent: Option<ExprStore>,
|
||||
tracking_parent: bool,
|
||||
}
|
||||
#[derive(Clone, Default)]
|
||||
pub struct ExprStore(Rc<ExprStoreData>);
|
||||
@@ -25,16 +24,12 @@ impl ExprStore {
|
||||
/// but operations on the parent can access the child exprs too until this
|
||||
/// store is dropped.
|
||||
#[must_use]
|
||||
pub fn derive(&self, tracking_parent: bool) -> Self {
|
||||
Self(Rc::new(ExprStoreData {
|
||||
exprs: RefCell::default(),
|
||||
parent: Some(self.clone()),
|
||||
tracking_parent,
|
||||
}))
|
||||
pub fn derive(&self) -> Self {
|
||||
Self(Rc::new(ExprStoreData { exprs: RefCell::default(), parent: Some(self.clone()) }))
|
||||
}
|
||||
pub fn give_expr(&self, expr: Expr) {
|
||||
if self.0.tracking_parent {
|
||||
self.0.parent.as_ref().unwrap().give_expr(expr.clone());
|
||||
if let Some(parent) = self.0.parent.as_ref() {
|
||||
parent.give_expr(expr.clone())
|
||||
}
|
||||
match self.0.exprs.borrow_mut().entry(expr.id()) {
|
||||
Entry::Occupied(mut oe) => oe.get_mut().0 += 1,
|
||||
@@ -44,8 +39,8 @@ impl ExprStore {
|
||||
}
|
||||
}
|
||||
pub fn take_expr(&self, ticket: api::ExprTicket) -> Option<Expr> {
|
||||
if self.0.tracking_parent {
|
||||
self.0.parent.as_ref().unwrap().take_expr(ticket);
|
||||
if let Some(parent) = self.0.parent.as_ref() {
|
||||
parent.take_expr(ticket);
|
||||
}
|
||||
match self.0.exprs.borrow_mut().entry(ticket) {
|
||||
Entry::Vacant(_) => panic!("Attempted to double-take expression"),
|
||||
@@ -79,13 +74,11 @@ impl Drop for ExprStore {
|
||||
if 1 < Rc::strong_count(&self.0) {
|
||||
return;
|
||||
}
|
||||
if !self.0.tracking_parent {
|
||||
return;
|
||||
}
|
||||
let parent = self.0.parent.as_ref().unwrap();
|
||||
for (id, (count, _)) in self.0.exprs.borrow().iter() {
|
||||
for _ in 0..*count {
|
||||
parent.take_expr(*id);
|
||||
if let Some(parent) = self.0.parent.as_ref() {
|
||||
for (id, (count, _)) in self.0.exprs.borrow().iter() {
|
||||
for _ in 0..*count {
|
||||
parent.take_expr(*id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user