From 8471c7a90f6df2e07d0c9c51157b3f75faeccdd4 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Thu, 18 Jan 2024 18:17:26 +0100 Subject: [PATCH] move morphism to editors/char mod --- examples/tty-02-node/src/main.rs | 85 +++++------------------ lib-nested-core/src/editors/char/mod.rs | 44 +++++++++--- lib-nested-core/src/repr_tree/mod.rs | 4 +- lib-nested-core/src/repr_tree/morphism.rs | 1 - 4 files changed, 54 insertions(+), 80 deletions(-) diff --git a/examples/tty-02-node/src/main.rs b/examples/tty-02-node/src/main.rs index b561928..c3bc61b 100644 --- a/examples/tty-02-node/src/main.rs +++ b/examples/tty-02-node/src/main.rs @@ -28,6 +28,8 @@ async fn main() { */ let ctx = Arc::new(RwLock::new(Context::new())); + nested::editors::char::init_ctx( ctx.clone() ); + /* structure of Repr-Tree * * === Repr-Tree === @@ -67,43 +69,6 @@ async fn main() { port_edit.outer().into() ); - rt_digit.write().unwrap() - .insert_leaf( - vec![ Context::parse(&ctx, "u32") ].into_iter(), - port_u32.outer().into() - ); - - let morphtype = - nested::repr_tree::MorphismType { - src_type: Context::parse(&ctx, "Char"), - dst_type: Context::parse(&ctx, "Char~EditTree") - }; - - ctx.write().unwrap() - .morphisms - .add_morphism( - morphtype, - { - let ctx =ctx.clone(); - move |rt, σ| { - /* Create EditTree object - */ - let mut edittree_char = nested::editors::char::CharEditor::new_edit_tree( - ctx.clone(), - r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() - ); - edittree_char = nested_tty::editors::edittree_make_char_view( edittree_char ); - - /* Insert EditTree into ReprTree - */ - rt.write().unwrap() - .insert_leaf( - vec![ Context::parse(&ctx, "EditTree") ].into_iter(), - SingletonBuffer::new(edittree_char).get_port().into() - ); - } - } - ); /* let rt_string = ReprTree::new_arc( Context::parse(&ctx, "") ); @@ -118,25 +83,24 @@ async fn main() { /* setup projections between representations */ - /* - // created by Char ==> Char~EditTree - let mut edittree_char = - nested::editors::char::CharEditor::new_edit_tree( - ctx.clone(), - r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() - ); -*/ - + eprintln!("rt_digit = {:?}", rt_digit); eprintln!("morph [ Char ==> Char~EditTree ]"); + + let rt_char = rt_digit.read().unwrap() + .descend(Context::parse(&ctx, "Char")) + .unwrap().clone(); + + eprintln!("rt_char = {:?}", rt_char); + ctx.read().unwrap() .morphisms .morph( - rt_digit.read().unwrap() - .descend(Context::parse(&ctx, "Char")) - .unwrap().clone(), + rt_char.clone(), &Context::parse(&ctx, "Char~EditTree") ); + eprintln!("rt_digit = {:?}", rt_char); + let edittree_char = ReprTree::descend_ladder( &rt_digit, @@ -166,34 +130,17 @@ async fn main() { node_edit_digit = nested_tty::editors::edittree_make_digit_view( node_edit_digit ); let mut edit_digit = node_edit_digit.get_edit::< nested::editors::integer::DigitEditor >().unwrap(); - +/* // created by ==> ~U32 port_u32.attach_to( port_char.outer().map(|c| c.to_digit(16).unwrap_or(0)) ); // port_u32.attach_to( edit_digit.read().unwrap().get_data_port().map(|d| d.unwrap_or(0)) ); let port_proj_u32_to_char = port_u32.outer().map(|val| char::from_digit(val, 16).unwrap_or('?') ); + */ let buf_edit_digit = r3vi::buffer::singleton::SingletonBuffer::new( node_edit_digit ); port_edit.attach_to( buf_edit_digit.get_port() ); -/* - ctx.write().unwrap() - .morphisms - .add_morphism( - MorphismType { - src_type: Context::parse(&ctx, "Char"), - dst_type: Context::parse(&ctx, "Char~EditTree") - }, - - |rt, _σ| { - rt.write().unwrap() - .insert_branch( - Context::parse(&ctx, "") - ); - } - ) -*/ - /* setup terminal */ let app = TTYApplication::new({ @@ -224,7 +171,7 @@ async fn main() { compositor .write() .unwrap() - .push(nested_tty::make_label(&label).offset(Vector2::new(0, 2))); + .push(nested_tty::make_label(&label).offset(Vector2::new(0, 1))); /* compositor .write() diff --git a/lib-nested-core/src/editors/char/mod.rs b/lib-nested-core/src/editors/char/mod.rs index d4123e8..7be55a3 100644 --- a/lib-nested-core/src/editors/char/mod.rs +++ b/lib-nested-core/src/editors/char/mod.rs @@ -16,14 +16,42 @@ use { std::sync::RwLock }; -pub fn init_ctx( ctx: &mut Context ) { - /* - ctx.add_node_ctor( - "Char", - Arc::new(|ctx: Arc>, _ty: TypeTerm, depth: OuterViewPort>| { - Some(CharEditor::new_node(ctx, depth)) - })); - */ +pub fn init_ctx( ctx: Arc> ) { + + let morphtype = + crate::repr_tree::MorphismType { + src_type: Context::parse(&ctx, "Char"), + dst_type: Context::parse(&ctx, "Char~EditTree") + }; + + ctx.write().unwrap() + .morphisms + .add_morphism( + morphtype, + { + let ctx = ctx.clone(); + move |rt, σ| { + /* Create EditTree object + */ + let mut edittree_char = CharEditor::new_edit_tree( + ctx.clone(), + r3vi::buffer::singleton::SingletonBuffer::::new(0).get_port() + ); +/* + /* setup tty-view for EditTree + */ + edittree_char = nested_tty::editors::edittree_make_char_view( edittree_char ); +*/ + /* Insert EditTree into ReprTree + */ + let mut rt = rt.write().unwrap(); + rt.insert_leaf( + vec![ Context::parse(&ctx, "EditTree") ].into_iter(), + SingletonBuffer::new(edittree_char).get_port().into() + ); + } + } + ); } pub struct CharEditor { diff --git a/lib-nested-core/src/repr_tree/mod.rs b/lib-nested-core/src/repr_tree/mod.rs index bd62527..1f5ce63 100644 --- a/lib-nested-core/src/repr_tree/mod.rs +++ b/lib-nested-core/src/repr_tree/mod.rs @@ -26,10 +26,10 @@ pub struct ReprTree { impl std::fmt::Debug for ReprTree { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "type: {:?}", self.type_tag)?; + writeln!(f, "| type: {:?}", self.type_tag)?; for (_k,x) in self.branches.iter() { - write!(f, "child: {:?}", x)?; + writeln!(f, "|--> child: {:?}", x)?; } Ok(()) diff --git a/lib-nested-core/src/repr_tree/morphism.rs b/lib-nested-core/src/repr_tree/morphism.rs index 2cb58cc..b7cc3bc 100644 --- a/lib-nested-core/src/repr_tree/morphism.rs +++ b/lib-nested-core/src/repr_tree/morphism.rs @@ -68,7 +68,6 @@ impl MorphismBase { ); if let Ok(σ) = unification_problem.solve() { - eprintln!("found matching morphism"); return Some((m, σ)); } }