add static TypeIDs to create basic types without context

This commit is contained in:
Michael Sippel 2024-08-22 17:41:57 +02:00
parent e7331b36ae
commit fb796cda04
Signed by: senvas
GPG key ID: F96CF119C34B64A6
3 changed files with 95 additions and 29 deletions

View file

@ -13,6 +13,14 @@ use {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub static TYPEID_edittree : TypeID = TypeID::Fun(0);
pub static TYPEID_char : TypeID = TypeID::Fun(1);
pub static TYPEID_u64 : TypeID = TypeID::Fun(2);
pub static TYPEID_list : TypeID = TypeID::Fun(3);
pub static TYPEID_vec : TypeID = TypeID::Fun(4);
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
#[derive(Clone)] #[derive(Clone)]
pub struct Context { pub struct Context {
/// assigns a name to every type /// assigns a name to every type
@ -35,19 +43,27 @@ pub struct Context {
parent: Option<Arc<RwLock<Context>>>, parent: Option<Arc<RwLock<Context>>>,
} }
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl Context { impl Context {
pub fn with_parent( pub fn with_parent(
parent: Option<Arc<RwLock<Context>>> parent: Option<Arc<RwLock<Context>>>
) -> Self { ) -> Self {
let mut dict = TypeDict::new();
let list_typeid = dict.add_typename("List".into());
Context { Context {
type_dict: match parent.as_ref() { type_dict: match parent.as_ref() {
Some(p) => p.read().unwrap().type_dict.clone(), Some(p) => p.read().unwrap().type_dict.clone(),
None => Arc::new(RwLock::new(dict)) None => {
let mut dict = TypeDict::new();
assert_eq!( TYPEID_edittree, dict.add_typename("EditTree".into()) );
assert_eq!( TYPEID_char, dict.add_typename("Char".into()) );
assert_eq!( TYPEID_u64, dict.add_typename("machine.UInt64".into()) );
assert_eq!( TYPEID_list, dict.add_typename("List".into()) );
assert_eq!( TYPEID_vec, dict.add_typename("Vec".into()) );
Arc::new(RwLock::new(dict))
}
}, },
morphisms: MorphismBase::new( list_typeid ), morphisms: MorphismBase::new( TYPEID_list ),
nodes: HashMap::new(), nodes: HashMap::new(),
list_types: match parent.as_ref() { list_types: match parent.as_ref() {
Some(p) => p.read().unwrap().list_types.clone(), Some(p) => p.read().unwrap().list_types.clone(),

View file

@ -1,8 +1,11 @@
use { use {
laddertypes::{TypeTerm, TypeID, morphism::Morphism}, laddertypes::{TypeTerm, TypeID, morphism::Morphism},
r3vi::view::{AnyOuterViewPort, port::UpdateTask}, r3vi::view::{AnyOuterViewPort, port::*, list::*},
crate::{ crate::{
repr_tree::{ReprTree, ReprTreeExt, ReprLeaf}, repr_tree::{
ReprTree, ReprTreeExt, ReprLeaf,
context::{*}
},
}, },
std::{ std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
@ -122,29 +125,64 @@ impl GenericReprTreeMorphism {
pub fn into_list_map_dyn(&self, typeid_list: TypeID) pub fn into_list_map_dyn(&self, typeid_list: TypeID)
-> Option< GenericReprTreeMorphism > -> Option< GenericReprTreeMorphism >
{ {
let typeid_char = TypeID::Fun(1);
let typeid_u64 = TypeID::Fun(5);
let typeid_edittree = TypeID::Fun(2);
let src_item_type_lnf = self.morph_type.src_type.clone().get_lnf_vec(); let src_item_type_lnf = self.morph_type.src_type.clone().get_lnf_vec();
let dst_item_type_lnf = self.morph_type.dst_type.clone().get_lnf_vec(); let dst_item_type_lnf = self.morph_type.dst_type.clone().get_lnf_vec();
if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_char)) && eprintln!("into list map dyn");
dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_u64))
if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char)) &&
dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char))
{ {
Some( self.into_list_map::< char, u64 >(typeid_list) ) Some( self.into_list_map::< char, char >(TYPEID_list) )
} }
else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_u64)) && else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64)) &&
dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_char)) dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64))
{ {
Some( self.into_list_map::< u64, char >(typeid_list) ) Some( self.into_list_map::< u64, u64 >(TYPEID_list) )
} }
else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_char)) && else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char)) &&
dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(typeid_edittree)) dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64))
{ {
Some( self.into_list_map::< char, Arc<RwLock<crate::edit_tree::EditTree>> >(typeid_list) ) Some( self.into_list_map::< char, u64 >(TYPEID_list) )
} }
else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_u64)) &&
dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char))
{
Some( self.into_list_map::< u64, char >(TYPEID_list) )
}
else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char)) &&
dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_edittree))
{
Some( self.into_list_map::< char, Arc<RwLock<crate::edit_tree::EditTree>> >(TYPEID_list) )
}
else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_edittree)) &&
dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TYPEID_char))
{
Some( self.into_list_map::< Arc<RwLock<crate::edit_tree::EditTree>>, char >(TYPEID_list) )
}
/*
else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ])) &&
dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ]))
{
Some( self.into_list_map::< char, char >(typeid_list) )
}
else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ])) &&
dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ]))
{
Some( self.into_list_map::< u64, u64 >(typeid_list) )
}
else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ])) &&
dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ]))
{
Some( self.into_list_map::< OuterViewPort<dyn ListView<char>>, OuterViewPort<dyn ListView<u64>> >(typeid_list) )
}
else if src_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_u64) ])) &&
dst_item_type_lnf.last() == Some(&TypeTerm::App(vec![ TypeTerm::TypeID(typeid_list), TypeTerm::TypeID(typeid_char) ]))
{
Some( self.into_list_map::< OuterViewPort<dyn ListView<u64>>, OuterViewPort<dyn ListView<char>> >(typeid_list) )
}
*/
else else
{ {
eprintln!("no list map type for {:?}", dst_item_type_lnf.last()); eprintln!("no list map type for {:?}", dst_item_type_lnf.last());

View file

@ -19,7 +19,7 @@ use {
sync::{Arc, RwLock}, sync::{Arc, RwLock},
any::Any any::Any
}, },
super::{Context, ReprLeaf, ReprTreeExt} super::{Context, ReprLeaf, ReprTreeExt, context::{TYPEID_vec, TYPEID_char, TYPEID_u64, TYPEID_edittree}}
}; };
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
@ -139,7 +139,6 @@ impl ReprTree {
Arc::new(RwLock::new(rt)) Arc::new(RwLock::new(rt))
} }
pub fn from_str( pub fn from_str(
type_tag: impl Into<TypeTerm>, type_tag: impl Into<TypeTerm>,
val: &str val: &str
@ -204,13 +203,13 @@ impl ReprTree {
return; return;
} }
} }
if let Some(leaf) = self.leaf.as_mut() { if let Some(leaf) = self.leaf.as_mut() {
leaf.attach_to(src_port); leaf.attach_to(src_port);
} else { } else {
if self.type_tag == TypeTerm::App(vec![ if self.type_tag == TypeTerm::App(vec![
TypeTerm::TypeID(TypeID::Fun(11)), TypeTerm::TypeID(TYPEID_vec),
TypeTerm::TypeID(TypeID::Fun(2)) TypeTerm::TypeID(TYPEID_edittree)
]) { ]) {
let mut leaf = ReprLeaf::from_vec_buffer( let mut leaf = ReprLeaf::from_vec_buffer(
VecBuffer::< VecBuffer::<
@ -222,8 +221,8 @@ impl ReprTree {
self.leaf = Some(leaf); self.leaf = Some(leaf);
} }
else if self.type_tag == TypeTerm::App(vec![ else if self.type_tag == TypeTerm::App(vec![
TypeTerm::TypeID(TypeID::Fun(11)), TypeTerm::TypeID(TYPEID_vec),
TypeTerm::TypeID(TypeID::Fun(1)) TypeTerm::TypeID(TYPEID_char)
]) { ]) {
let mut leaf = ReprLeaf::from_vec_buffer( let mut leaf = ReprLeaf::from_vec_buffer(
VecBuffer::<char>::new() VecBuffer::<char>::new()
@ -231,13 +230,25 @@ impl ReprTree {
leaf.attach_to(src_port); leaf.attach_to(src_port);
self.leaf = Some(leaf); self.leaf = Some(leaf);
} else { }
else if self.type_tag == TypeTerm::App(vec![
TypeTerm::TypeID(TYPEID_vec),
TypeTerm::TypeID(TYPEID_u64)
]) {
let mut leaf = ReprLeaf::from_vec_buffer(
VecBuffer::<u64>::new()
);
leaf.attach_to(src_port);
self.leaf = Some(leaf);
} else {
self.leaf = Some(ReprLeaf::from_view(src_port)); self.leaf = Some(ReprLeaf::from_view(src_port));
} }
} }
} }
pub fn detach(&mut self, ctx: &Arc<RwLock<Context>>) { pub fn detach(&mut self, ctx: &Arc<RwLock<Context>>) {
// eprintln!("DETACH {:?}", self.get_type());
if let Some(leaf) = self.leaf.as_mut() { if let Some(leaf) = self.leaf.as_mut() {
if self.type_tag == Context::parse(&ctx, "Char") { if self.type_tag == Context::parse(&ctx, "Char") {
leaf.detach::<dyn SingletonView<Item = char>>(); leaf.detach::<dyn SingletonView<Item = char>>();
@ -392,3 +403,4 @@ impl ReprTree {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>