From fea21dbda113dfeff23c1098e0f5093b8ebffdd0 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 1 Sep 2024 23:20:57 +0200 Subject: [PATCH] add morphisms to create edittree from/to digit/char and generic lists --- lib-nested-core/src/editors/char/mod.rs | 70 ++++++++++++--------- lib-nested-core/src/editors/digit/ctx.rs | 63 +++++++++++++++++++ lib-nested-core/src/editors/digit/editor.rs | 4 ++ lib-nested-core/src/editors/list/ctx.rs | 45 ++++--------- lib-nested-core/src/editors/list/editor.rs | 9 +-- 5 files changed, 125 insertions(+), 66 deletions(-) diff --git a/lib-nested-core/src/editors/char/mod.rs b/lib-nested-core/src/editors/char/mod.rs index 3d33efc..f9f97c0 100644 --- a/lib-nested-core/src/editors/char/mod.rs +++ b/lib-nested-core/src/editors/char/mod.rs @@ -25,49 +25,63 @@ pub fn init_ctx( ctx: Arc> ) { { let ctx = ctx.clone(); move |rt, σ| { - { let mut b = rt.write().unwrap().singleton_buffer::(); - if let Some(buf) = b { - // buffer already exists - } else { - // create char buffer - rt.write().unwrap().insert_leaf( - vec![].into_iter(), - ReprLeaf::from_singleton_buffer( - SingletonBuffer::new('\0') - ) - ); - } + if let Some(buf) = b { + // buffer already exists + } else { + // create char buffer + rt.write().unwrap().insert_leaf( + vec![].into_iter(), + ReprLeaf::from_singleton_buffer(SingletonBuffer::new('\0')) + ); } + } + + let char_buf = rt.singleton_buffer::(); + let mut edittree = CharEditor::new_edit_tree( + ctx.clone(), + char_buf, + SingletonBuffer::::new(0).get_port() + ); - let char_buf = rt.singleton_buffer::(); - - let mut edittree = CharEditor::new_edit_tree( - ctx.clone(), - char_buf, - SingletonBuffer::::new(0).get_port() - ); - - rt.insert_leaf( + rt.insert_leaf( Context::parse(&ctx, "EditTree"), ReprLeaf::from_singleton_buffer( SingletonBuffer::new( Arc::new(RwLock::new(edittree)) ) ) - ); + ); - ctx.read().unwrap().setup_edittree( - rt.clone(), - SingletonBuffer::new(0).get_port() - ); - } - + ctx.read().unwrap().setup_edittree(rt); + } } ); + let char_morph_from_edittree = GenericReprTreeMorphism::new( + Context::parse(&ctx, "Char~EditTree"), + Context::parse(&ctx, "Char"), + { + let ctx = ctx.clone(); + move |rt, σ| + { + let mut b = rt + .descend(Context::parse(&ctx, "EditTree")).unwrap() + .view_singleton::>>(); + + rt.attach_leaf_to( + Context::parse(&ctx, "Char"), + b.map(|x| + x.read().unwrap() + .get_edit::().unwrap() + .read().unwrap() + .get()) + ); + } + }); ctx.write().unwrap().morphisms.add_morphism( char_morph_to_edittree ); + ctx.write().unwrap().morphisms.add_morphism( char_morph_from_edittree ); } pub struct CharEditor { diff --git a/lib-nested-core/src/editors/digit/ctx.rs b/lib-nested-core/src/editors/digit/ctx.rs index c62b297..f78b962 100644 --- a/lib-nested-core/src/editors/digit/ctx.rs +++ b/lib-nested-core/src/editors/digit/ctx.rs @@ -19,6 +19,43 @@ pub fn init_ctx( ctx: Arc> ) { // todo: proper scoping of Radix variable ctx.write().unwrap().add_varname("Radix"); + let digit_make_edittree = GenericReprTreeMorphism::new( + Context::parse(&ctx, ""), + Context::parse(&ctx, "~EditTree"), + { + let ctx = ctx.clone(); + move |src_rt, σ| { + let radix = + match σ.get( &laddertypes::TypeID::Var(ctx.read().unwrap().get_var_typeid("Radix").unwrap()) ) { + Some(TypeTerm::Num(n)) => *n as u32, + _ => 0 + }; + + let char_buf = SingletonBuffer::::new('?'); + + /* Create EditTree object + */ + let mut edittree = DigitEditor::new( + ctx.clone(), + radix, + char_buf + ).into_node( + r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() + ); + + src_rt.write().unwrap() + .insert_branch( + ReprTree::from_singleton_buffer( + Context::parse(&ctx, "EditTree"), + SingletonBuffer::new(Arc::new(RwLock::new(edittree))) + ) + ); + + ctx.read().unwrap().setup_edittree( src_rt ); + } + } + ); + let digit_morph_char_to_edittree = GenericReprTreeMorphism::new( Context::parse(&ctx, "~Char"), Context::parse(&ctx, "~EditTree"), @@ -57,6 +94,30 @@ pub fn init_ctx( ctx: Arc> ) { } ); + let digit_morph_char_from_edittree = GenericReprTreeMorphism::new( + Context::parse(&ctx, "~EditTree"), + Context::parse(&ctx, "~Char"), + + { + let ctx = ctx.clone(); + move |src_rt, σ| { + let edittree = src_rt.edittree( &ctx ); + let port = + edittree + .get() + .read().unwrap() + .get_edit::().unwrap() + .read().unwrap() + .get_char_port(); + + src_rt.insert_leaf( + Context::parse(&ctx, "Char"), + ReprLeaf::from_view( port ) + ) + } + } + ); + let digit_morph_char_to_u64 = GenericReprTreeMorphism::new( Context::parse(&ctx, "~Char"), Context::parse(&ctx, "~ℤ_2^64~machine.UInt64"), @@ -134,7 +195,9 @@ pub fn init_ctx( ctx: Arc> ) { ); + ctx.write().unwrap().morphisms.add_morphism( digit_make_edittree ); ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_to_edittree ); + ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_from_edittree ); ctx.write().unwrap().morphisms.add_morphism( digit_morph_char_to_u64 ); ctx.write().unwrap().morphisms.add_morphism( digit_morph_u64_to_char ); } diff --git a/lib-nested-core/src/editors/digit/editor.rs b/lib-nested-core/src/editors/digit/editor.rs index 125d2a3..2f80d5d 100644 --- a/lib-nested-core/src/editors/digit/editor.rs +++ b/lib-nested-core/src/editors/digit/editor.rs @@ -59,6 +59,10 @@ impl DigitEditor { */ } + pub fn get_char_port(&self) -> OuterViewPort> { + self.data.get_port() + } + pub fn get_data_port(&self) -> OuterViewPort>> { let radix = self.radix; self.data.get_port().map(move |c| diff --git a/lib-nested-core/src/editors/list/ctx.rs b/lib-nested-core/src/editors/list/ctx.rs index e4c35fa..0d38e7a 100644 --- a/lib-nested-core/src/editors/list/ctx.rs +++ b/lib-nested-core/src/editors/list/ctx.rs @@ -63,36 +63,24 @@ pub fn init_ctx(ctx: Arc>) { } ); - - let list_morph_editsetup2 = GenericReprTreeMorphism::new( - Context::parse(&ctx, "~EditTree"), - Context::parse(&ctx, ""), + let list_morph_editsetup3 = GenericReprTreeMorphism::new( + Context::parse(&ctx, " ~ EditTree"), + Context::parse(&ctx, " ~ "), { let ctx = ctx.clone(); move |src_rt, σ| { - let edittree = - src_rt - .descend(Context::parse(&ctx, "~EditTree")).unwrap() - .singleton_buffer::>>(); - + let edittree = src_rt.edittree( &ctx ); let list_edit = edittree.get().read().unwrap().get_edit::< ListEditor >().unwrap(); let edittree_items = list_edit.read().unwrap().data.get_port().to_list(); - src_rt.insert_leaf( - Context::parse(&ctx, ""), - ReprLeaf::from_view( - edittree_items - .map(|edittree_char| - edittree_char - .read().unwrap() - .get_edit::().unwrap() - .read().unwrap() - .get() - ) - ) + eprintln!("edittree_items.len() = {:?}", edittree_items.get_view().unwrap().len()); + + src_rt.attach_leaf_to( + Context::parse(&ctx, " ~ ") + .apply_substitution(&|x| σ.get(x).cloned()).clone(), + edittree_items ); } - } ); @@ -132,21 +120,14 @@ pub fn init_ctx(ctx: Arc>) { { let ctx = ctx.clone(); move |src_rt, σ| { - let p = - src_rt - .descend(Context::parse(&ctx, "")).expect("descend") - .get_port::> >>().unwrap(); - - src_rt.attach_leaf_to( - Context::parse(&ctx, " ~ "), - p - ); + let list_port = src_rt.get_port::> >>().unwrap(); + src_rt.attach_leaf_to( Context::parse(&ctx, ""), list_port ); } } ); ctx.write().unwrap().morphisms.add_morphism( list_morph_editsetup1 ); - ctx.write().unwrap().morphisms.add_morphism( list_morph_editsetup2 ); + ctx.write().unwrap().morphisms.add_morphism( list_morph_editsetup3 ); ctx.write().unwrap().morphisms.add_morphism( list_morph_from_vec_char ); ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_char ); ctx.write().unwrap().morphisms.add_morphism( list_morph_to_vec_edittree ); diff --git a/lib-nested-core/src/editors/list/editor.rs b/lib-nested-core/src/editors/list/editor.rs index 64c931f..575f6eb 100644 --- a/lib-nested-core/src/editors/list/editor.rs +++ b/lib-nested-core/src/editors/list/editor.rs @@ -1,7 +1,8 @@ use { r3vi::{ view::{OuterViewPort, singleton::*, sequence::*}, - buffer::{singleton::*, vec::*} + buffer::{singleton::*, vec::*}, + projection::* }, laddertypes::{TypeTerm}, crate::{ @@ -319,11 +320,7 @@ impl ListEditor { let mut b = item.ctrl.spillbuf.write().unwrap(); let rt = ReprTree::new_arc(self.typ.clone()); - let mut et = self.ctx.read().unwrap() - .setup_edittree( - &rt -// , self.depth.map(|d| d+1) - ); + let mut et = self.ctx.read().unwrap().setup_edittree(&rt); if let Some(edittree) = et.as_mut(){