From 9fe8fa9e3b6ea12bff09693b4e40d64186b1a7e4 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sat, 25 May 2024 19:21:16 +0200 Subject: [PATCH] clean up posint example & use u64 instead of usize in PositionalUInt trait --- examples/tty-04-posint/src/main.rs | 164 +++++++++++-------- lib-nested-core/src/editors/digit/ctx.rs | 20 +-- lib-nested-core/src/editors/integer/ctx.rs | 12 +- lib-nested-core/src/editors/integer/mod.rs | 34 ++-- lib-nested-core/src/editors/integer/radix.rs | 20 +-- lib-nested-core/src/repr_tree/mod.rs | 20 +++ 6 files changed, 158 insertions(+), 112 deletions(-) diff --git a/examples/tty-04-posint/src/main.rs b/examples/tty-04-posint/src/main.rs index 687d345..659d499 100644 --- a/examples/tty-04-posint/src/main.rs +++ b/examples/tty-04-posint/src/main.rs @@ -43,48 +43,54 @@ async fn main() { nested::editors::list::init_ctx( ctx.clone() ); nested_tty::setup_edittree_hook(&ctx); - /* Create a Representation-Tree of type > - */ - let mut rt_digitlist = ReprTree::new_arc( Context::parse(&ctx, ">") ); - let mut rt_digitseq = ReprTree::new_arc( Context::parse(&ctx, ">") ); - rt_digitseq.insert_branch( rt_digitlist ); - let mut rt_posint = ReprTree::new_arc(Context::parse(&ctx, "")); - rt_posint.insert_branch( rt_digitseq ); - let mut rt_int = ReprTree::new_arc( Context::parse(&ctx, "ℕ") ); - rt_int.insert_branch( rt_posint ); - /* Setup an Editor for this ReprTree - * (this will add the representation >~EditTree to the ReprTree) + /* Create a Representation-Tree of type `ℕ` + */ + let mut rt_int = ReprTree::new_arc( Context::parse(&ctx, "ℕ") ); + + /* Add a specific Representation-Path (big-endian hexadecimal) + */ + rt_int.create_branch( + Context::parse(&ctx, " ~ > ~ >") + ); + + /* Setup an Editor for the big-endian hexadecimal representation + * (this will add the representation `>~EditTree` to the ReprTree) */ let rt_edittree_list = ctx.read().unwrap() .setup_edittree( - ReprTree::descend( - &rt_int, - Context::parse(&ctx, "~>") - ).expect("cant descend reprtree"), + rt_int.descend(Context::parse(&ctx, " + + ~ > + ~ > + ")).expect("cant descend reprtree"), SingletonBuffer::new(0).get_port() ); + /* Setup a morphism to extract Char values from the list-editor + */ ctx.read().unwrap().morphisms.apply_morphism( - ReprTree::descend(&rt_int, - Context::parse(&ctx, " - - ~> - ~> - ") - ).expect("cant descend repr tree"), + rt_int.descend(Context::parse(&ctx, " + + ~ > + ~ > + ")).expect("cant descend reprtree"), &Context::parse(&ctx, ">~EditTree"), &Context::parse(&ctx, "~Char>") ); /* * map seq of chars to seq of u64 digits + * and add this projection to the ReprTree */ - let mut chars_view = - ReprTree::descend( - &rt_int, - Context::parse(&ctx, "~>~~Char>") - ).expect("cant descend") + + // + //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV + let mut chars_view = rt_int.descend(Context::parse(&ctx, " + < PosInt 16 BigEndian > + ~ < Seq > + ~ < List ~Char > + ")).expect("cant descend") .read().unwrap() .get_port::>() .unwrap(); @@ -94,75 +100,85 @@ async fn main() { .filter_map( |digit_char| - /* TODO: call morphism + /* TODO: call morphism for each item */ match digit_char.to_digit(16) { - Some(d) => Some(d as usize), + Some(d) => Some(d as u64), None => None } ); - rt_int.write().unwrap().insert_leaf( - vec![ - Context::parse(&ctx, ""), - Context::parse(&ctx, ">"), - Context::parse(&ctx, ""), - Context::parse(&ctx, "") - ].into_iter(), + rt_int.insert_leaf(Context::parse(&ctx, " + + ~ + ~ ℤ_2^64 + ~ machine.UInt64 > + "), nested::repr_tree::ReprLeaf::from_view( digits_view.clone() ) ); - // + //ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ // + + /* convert to little endian + */ ctx.read().unwrap().morphisms.apply_morphism( rt_int.clone(), &Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>"), &Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>") ); + + /* convert to decimal + */ ctx.read().unwrap().morphisms.apply_morphism( rt_int.clone(), &Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>"), &Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>") ); + + /* convert back to big endian + */ ctx.read().unwrap().morphisms.apply_morphism( rt_int.clone(), &Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>"), &Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>") ); - let dec_digits_view = ReprTree::descend(&rt_int, - Context::parse(&ctx, " - - ~< Seq ~ℤ_2^64~machine.UInt64 > - ") - ).expect("cant descend repr tree") - .read().unwrap() - .get_port::>().unwrap() - .map( - /* TODO: call morphism - */ - |digit| { - TerminalAtom::from( - char::from_digit(*digit as u32, 10) - ) - } - ) - .to_grid_horizontal(); + /* map seq of u64 digits to seq of chars + * and add this projection to the ReprTree + */ - let hex_digits_view = - ReprTree::descend( - &rt_int, - Context::parse(&ctx, " - - ~ > - ~ - ~Char>") - ).expect("cant descend") + // + //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV + let dec_digits_view = + rt_int.descend(Context::parse(&ctx, " + < PosInt 10 BigEndian > + ~ < Seq + ~ ℤ_2^64 + ~ machine.UInt64 > + ")).expect("cant descend repr tree") .read().unwrap() - .get_port::>().unwrap() - .to_sequence() - .to_grid_horizontal() - .map_item(|_pt,c| TerminalAtom::new(*c, TerminalStyle::fg_color((30,90,200)))); + .get_port::>().unwrap() + .map(|digit| TerminalAtom::from(char::from_digit(*digit as u32, 10))) + .to_grid_horizontal(); + //ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ + // + //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV + let hex_digits_view = + rt_int.descend(Context::parse(&ctx, " + < PosInt 16 BigEndian > + ~ < Seq + ~ ℤ_2^64 + ~ machine.UInt64 > + ")).expect("cant descend") + .read().unwrap() + .view_seq::< u64 >() + .map(|digit| TerminalAtom::from(char::from_digit(*digit as u32, 16))) + .to_grid_horizontal(); + //ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ + // + + /* setup terminal */ @@ -198,11 +214,19 @@ async fn main() { .display_view() .offset(Vector2::new(3,2))); - comp.push(dec_digits_view.offset(Vector2::new(3,4))); - comp.push(hex_digits_view.offset(Vector2::new(3,5))); + comp.push(nested_tty::make_label("dec: ").offset(Vector2::new(3,4))); + comp.push(dec_digits_view.offset(Vector2::new(8,4)).map_item(|_,a| { + a.add_style_back(TerminalStyle::fg_color((30,90,200))) + })); + + comp.push(nested_tty::make_label("hex: ").offset(Vector2::new(3,5))); + comp.push(hex_digits_view.offset(Vector2::new(8,5)).map_item(|_,a| { + a.add_style_back(TerminalStyle::fg_color((200, 200, 30))) + })); } /* write the changes in the view of `term_port` to the terminal */ app.show().await.expect("output error!"); } + diff --git a/lib-nested-core/src/editors/digit/ctx.rs b/lib-nested-core/src/editors/digit/ctx.rs index d5822c2..10afe9b 100644 --- a/lib-nested-core/src/editors/digit/ctx.rs +++ b/lib-nested-core/src/editors/digit/ctx.rs @@ -98,18 +98,18 @@ pub fn init_ctx( ctx: Arc> ) { _ => 0 }; - if radix <= 256 { + if radix <= 16 { if let Some(src_rt) = rt.descend(Context::parse(&ctx, "Char")) { /* insert projected view into ReprTree */ - let u8_view = + let u64_view = src_rt.view_char() - .map(move |c| c.to_digit(radix).unwrap_or(0) as u8); + .map(move |c| c.to_digit(radix).unwrap_or(0) as u64); - rt.write().unwrap().attach_leaf_to::>( - Context::parse(&ctx, "ℤ_256~machine::UInt8").get_lnf_vec().into_iter(), - u8_view + rt.write().unwrap().attach_leaf_to::>( + Context::parse(&ctx, "ℤ_2^64~machine::UInt64").get_lnf_vec().into_iter(), + u64_view ); } else { eprintln!("could not find required source representation: ~Char", radix); @@ -134,20 +134,20 @@ pub fn init_ctx( ctx: Arc> ) { move |rt: &mut Arc>, σ: &std::collections::HashMap| { /* infer radix from type */ - let radix = + let radix = match σ.get( &laddertypes::TypeID::Var(ctx.read().unwrap().get_var_typeid("Radix").unwrap()) ) { Some(TypeTerm::Num(n)) => (*n) as u32, _ => 0 }; - if radix <= 256 { + if radix <= 16 { /* insert projected view into ReprTree */ let char_view = rt.descend(Context::parse(&ctx, "ℤ_2^64~machine::UInt64")) .unwrap() - .view_usize() - .map(move |digit| char::from_digit((digit%radix as usize) as u32, radix).unwrap_or('?')); + .view_u64() + .map(move |digit| char::from_digit((digit%radix as u64) as u32, radix).unwrap_or('?')); rt.write().unwrap().attach_leaf_to::>( Context::parse(&ctx, "Char").get_lnf_vec().into_iter(), diff --git a/lib-nested-core/src/editors/integer/ctx.rs b/lib-nested-core/src/editors/integer/ctx.rs index 23eeab6..958ec65 100644 --- a/lib-nested-core/src/editors/integer/ctx.rs +++ b/lib-nested-core/src/editors/integer/ctx.rs @@ -39,7 +39,7 @@ pub fn init_ctx(ctx: Arc>) { .apply_substitution(&|k|σ.get(k).cloned()).clone() ).expect("cant descend") .read().unwrap() - .view_seq::< usize >(); + .view_seq::< u64 >(); src_rt.write().unwrap().insert_leaf( vec![ @@ -60,6 +60,8 @@ pub fn init_ctx(ctx: Arc>) { + + let morphism_type = MorphismType { src_type: Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>"), dst_type: Context::parse(&ctx, "ℕ ~ ~ ~ℤ_2^64~machine.UInt64>") @@ -79,7 +81,7 @@ pub fn init_ctx(ctx: Arc>) { .apply_substitution(&|k|σ.get(k).cloned()).clone() ).expect("cant descend") .read().unwrap() - .view_seq::< usize >(); + .view_seq::< u64 >(); src_rt.write().unwrap().insert_leaf( vec![ @@ -113,14 +115,14 @@ pub fn init_ctx(ctx: Arc>) { let src_radix = match σ.get(&laddertypes::TypeID::Var( ctx.read().unwrap().get_var_typeid("SrcRadix").unwrap() )) { - Some(laddertypes::TypeTerm::Num(n)) => *n as usize, + Some(laddertypes::TypeTerm::Num(n)) => *n as u64, _ => 0 }; let dst_radix = match σ.get(&laddertypes::TypeID::Var( ctx.read().unwrap().get_var_typeid("DstRadix").unwrap() )) { - Some(laddertypes::TypeTerm::Num(n)) => *n as usize, + Some(laddertypes::TypeTerm::Num(n)) => *n as u64, _ => 0 }; @@ -134,7 +136,7 @@ pub fn init_ctx(ctx: Arc>) { let dst_digits_port = src_digits_rt.read().unwrap() - .view_seq::() + .view_seq::() .to_positional_uint( src_radix ) .transform_radix( dst_radix ) ; diff --git a/lib-nested-core/src/editors/integer/mod.rs b/lib-nested-core/src/editors/integer/mod.rs index e86a431..b7cd6df 100644 --- a/lib-nested-core/src/editors/integer/mod.rs +++ b/lib-nested-core/src/editors/integer/mod.rs @@ -29,9 +29,9 @@ use { std::sync::{Arc, RwLock} }; -pub trait PositionalUInt : SequenceView { - fn get_radix(&self) -> usize; - fn get_value(&self) -> usize { +pub trait PositionalUInt : SequenceView { + fn get_radix(&self) -> u64; + fn get_value(&self) -> u64 { let mut val = 0; let mut r = 1; for i in 0..self.len().unwrap_or(0) { @@ -44,14 +44,14 @@ pub trait PositionalUInt : SequenceView { } impl PositionalUInt for RwLock { - fn get_radix(&self) -> usize { + fn get_radix(&self) -> u64 { self.read().unwrap().get_radix() } } struct PosUIntFromDigits { - radix: usize, - src_digits: Option>>, + radix: u64, + src_digits: Option>>, cast: Arc>> } @@ -60,9 +60,9 @@ impl View for PosUIntFromDigits { } impl SequenceView for PosUIntFromDigits { - type Item = usize; + type Item = u64; - fn get(&self, idx: &usize) -> Option { + fn get(&self, idx: &usize) -> Option { self.src_digits.get(idx) } @@ -72,13 +72,13 @@ impl SequenceView for PosUIntFromDigits { } impl PositionalUInt for PosUIntFromDigits { - fn get_radix(&self) -> usize { + fn get_radix(&self) -> u64 { self.radix } } -impl Observer> for PosUIntFromDigits { - fn reset(&mut self, new_src: Option>>) { +impl Observer> for PosUIntFromDigits { + fn reset(&mut self, new_src: Option>>) { self.src_digits = new_src; // self.cast.write().unwrap().notify(0); } @@ -91,11 +91,11 @@ impl Observer> for PosUIntFromDigits { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub trait DigitSeqProjection { - fn to_positional_uint(&self, radix: usize) -> OuterViewPort; + fn to_positional_uint(&self, radix: u64) -> OuterViewPort; } -impl DigitSeqProjection for OuterViewPort> { - fn to_positional_uint(&self, radix: usize) -> OuterViewPort { +impl DigitSeqProjection for OuterViewPort> { + fn to_positional_uint(&self, radix: u64) -> OuterViewPort { let port = ViewPort::new(); port.add_update_hook(Arc::new(self.0.clone())); @@ -115,7 +115,7 @@ impl DigitSeqProjection for OuterViewPort> { struct PosUIntToDigits { src: Option>, - cast: Arc>>> + cast: Arc>>> } impl View for PosUIntToDigits { @@ -123,9 +123,9 @@ impl View for PosUIntToDigits { } impl SequenceView for PosUIntToDigits { - type Item = usize; + type Item = u64; - fn get(&self, idx: &usize) -> Option { + fn get(&self, idx: &usize) -> Option { self.src.get(idx) } diff --git a/lib-nested-core/src/editors/integer/radix.rs b/lib-nested-core/src/editors/integer/radix.rs index db3506c..4c5cf31 100644 --- a/lib-nested-core/src/editors/integer/radix.rs +++ b/lib-nested-core/src/editors/integer/radix.rs @@ -21,13 +21,13 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub trait PosIntProjections { - fn transform_radix(&self, dst_radix: usize) -> OuterViewPort>; + fn transform_radix(&self, dst_radix: u64) -> OuterViewPort>; // fn to_digits(&self) -> OuterViewPort>; } impl PosIntProjections for OuterViewPort { - fn transform_radix(&self, dst_radix: usize) -> OuterViewPort> { - let port = ViewPort::>::new(); + fn transform_radix(&self, dst_radix: u64) -> OuterViewPort> { + let port = ViewPort::>::new(); port.add_update_hook(Arc::new(self.0.clone())); // let mut vec_port = ViewPort::new(); @@ -39,7 +39,7 @@ impl PosIntProjections for OuterViewPort { })); self.add_observer(proj.clone()); - port.set_view(Some(proj as Arc>)); + port.set_view(Some(proj as Arc>)); port.into_outer() } /* @@ -61,9 +61,9 @@ impl PosIntProjections for OuterViewPort { pub struct RadixProjection { src: Option>, - dst_radix: usize, - dst_digits: VecBuffer, - cast: Arc>>> + dst_radix: u64, + dst_digits: VecBuffer, + cast: Arc>>> } impl View for RadixProjection { @@ -71,9 +71,9 @@ impl View for RadixProjection { } impl SequenceView for RadixProjection { - type Item = usize; + type Item = u64; - fn get(&self, idx: &usize) -> Option { + fn get(&self, idx: &usize) -> Option { if *idx < self.dst_digits.len() { Some(self.dst_digits.get(*idx)) } else { @@ -87,7 +87,7 @@ impl SequenceView for RadixProjection { } impl PositionalUInt for RadixProjection { - fn get_radix(&self) -> usize { + fn get_radix(&self) -> u64 { self.dst_radix } } diff --git a/lib-nested-core/src/repr_tree/mod.rs b/lib-nested-core/src/repr_tree/mod.rs index a5c47a8..55c74ec 100644 --- a/lib-nested-core/src/repr_tree/mod.rs +++ b/lib-nested-core/src/repr_tree/mod.rs @@ -411,6 +411,7 @@ pub trait ReprTreeExt { fn insert_leaf(&mut self, type_ladder: impl Into, leaf: ReprLeaf); fn insert_branch(&mut self, repr: Arc>); + fn create_branch(&mut self, rung: impl Into); fn descend(&self, target_type: impl Into) -> Option>>; fn view_char(&self) -> OuterViewPort>; @@ -435,6 +436,25 @@ impl ReprTreeExt for Arc> { self.write().unwrap().insert_branch(repr) } + fn create_branch(&mut self, rung: impl Into) { + let lnf = rung.into().get_lnf_vec(); + eprintln!("lnf ={:?}",lnf); + + let mut child = None; + for rung in lnf.iter().rev() { + eprintln!("create {:?}",rung); + let mut parent = ReprTree::new_arc( rung.clone() ); + if let Some(c) = child.take() { + parent.insert_branch( c ); + } + child = Some(parent); + } + + if let Some(child) = child.take() { + self.insert_branch(child); + } + } + fn descend(&self, target_type: impl Into) -> Option>> { ReprTree::descend( self, target_type ) }