Skip to content

Commit

Permalink
Forgot the expression part.
Browse files Browse the repository at this point in the history
  • Loading branch information
uberFoo committed Apr 23, 2024
1 parent b6ff1ec commit cc2bd6b
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 14 deletions.
96 changes: 89 additions & 7 deletions src/v2/lu_dog/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
//! * [`FunctionCall`]
//! * [`XFuture`]
//! * [`Grouped`]
//! * [`HaltAndCatchFire`]
//! * [`XIf`]
//! * [`ImplementationBlock`]
//! * [`Import`]
Expand Down Expand Up @@ -106,13 +107,14 @@ use crate::v2::lu_dog::types::{
Comparison, DataStructure, DwarfSourceFile, EnumField, EnumGeneric, Enumeration, Expression,
ExpressionBit, ExpressionStatement, ExternalImplementation, Field, FieldAccess,
FieldAccessTarget, FieldExpression, FloatLiteral, ForLoop, FormatBit, FormatString,
FuncGeneric, Function, FunctionCall, Grouped, ImplementationBlock, Import, Index,
IntegerLiteral, Item, Lambda, LambdaParameter, LetStatement, List, ListElement, ListExpression,
Literal, LocalVariable, MethodCall, NamedFieldExpression, ObjectWrapper, Operator, Parameter,
PathElement, Pattern, RangeExpression, ResultStatement, Span, Statement, StaticMethodCall,
StringBit, StringLiteral, StructExpression, StructField, StructGeneric, TupleField, TypeCast,
Unary, Unit, UnnamedFieldExpression, ValueType, Variable, VariableExpression, WoogStruct,
XFuture, XIf, XMacro, XMatch, XPath, XPlugin, XPrint, XReturn, XValue, ZObjectStore,
FuncGeneric, Function, FunctionCall, Grouped, HaltAndCatchFire, ImplementationBlock, Import,
Index, IntegerLiteral, Item, Lambda, LambdaParameter, LetStatement, List, ListElement,
ListExpression, Literal, LocalVariable, MethodCall, NamedFieldExpression, ObjectWrapper,
Operator, Parameter, PathElement, Pattern, RangeExpression, ResultStatement, Span, Statement,
StaticMethodCall, StringBit, StringLiteral, StructExpression, StructField, StructGeneric,
TupleField, TypeCast, Unary, Unit, UnnamedFieldExpression, ValueType, Variable,
VariableExpression, WoogStruct, XFuture, XIf, XMacro, XMatch, XPath, XPlugin, XPrint, XReturn,
XValue, ZObjectStore,
};

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -152,6 +154,7 @@ pub struct ObjectStore {
function_call: Rc<RefCell<HashMap<Uuid, Rc<RefCell<FunctionCall>>>>>,
x_future: Rc<RefCell<HashMap<Uuid, Rc<RefCell<XFuture>>>>>,
grouped: Rc<RefCell<HashMap<Uuid, Rc<RefCell<Grouped>>>>>,
halt_and_catch_fire: Rc<RefCell<HashMap<Uuid, Rc<RefCell<HaltAndCatchFire>>>>>,
x_if: Rc<RefCell<HashMap<Uuid, Rc<RefCell<XIf>>>>>,
implementation_block: Rc<RefCell<HashMap<Uuid, Rc<RefCell<ImplementationBlock>>>>>,
import: Rc<RefCell<HashMap<Uuid, Rc<RefCell<Import>>>>>,
Expand Down Expand Up @@ -243,6 +246,7 @@ impl ObjectStore {
function_call: Rc::new(RefCell::new(HashMap::default())),
x_future: Rc::new(RefCell::new(HashMap::default())),
grouped: Rc::new(RefCell::new(HashMap::default())),
halt_and_catch_fire: Rc::new(RefCell::new(HashMap::default())),
x_if: Rc::new(RefCell::new(HashMap::default())),
implementation_block: Rc::new(RefCell::new(HashMap::default())),
import: Rc::new(RefCell::new(HashMap::default())),
Expand Down Expand Up @@ -1604,6 +1608,54 @@ impl ObjectStore {
(0..len).map(move |i| values[i].clone())
}

/// Inter (insert) [`HaltAndCatchFire`] into the store.
///
pub fn inter_halt_and_catch_fire(
&mut self,
halt_and_catch_fire: Rc<RefCell<HaltAndCatchFire>>,
) {
let read = halt_and_catch_fire.borrow();
self.halt_and_catch_fire
.borrow_mut()
.insert(read.id, halt_and_catch_fire.clone());
}

/// Exhume (get) [`HaltAndCatchFire`] from the store.
///
pub fn exhume_halt_and_catch_fire(&self, id: &Uuid) -> Option<Rc<RefCell<HaltAndCatchFire>>> {
self.halt_and_catch_fire
.borrow()
.get(id)
.map(|halt_and_catch_fire| halt_and_catch_fire.clone())
}

/// Exorcise (remove) [`HaltAndCatchFire`] from the store.
///
pub fn exorcise_halt_and_catch_fire(
&mut self,
id: &Uuid,
) -> Option<Rc<RefCell<HaltAndCatchFire>>> {
self.halt_and_catch_fire
.borrow_mut()
.remove(id)
.map(|halt_and_catch_fire| halt_and_catch_fire.clone())
}

/// Get an iterator over the internal `HashMap<&Uuid, HaltAndCatchFire>`.
///
pub fn iter_halt_and_catch_fire(
&self,
) -> impl Iterator<Item = Rc<RefCell<HaltAndCatchFire>>> + '_ {
let values: Vec<Rc<RefCell<HaltAndCatchFire>>> = self
.halt_and_catch_fire
.borrow()
.values()
.map(|halt_and_catch_fire| halt_and_catch_fire.clone())
.collect();
let len = values.len();
(0..len).map(move |i| values[i].clone())
}

/// Inter (insert) [`XIf`] into the store.
///
pub fn inter_x_if(&mut self, x_if: Rc<RefCell<XIf>>) {
Expand Down Expand Up @@ -3918,6 +3970,18 @@ impl ObjectStore {
}
}

// Persist Halt and Catch Fire.
{
let path = path.join("halt_and_catch_fire");
fs::create_dir_all(&path)?;
for halt_and_catch_fire in self.halt_and_catch_fire.borrow().values() {
let path = path.join(format!("{}.json", halt_and_catch_fire.borrow().id));
let file = fs::File::create(path)?;
let mut writer = io::BufWriter::new(file);
serde_json::to_writer_pretty(&mut writer, &halt_and_catch_fire)?;
}
}

// Persist If.
{
let path = path.join("x_if");
Expand Down Expand Up @@ -5083,6 +5147,24 @@ impl ObjectStore {
}
}

// Load Halt and Catch Fire.
{
let path = path.join("halt_and_catch_fire");
let entries = fs::read_dir(path)?;
for entry in entries {
let entry = entry?;
let path = entry.path();
let file = fs::File::open(path)?;
let reader = io::BufReader::new(file);
let halt_and_catch_fire: Rc<RefCell<HaltAndCatchFire>> =
serde_json::from_reader(reader)?;
store
.halt_and_catch_fire
.borrow_mut()
.insert(halt_and_catch_fire.borrow().id, halt_and_catch_fire.clone());
}
}

// Load If.
{
let path = path.join("x_if");
Expand Down
100 changes: 93 additions & 7 deletions src/v2/lu_dog_rwlock/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
//! * [`FunctionCall`]
//! * [`XFuture`]
//! * [`Grouped`]
//! * [`HaltAndCatchFire`]
//! * [`XIf`]
//! * [`ImplementationBlock`]
//! * [`Import`]
Expand Down Expand Up @@ -106,13 +107,14 @@ use crate::v2::lu_dog_rwlock::types::{
Comparison, DataStructure, DwarfSourceFile, EnumField, EnumGeneric, Enumeration, Expression,
ExpressionBit, ExpressionStatement, ExternalImplementation, Field, FieldAccess,
FieldAccessTarget, FieldExpression, FloatLiteral, ForLoop, FormatBit, FormatString,
FuncGeneric, Function, FunctionCall, Grouped, ImplementationBlock, Import, Index,
IntegerLiteral, Item, Lambda, LambdaParameter, LetStatement, List, ListElement, ListExpression,
Literal, LocalVariable, MethodCall, NamedFieldExpression, ObjectWrapper, Operator, Parameter,
PathElement, Pattern, RangeExpression, ResultStatement, Span, Statement, StaticMethodCall,
StringBit, StringLiteral, StructExpression, StructField, StructGeneric, TupleField, TypeCast,
Unary, Unit, UnnamedFieldExpression, ValueType, Variable, VariableExpression, WoogStruct,
XFuture, XIf, XMacro, XMatch, XPath, XPlugin, XPrint, XReturn, XValue, ZObjectStore,
FuncGeneric, Function, FunctionCall, Grouped, HaltAndCatchFire, ImplementationBlock, Import,
Index, IntegerLiteral, Item, Lambda, LambdaParameter, LetStatement, List, ListElement,
ListExpression, Literal, LocalVariable, MethodCall, NamedFieldExpression, ObjectWrapper,
Operator, Parameter, PathElement, Pattern, RangeExpression, ResultStatement, Span, Statement,
StaticMethodCall, StringBit, StringLiteral, StructExpression, StructField, StructGeneric,
TupleField, TypeCast, Unary, Unit, UnnamedFieldExpression, ValueType, Variable,
VariableExpression, WoogStruct, XFuture, XIf, XMacro, XMatch, XPath, XPlugin, XPrint, XReturn,
XValue, ZObjectStore,
};

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -152,6 +154,7 @@ pub struct ObjectStore {
function_call: Arc<RwLock<HashMap<Uuid, Arc<RwLock<FunctionCall>>>>>,
x_future: Arc<RwLock<HashMap<Uuid, Arc<RwLock<XFuture>>>>>,
grouped: Arc<RwLock<HashMap<Uuid, Arc<RwLock<Grouped>>>>>,
halt_and_catch_fire: Arc<RwLock<HashMap<Uuid, Arc<RwLock<HaltAndCatchFire>>>>>,
x_if: Arc<RwLock<HashMap<Uuid, Arc<RwLock<XIf>>>>>,
implementation_block: Arc<RwLock<HashMap<Uuid, Arc<RwLock<ImplementationBlock>>>>>,
import: Arc<RwLock<HashMap<Uuid, Arc<RwLock<Import>>>>>,
Expand Down Expand Up @@ -243,6 +246,7 @@ impl ObjectStore {
function_call: Arc::new(RwLock::new(HashMap::default())),
x_future: Arc::new(RwLock::new(HashMap::default())),
grouped: Arc::new(RwLock::new(HashMap::default())),
halt_and_catch_fire: Arc::new(RwLock::new(HashMap::default())),
x_if: Arc::new(RwLock::new(HashMap::default())),
implementation_block: Arc::new(RwLock::new(HashMap::default())),
import: Arc::new(RwLock::new(HashMap::default())),
Expand Down Expand Up @@ -1764,6 +1768,58 @@ impl ObjectStore {
(0..len).map(move |i| values[i].clone())
}

/// Inter (insert) [`HaltAndCatchFire`] into the store.
///
pub fn inter_halt_and_catch_fire(
&mut self,
halt_and_catch_fire: Arc<RwLock<HaltAndCatchFire>>,
) {
let read = halt_and_catch_fire.read().unwrap();
self.halt_and_catch_fire
.write()
.unwrap()
.insert(read.id, halt_and_catch_fire.clone());
}

/// Exhume (get) [`HaltAndCatchFire`] from the store.
///
pub fn exhume_halt_and_catch_fire(&self, id: &Uuid) -> Option<Arc<RwLock<HaltAndCatchFire>>> {
self.halt_and_catch_fire
.read()
.unwrap()
.get(id)
.map(|halt_and_catch_fire| halt_and_catch_fire.clone())
}

/// Exorcise (remove) [`HaltAndCatchFire`] from the store.
///
pub fn exorcise_halt_and_catch_fire(
&mut self,
id: &Uuid,
) -> Option<Arc<RwLock<HaltAndCatchFire>>> {
self.halt_and_catch_fire
.write()
.unwrap()
.remove(id)
.map(|halt_and_catch_fire| halt_and_catch_fire.clone())
}

/// Get an iterator over the internal `HashMap<&Uuid, HaltAndCatchFire>`.
///
pub fn iter_halt_and_catch_fire(
&self,
) -> impl Iterator<Item = Arc<RwLock<HaltAndCatchFire>>> + '_ {
let values: Vec<Arc<RwLock<HaltAndCatchFire>>> = self
.halt_and_catch_fire
.read()
.unwrap()
.values()
.map(|halt_and_catch_fire| halt_and_catch_fire.clone())
.collect();
let len = values.len();
(0..len).map(move |i| values[i].clone())
}

/// Inter (insert) [`XIf`] into the store.
///
pub fn inter_x_if(&mut self, x_if: Arc<RwLock<XIf>>) {
Expand Down Expand Up @@ -4332,6 +4388,18 @@ impl ObjectStore {
}
}

// Persist Halt and Catch Fire.
{
let path = path.join("halt_and_catch_fire");
fs::create_dir_all(&path)?;
for halt_and_catch_fire in self.halt_and_catch_fire.read().unwrap().values() {
let path = path.join(format!("{}.json", halt_and_catch_fire.read().unwrap().id));
let file = fs::File::create(path)?;
let mut writer = io::BufWriter::new(file);
serde_json::to_writer_pretty(&mut writer, &halt_and_catch_fire)?;
}
}

// Persist If.
{
let path = path.join("x_if");
Expand Down Expand Up @@ -5529,6 +5597,24 @@ impl ObjectStore {
}
}

// Load Halt and Catch Fire.
{
let path = path.join("halt_and_catch_fire");
let entries = fs::read_dir(path)?;
for entry in entries {
let entry = entry?;
let path = entry.path();
let file = fs::File::open(path)?;
let reader = io::BufReader::new(file);
let halt_and_catch_fire: Arc<RwLock<HaltAndCatchFire>> =
serde_json::from_reader(reader)?;
store.halt_and_catch_fire.write().unwrap().insert(
halt_and_catch_fire.read().unwrap().id,
halt_and_catch_fire.clone(),
);
}
}

// Load If.
{
let path = path.join("x_if");
Expand Down

0 comments on commit cc2bd6b

Please sign in to comment.