Skip to content

Commit

Permalink
Added Map to LuDog.
Browse files Browse the repository at this point in the history
  • Loading branch information
uberFoo committed Apr 29, 2024
1 parent 069fe0d commit 01351bb
Show file tree
Hide file tree
Showing 30 changed files with 1,512 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sarzak"
version = "2.13.23"
version = "2.13.24"
edition = "2021"
authors = ["Keith T. Star <[email protected]>"]
categories = ["compilers", "memory-management"]
Expand Down
2 changes: 1 addition & 1 deletion models/lu_dog.json

Large diffs are not rendered by default.

59 changes: 58 additions & 1 deletion src/v2/lu_dog/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
//! * [`Literal`]
//! * [`LocalVariable`]
//! * [`XMacro`]
//! * [`Map`]
//! * [`XMatch`]
//! * [`MethodCall`]
//! * [`NamedFieldExpression`]
Expand Down Expand Up @@ -109,7 +110,7 @@ use crate::v2::lu_dog::types::{
FieldAccessTarget, FieldExpression, FloatLiteral, ForLoop, FormatBit, FormatString,
FuncGeneric, Function, FunctionCall, Grouped, HaltAndCatchFire, ImplementationBlock, Import,
Index, IntegerLiteral, Item, Lambda, LambdaParameter, LetStatement, List, ListElement,
ListExpression, Literal, LocalVariable, MethodCall, NamedFieldExpression, ObjectWrapper,
ListExpression, Literal, LocalVariable, Map, MethodCall, NamedFieldExpression, ObjectWrapper,
Operator, Parameter, PathElement, Pattern, RangeExpression, ResultStatement, Span, Statement,
StaticMethodCall, StringBit, StringLiteral, StructExpression, StructField, StructGeneric,
TupleField, TypeCast, Unary, Unit, UnnamedFieldExpression, ValueType, Variable,
Expand Down Expand Up @@ -170,6 +171,7 @@ pub struct ObjectStore {
literal: Rc<RefCell<HashMap<Uuid, Rc<RefCell<Literal>>>>>,
local_variable: Rc<RefCell<HashMap<Uuid, Rc<RefCell<LocalVariable>>>>>,
x_macro: Rc<RefCell<HashMap<Uuid, Rc<RefCell<XMacro>>>>>,
map: Rc<RefCell<HashMap<Uuid, Rc<RefCell<Map>>>>>,
x_match: Rc<RefCell<HashMap<Uuid, Rc<RefCell<XMatch>>>>>,
method_call: Rc<RefCell<HashMap<Uuid, Rc<RefCell<MethodCall>>>>>,
named_field_expression: Rc<RefCell<HashMap<Uuid, Rc<RefCell<NamedFieldExpression>>>>>,
Expand Down Expand Up @@ -262,6 +264,7 @@ impl ObjectStore {
literal: Rc::new(RefCell::new(HashMap::default())),
local_variable: Rc::new(RefCell::new(HashMap::default())),
x_macro: Rc::new(RefCell::new(HashMap::default())),
map: Rc::new(RefCell::new(HashMap::default())),
x_match: Rc::new(RefCell::new(HashMap::default())),
method_call: Rc::new(RefCell::new(HashMap::default())),
named_field_expression: Rc::new(RefCell::new(HashMap::default())),
Expand Down Expand Up @@ -2218,6 +2221,34 @@ impl ObjectStore {
(0..len).map(move |i| values[i].clone())
}

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

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

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

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

/// Inter (insert) [`XMatch`] into the store.
///
pub fn inter_x_match(&mut self, x_match: Rc<RefCell<XMatch>>) {
Expand Down Expand Up @@ -4162,6 +4193,18 @@ impl ObjectStore {
}
}

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

// Persist Match.
{
let path = path.join("x_match");
Expand Down Expand Up @@ -5422,6 +5465,20 @@ impl ObjectStore {
}
}

// Load Map.
{
let path = path.join("map");
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 map: Rc<RefCell<Map>> = serde_json::from_reader(reader)?;
store.map.borrow_mut().insert(map.borrow().id, map.clone());
}
}

// Load Match.
{
let path = path.join("x_match");
Expand Down
2 changes: 2 additions & 0 deletions src/v2/lu_dog/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub mod list_expression;
pub mod literal;
pub mod local_variable;
pub mod macro_call;
pub mod map;
pub mod method_call;
pub mod multiplication;
pub mod named_field_expression;
Expand Down Expand Up @@ -211,6 +212,7 @@ pub use crate::v2::lu_dog::literal::LiteralEnum;
pub use crate::v2::lu_dog::local_variable::LocalVariable;
pub use crate::v2::lu_dog::macro_call::MacroCall;
pub use crate::v2::lu_dog::macro_call::MACRO_CALL;
pub use crate::v2::lu_dog::map::Map;
pub use crate::v2::lu_dog::method_call::MethodCall;
pub use crate::v2::lu_dog::multiplication::Multiplication;
pub use crate::v2::lu_dog::multiplication::MULTIPLICATION;
Expand Down
76 changes: 76 additions & 0 deletions src/v2/lu_dog/types/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// {"magic":"","directive":{"Start":{"directive":"allow-editing","tag":"map-struct-definition-file"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-use-statements"}}}
use std::cell::RefCell;
use std::rc::Rc;
use uuid::Uuid;

use crate::v2::lu_dog::types::value_type::ValueType;
use crate::v2::lu_dog::types::value_type::ValueTypeEnum;
use serde::{Deserialize, Serialize};

use crate::v2::lu_dog::store::ObjectStore as LuDogStore;
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}

// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-struct-documentation"}}}
/// This is a hashmap.
///
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-struct-definition"}}}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Map {
pub id: Uuid,
/// R115: [`Map`] 'has a key' [`ValueType`]
pub key_type: Uuid,
/// R116: [`Map`] 'values have' [`ValueType`]
pub value_type: Uuid,
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-implementation"}}}
impl Map {
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-struct-impl-new"}}}
/// Inter a new 'Map' in the store, and return it's `id`.
pub fn new(
key_type: &Rc<RefCell<ValueType>>,
value_type: &Rc<RefCell<ValueType>>,
store: &mut LuDogStore,
) -> Rc<RefCell<Map>> {
let id = Uuid::new_v4();
let new = Rc::new(RefCell::new(Map {
id,
key_type: key_type.borrow().id,
value_type: value_type.borrow().id,
}));
store.inter_map(new.clone());
new
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-struct-impl-nav-forward-to-key_type"}}}
/// Navigate to [`ValueType`] across R115(1-*)
pub fn r115_value_type<'a>(&'a self, store: &'a LuDogStore) -> Vec<Rc<RefCell<ValueType>>> {
vec![store.exhume_value_type(&self.key_type).unwrap()]
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-struct-impl-nav-forward-to-value_type"}}}
/// Navigate to [`ValueType`] across R116(1-*)
pub fn r116_value_type<'a>(&'a self, store: &'a LuDogStore) -> Vec<Rc<RefCell<ValueType>>> {
vec![store.exhume_value_type(&self.value_type).unwrap()]
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"map-impl-nav-subtype-to-supertype-value_type"}}}
// Navigate to [`ValueType`] across R1(isa)
pub fn r1_value_type<'a>(&'a self, store: &'a LuDogStore) -> Vec<Rc<RefCell<ValueType>>> {
vec![store
.iter_value_type()
.find(|value_type| {
if let ValueTypeEnum::Map(id) = value_type.borrow().subtype {
id == self.id
} else {
false
}
})
.unwrap()]
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"End":{"directive":"allow-editing"}}}
36 changes: 36 additions & 0 deletions src/v2/lu_dog/types/value_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::v2::lu_dog::types::import::Import;
use crate::v2::lu_dog::types::lambda::Lambda;
use crate::v2::lu_dog::types::lambda_parameter::LambdaParameter;
use crate::v2::lu_dog::types::list::List;
use crate::v2::lu_dog::types::map::Map;
use crate::v2::lu_dog::types::parameter::Parameter;
use crate::v2::lu_dog::types::range::RANGE;
use crate::v2::lu_dog::types::span::Span;
Expand Down Expand Up @@ -79,6 +80,7 @@ pub enum ValueTypeEnum {
Import(Uuid),
Lambda(Uuid),
List(Uuid),
Map(Uuid),
ZObjectStore(Uuid),
XPlugin(Uuid),
Range(Uuid),
Expand Down Expand Up @@ -271,6 +273,23 @@ impl ValueType {
new
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"value_type-struct-impl-new_map"}}}
/// Inter a new ValueType in the store, and return it's `id`.
pub fn new_map(
bogus: bool,
subtype: &Rc<RefCell<Map>>,
store: &mut LuDogStore,
) -> Rc<RefCell<ValueType>> {
let id = Uuid::new_v4();
let new = Rc::new(RefCell::new(ValueType {
bogus: bogus,
subtype: ValueTypeEnum::Map(subtype.borrow().id), // b
id,
}));
store.inter_value_type(new.clone());
new
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"value_type-struct-impl-new_z_object_store"}}}
/// Inter a new ValueType in the store, and return it's `id`.
pub fn new_z_object_store(
Expand Down Expand Up @@ -456,6 +475,23 @@ impl ValueType {
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"value_type-struct-impl-nav-backward-1_M-to-woog_option"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"value_type-struct-impl-nav-backward-1_M-to-map"}}}
/// Navigate to [`Map`] across R115(1-M)
pub fn r115_map<'a>(&'a self, store: &'a LuDogStore) -> Vec<Rc<RefCell<Map>>> {
store
.iter_map()
.filter(|map| map.borrow().key_type == self.id)
.collect()
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"value_type-struct-impl-nav-backward-1_M-to-map"}}}
/// Navigate to [`Map`] across R116(1-M)
pub fn r116_map<'a>(&'a self, store: &'a LuDogStore) -> Vec<Rc<RefCell<Map>>> {
store
.iter_map()
.filter(|map| map.borrow().value_type == self.id)
.collect()
}
// {"magic":"","directive":{"End":{"directive":"ignore-orig"}}}
// {"magic":"","directive":{"Start":{"directive":"ignore-orig","tag":"value_type-struct-impl-nav-backward-1_M-to-parameter"}}}
/// Navigate to [`Parameter`] across R79(1-M)
Expand Down
Loading

0 comments on commit 01351bb

Please sign in to comment.