From 830ce613ea18c677848f26e687cf0b20233cac98 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 2 Aug 2024 22:43:00 +0200 Subject: [PATCH] example: two editors with different radices --- examples/tty-04-posint/src/main.rs | 240 ++++++++++++---------- lib-nested-core/src/repr_tree/morphism.rs | 11 + 2 files changed, 140 insertions(+), 111 deletions(-) diff --git a/examples/tty-04-posint/src/main.rs b/examples/tty-04-posint/src/main.rs index 67385eb..ed8311f 100644 --- a/examples/tty-04-posint/src/main.rs +++ b/examples/tty-04-posint/src/main.rs @@ -47,24 +47,101 @@ fn rebuild_projections( } } -fn setup_le_master(ctx: &Arc>, rt_int: &Arc>) { +fn setup_hex_master(ctx: &Arc>, rt_int: &Arc>) { rebuild_projections( ctx.clone(), rt_int.clone(), vec![ - // Little Endian Editor + // extract values from hex-editor ( - "ℕ ~ ~ > ~ ~ Char> ~ EditTree", - "ℕ ~ ~ > ~ ~ Char>" + "ℕ ~ ~ > ~ ~ Char> ~ EditTree", + "ℕ ~ ~ > ~ ~ Char>" ), - // Convert Endianness + // convert to little-endian + ( + "ℕ ~ ~ > ~ ~ Char>", + "ℕ ~ ~ > ~ ~ Char>" + ), + // convert digit representation from char to u64 + ( + "ℕ ~ ~ > ~ ~ Char>", + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" + ), + // convert radix to decimal + ( + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>", + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" + ), + // convert decimal digit representation back to char + ( + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>", + "ℕ ~ ~ > ~ ~ Char>" + ), + // convert to big-endian + ( + "ℕ ~ ~ > ~ ~ Char>", + "ℕ ~ ~ > ~ ~ Char>" + ), + + // decimal editor + ( + "ℕ ~ ~ > ~ ~ Char>", + "ℕ ~ ~ > ~ ~ Char ~ EditTree>" + ), + ( + "ℕ ~ ~ > ~ ~ Char ~ EditTree>", + "ℕ ~ ~ > ~ ~ Char ~ EditTree> ~ " + ), + ( + "ℕ ~ ~ > ~ ~ Char ~ EditTree> ~ ", + "ℕ ~ ~ > ~ ~ Char> ~ EditTree" + ), + + ].into_iter() + .map(|(s,d)| (Context::parse(&ctx, s), Context::parse(&ctx, d))) + .collect() + ); +} + +fn setup_dec_master(ctx: &Arc>, rt_int: &Arc>) { + rebuild_projections( + ctx.clone(), + rt_int.clone(), + vec![ + // extract values from decimal-editor + ( + "ℕ ~ ~ > ~ ~ Char> ~ EditTree", + "ℕ ~ ~ > ~ ~ Char>" + ), + + // convert to little-endian + ( + "ℕ ~ ~ > ~ ~ Char>", + "ℕ ~ ~ > ~ ~ Char>" + ), + // convert digit representation to u64 + ( + "ℕ ~ ~ > ~ ~ Char>", + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" + ), + // convert radix to decimal + ( + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>", + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" + ), + // convert back digit representation char + ( + "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>", + "ℕ ~ ~ > ~ ~ Char>" + ), + // convert back to big-endian ( "ℕ ~ ~ > ~ ~ Char>", "ℕ ~ ~ > ~ ~ Char>" ), - // Big Endian Editor + // hex editor ( "ℕ ~ ~ > ~ ~ Char>", "ℕ ~ ~ > ~ ~ Char ~ EditTree>" @@ -77,65 +154,6 @@ fn setup_le_master(ctx: &Arc>, rt_int: &Arc>) { "ℕ ~ ~ > ~ ~ Char ~ EditTree> ~ ", "ℕ ~ ~ > ~ ~ Char> ~ EditTree" ), - - // convert to different digit-representation - ( - "ℕ ~ ~ > ~ ~ Char>", - "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" - ), - // Radix Convert - ( - "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>", - "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" - ), - ].into_iter() - .map(|(s,d)| (Context::parse(&ctx, s), Context::parse(&ctx, d))) - .collect() - ); -} - -fn setup_be_master(ctx: &Arc>, rt_int: &Arc>) { - rebuild_projections( - ctx.clone(), - rt_int.clone(), - vec![ - // Big Endian Editor - ( - "ℕ ~ ~ > ~ ~ Char> ~ EditTree", - "ℕ ~ ~ > ~ ~ Char>" - ), - - // Convert Endianness - ( - "ℕ ~ ~ > ~ ~ Char>", - "ℕ ~ ~ > ~ ~ Char>" - ), - - // Little Endian Editor - ( - "ℕ ~ ~ > ~ ~ Char>", - "ℕ ~ ~ > ~ ~ Char ~ EditTree>" - ), - ( - "ℕ ~ ~ > ~ ~ Char ~ EditTree>", - "ℕ ~ ~ > ~ ~ Char ~ EditTree> ~ " - ), - ( - "ℕ ~ ~ > ~ ~ Char ~ EditTree> ~ ", - "ℕ ~ ~ > ~ ~ Char> ~ EditTree" - ), - - - // convert to different digit-representation - ( - "ℕ ~ ~ > ~ ~ Char>", - "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" - ), - // Radix Convert - ( - "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>", - "ℕ ~ ~ > ~ ~ ℤ_2^64 ~ machine.UInt64>" - ) ].into_iter() .map(|(s,d)| (Context::parse(&ctx, s), Context::parse(&ctx, d))) .collect() @@ -159,32 +177,19 @@ async fn main() { /* Add a specific Representation-Path (big-endian hexadecimal) */ - let mut digits_be = VecBuffer::with_data(vec![ 'c', 'f', 'f' ]); + let mut digits_hex = VecBuffer::with_data(vec![ 'c', 'f', 'f' ]); rt_int.insert_leaf( Context::parse(&ctx, "~>~>~~"), - nested::repr_tree::ReprLeaf::from_vec_buffer( digits_be.clone() ) + nested::repr_tree::ReprLeaf::from_vec_buffer( digits_hex.clone() ) ); - let mut digits_le = VecBuffer::with_data(vec!['3', '2', '1']); + let mut digits_dec = VecBuffer::with_data(vec!['3', '2', '1']); rt_int.insert_leaf( - Context::parse(&ctx, "~>~>~~"), - nested::repr_tree::ReprLeaf::from_vec_buffer( digits_le.clone() ) + Context::parse(&ctx, "~>~>~~"), + nested::repr_tree::ReprLeaf::from_vec_buffer( digits_dec.clone() ) ); - let mut digits_le_editvec = VecBuffer::>>::new(); - rt_int.insert_leaf( - Context::parse(&ctx, " - - ~ > - ~ - ~ Char - ~ EditTree> - ~ - "), - nested::repr_tree::ReprLeaf::from_vec_buffer( digits_le_editvec.clone() ) - ); - - let mut digits_be_editvec = VecBuffer::>>::new(); + let mut digits_hex_editvec = VecBuffer::>>::new(); rt_int.insert_leaf( Context::parse(&ctx, " @@ -194,7 +199,20 @@ async fn main() { ~ EditTree> ~ "), - nested::repr_tree::ReprLeaf::from_vec_buffer( digits_be_editvec.clone() ) + nested::repr_tree::ReprLeaf::from_vec_buffer( digits_hex_editvec.clone() ) + ); + + let mut digits_dec_editvec = VecBuffer::>>::new(); + rt_int.insert_leaf( + Context::parse(&ctx, " + + ~ > + ~ + ~ Char + ~ EditTree> + ~ + "), + nested::repr_tree::ReprLeaf::from_vec_buffer( digits_dec_editvec.clone() ) ); /* initially copy values from Vec to EditTree... @@ -205,38 +223,27 @@ async fn main() { // master representation vec![ ( - "ℕ ~ ~ > ~ ~ Char> ~ ", - "ℕ ~ ~ > ~ ~ Char>" + "ℕ ~ ~ > ~ ~ Char> ~ ", + "ℕ ~ ~ > ~ ~ Char>" ), ( - "ℕ ~ ~ > ~ ~ Char>", - "ℕ ~ ~ > ~ ~ Char ~ EditTree>" + "ℕ ~ ~ > ~ ~ Char>", + "ℕ ~ ~ > ~ ~ Char ~ EditTree>" ), ( - "ℕ ~ ~ > ~ ~ Char> ~ ", - "ℕ ~ ~ > ~ ~ Char> ~ ~ " + "ℕ ~ ~ > ~ ~ Char> ~ ", + "ℕ ~ ~ > ~ ~ Char> ~ ~ " ), ( - "ℕ ~ ~ > ~ ~ Char> ~ ~ ", - "ℕ ~ ~ > ~ ~ Char> ~ EditTree" + "ℕ ~ ~ > ~ ~ Char> ~ ~ ", + "ℕ ~ ~ > ~ ~ Char> ~ EditTree" ), ].into_iter() .map(|(s,d)| (Context::parse(&ctx, s), Context::parse(&ctx, d))) .collect() ); - setup_le_master(&ctx, &rt_int); - - let edittree_hex_le_list = ctx.read().unwrap() - .setup_edittree( - rt_int.descend(Context::parse(&ctx," - - ~ > - ~ > - ~ - ")).expect("descend"), - SingletonBuffer::new(0).get_port() - ).unwrap().get(); + setup_hex_master(&ctx, &rt_int); let edittree_hex_be_list = ctx.read().unwrap() .setup_edittree( @@ -249,6 +256,17 @@ async fn main() { SingletonBuffer::new(0).get_port() ).unwrap().get(); + let edittree_dec_be_list = ctx.read().unwrap() + .setup_edittree( + rt_int.descend(Context::parse(&ctx," + + ~ > + ~ > + ~ + ")).expect("descend"), + SingletonBuffer::new(0).get_port() + ).unwrap().get(); + let hex_digits_view = rt_int.descend(Context::parse(&ctx, " @@ -280,17 +298,17 @@ async fn main() { */ let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "")); list_editor.data.push( edittree_hex_be_list.clone() ); - list_editor.data.push( edittree_hex_le_list.clone() ); + list_editor.data.push( edittree_dec_be_list.clone() ); let mut edittree = list_editor.into_node(SingletonBuffer::new(0).get_port()); /* cursors are a bit screwed initially so fix them up * TODO: how to fix this generally? */ edittree_hex_be_list.write().unwrap().goto(TreeCursor::none()); - edittree_hex_le_list.write().unwrap().goto(TreeCursor::none()); + edittree_dec_be_list.write().unwrap().goto(TreeCursor::none()); edittree.goto(TreeCursor{ leaf_mode: nested::editors::list::ListCursorMode::Insert, - tree_addr: vec![1,0] + tree_addr: vec![0,0] }); let edittree = Arc::new(RwLock::new(edittree)); @@ -310,14 +328,14 @@ async fn main() { 0 => { let mut li = last_idx.write().unwrap(); if *li != 0 { - setup_be_master(&ctx, &rt_int); + setup_hex_master(&ctx, &rt_int); *li = 0; } } 1 => { let mut li = last_idx.write().unwrap(); if *li != 1 { - setup_le_master(&ctx, &rt_int); + setup_dec_master(&ctx, &rt_int); *li = 1; } } @@ -355,7 +373,7 @@ async fn main() { } show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, " ~ ~Char>")).expect(""), 1); - show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, " ~ > ~ ~Char>")).expect(""), 4); + show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, " ~ ~Char>")).expect(""), 4); /* project the seq of u64 representations to a view */ diff --git a/lib-nested-core/src/repr_tree/morphism.rs b/lib-nested-core/src/repr_tree/morphism.rs index 68e837c..32db193 100644 --- a/lib-nested-core/src/repr_tree/morphism.rs +++ b/lib-nested-core/src/repr_tree/morphism.rs @@ -93,6 +93,17 @@ impl MorphismBase { } } + /* if src_item_type ~== "machine.UInt64", + dst_item_type ~== "Char" + */ + else if src_item_type_lnf.last() == Some(&TypeTerm::TypeID(TypeID::Fun(4))) && + dst_item_type_lnf.last() == Some(&TypeTerm::TypeID(TypeID::Fun(0))) + { + if let Some((m, σ)) = self.find_list_map_morphism::< u64, char >( src_item_type, dst_item_type ) { + return Some((m, σ)); + } + } + /* if src_item_type ~== "Char" dst_item_type ~== "EditTree" */