clean up posint example & use u64 instead of usize in PositionalUInt trait

This commit is contained in:
Michael Sippel 2024-05-25 19:21:16 +02:00
parent 40a5da4512
commit fdecc29e80
Signed by: senvas
GPG key ID: F96CF119C34B64A6
7 changed files with 184 additions and 138 deletions

View file

@ -43,48 +43,54 @@ async fn main() {
nested::editors::list::init_ctx( ctx.clone() ); nested::editors::list::init_ctx( ctx.clone() );
nested_tty::setup_edittree_hook(&ctx); nested_tty::setup_edittree_hook(&ctx);
/* Create a Representation-Tree of type <List <Digit 16>>
*/
let mut rt_digitlist = ReprTree::new_arc( Context::parse(&ctx, "<List <Digit 16>>") );
let mut rt_digitseq = ReprTree::new_arc( Context::parse(&ctx, "<Seq <Digit 16>>") );
rt_digitseq.insert_branch( rt_digitlist );
let mut rt_posint = ReprTree::new_arc(Context::parse(&ctx, "<PosInt 16 BigEndian>"));
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 /* Create a Representation-Tree of type ``
* (this will add the representation <List <Digit 16>>~EditTree to the ReprTree) */
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, "<PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16>>")
);
/* Setup an Editor for the big-endian hexadecimal representation
* (this will add the representation `<List <Digit 16>>~EditTree` to the ReprTree)
*/ */
let rt_edittree_list = ctx.read().unwrap() let rt_edittree_list = ctx.read().unwrap()
.setup_edittree( .setup_edittree(
ReprTree::descend( rt_int.descend(Context::parse(&ctx, "
&rt_int,
Context::parse(&ctx, "<PosInt 16 BigEndian>~<Seq~List <Digit 16>>")
).expect("cant descend reprtree"),
SingletonBuffer::new(0).get_port()
);
ctx.read().unwrap().morphisms.apply_morphism(
ReprTree::descend(&rt_int,
Context::parse(&ctx, "
<PosInt 16 BigEndian> <PosInt 16 BigEndian>
~ <Seq <Digit 16>> ~ <Seq <Digit 16>>
~ <List <Digit 16>> ~ <List <Digit 16>>
") ")).expect("cant descend reprtree"),
).expect("cant descend repr tree"), SingletonBuffer::new(0).get_port()
);
/* Setup a morphism to extract Char values from the list-editor
*/
ctx.read().unwrap().morphisms.apply_morphism(
rt_int.descend(Context::parse(&ctx, "
<PosInt 16 BigEndian>
~ <Seq <Digit 16>>
~ <List <Digit 16>>
")).expect("cant descend reprtree"),
&Context::parse(&ctx, "<List <Digit 16>>~EditTree"), &Context::parse(&ctx, "<List <Digit 16>>~EditTree"),
&Context::parse(&ctx, "<List <Digit 16>~Char>") &Context::parse(&ctx, "<List <Digit 16>~Char>")
); );
/* /*
* map seq of chars to seq of u64 digits * map seq of chars to seq of u64 digits
* and add this projection to the ReprTree
*/ */
let mut chars_view =
ReprTree::descend( //
&rt_int, //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
Context::parse(&ctx, "<PosInt 16 BigEndian>~<Seq <Digit 16>>~<List <Digit 16>~Char>") let mut chars_view = rt_int.descend(Context::parse(&ctx, "
).expect("cant descend") < PosInt 16 BigEndian >
~ < Seq <Digit 16> >
~ < List <Digit 16>~Char >
")).expect("cant descend")
.read().unwrap() .read().unwrap()
.get_port::<dyn ListView<char>>() .get_port::<dyn ListView<char>>()
.unwrap(); .unwrap();
@ -94,75 +100,85 @@ async fn main() {
.filter_map( .filter_map(
|digit_char| |digit_char|
/* TODO: call morphism /* TODO: call morphism for each item
*/ */
match digit_char.to_digit(16) { match digit_char.to_digit(16) {
Some(d) => Some(d as usize), Some(d) => Some(d as u64),
None => None None => None
} }
); );
rt_int.write().unwrap().insert_leaf( rt_int.insert_leaf(Context::parse(&ctx, "
vec![ <PosInt 16 BigEndian>
Context::parse(&ctx, "<PosInt 16 BigEndian>"), ~ <Seq <Digit 16>
Context::parse(&ctx, "<Seq <Digit 16>>"), ~ _2^64
Context::parse(&ctx, "<Seq _2^64>"), ~ machine.UInt64 >
Context::parse(&ctx, "<Seq machine.UInt64>") "),
].into_iter(),
nested::repr_tree::ReprLeaf::from_view( digits_view.clone() ) nested::repr_tree::ReprLeaf::from_view( digits_view.clone() )
); );
// //ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ
// //
/* convert to little endian
*/
ctx.read().unwrap().morphisms.apply_morphism( ctx.read().unwrap().morphisms.apply_morphism(
rt_int.clone(), rt_int.clone(),
&Context::parse(&ctx, "~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>~_2^64~machine.UInt64>"), &Context::parse(&ctx, "~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>~_2^64~machine.UInt64>"),
&Context::parse(&ctx, " ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>~_2^64~machine.UInt64>") &Context::parse(&ctx, " ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>~_2^64~machine.UInt64>")
); );
/* convert to decimal
*/
ctx.read().unwrap().morphisms.apply_morphism( ctx.read().unwrap().morphisms.apply_morphism(
rt_int.clone(), rt_int.clone(),
&Context::parse(&ctx, "~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>~_2^64~machine.UInt64>"), &Context::parse(&ctx, "~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>~_2^64~machine.UInt64>"),
&Context::parse(&ctx, " ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>~_2^64~machine.UInt64>") &Context::parse(&ctx, " ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>~_2^64~machine.UInt64>")
); );
/* convert back to big endian
*/
ctx.read().unwrap().morphisms.apply_morphism( ctx.read().unwrap().morphisms.apply_morphism(
rt_int.clone(), rt_int.clone(),
&Context::parse(&ctx, "~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>~_2^64~machine.UInt64>"), &Context::parse(&ctx, "~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>~_2^64~machine.UInt64>"),
&Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>~_2^64~machine.UInt64>") &Context::parse(&ctx, " ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>~_2^64~machine.UInt64>")
); );
let dec_digits_view = ReprTree::descend(&rt_int, /* map seq of u64 digits to seq of chars
Context::parse(&ctx, " * and add this projection to the ReprTree
<PosInt 10 BigEndian>
~< Seq <Digit 10>~_2^64~machine.UInt64 >
")
).expect("cant descend repr tree")
.read().unwrap()
.get_port::<dyn SequenceView<Item = usize>>().unwrap()
.map(
/* TODO: call morphism
*/ */
|digit| {
TerminalAtom::from(
char::from_digit(*digit as u32, 10)
)
}
)
.to_grid_horizontal();
let hex_digits_view = //
ReprTree::descend( //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
&rt_int, let dec_digits_view =
Context::parse(&ctx, " rt_int.descend(Context::parse(&ctx, "
<PosInt 16 BigEndian> < PosInt 10 BigEndian >
~<Seq <Digit 16> > ~ < Seq <Digit 10>
~<List <Digit 16> ~ _2^64
~Char>") ~ machine.UInt64 >
).expect("cant descend") ")).expect("cant descend repr tree")
.read().unwrap() .read().unwrap()
.get_port::<dyn ListView<char>>().unwrap() .get_port::<dyn SequenceView<Item = u64>>().unwrap()
.to_sequence() .map(|digit| TerminalAtom::from(char::from_digit(*digit as u32, 10)))
.to_grid_horizontal() .to_grid_horizontal();
.map_item(|_pt,c| TerminalAtom::new(*c, TerminalStyle::fg_color((30,90,200)))); //ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ
//
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
let hex_digits_view =
rt_int.descend(Context::parse(&ctx, "
< PosInt 16 BigEndian >
~ < Seq <Digit 16>
~ _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 /* setup terminal
*/ */
@ -198,11 +214,19 @@ async fn main() {
.display_view() .display_view()
.offset(Vector2::new(3,2))); .offset(Vector2::new(3,2)));
comp.push(dec_digits_view.offset(Vector2::new(3,4))); comp.push(nested_tty::make_label("dec: ").offset(Vector2::new(3,4)));
comp.push(hex_digits_view.offset(Vector2::new(3,5))); 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 /* write the changes in the view of `term_port` to the terminal
*/ */
app.show().await.expect("output error!"); app.show().await.expect("output error!");
} }

View file

@ -98,18 +98,18 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
_ => 0 _ => 0
}; };
if radix <= 256 { if radix <= 16 {
if let Some(src_rt) = rt.descend(Context::parse(&ctx, "Char")) { if let Some(src_rt) = rt.descend(Context::parse(&ctx, "Char")) {
/* insert projected view into ReprTree /* insert projected view into ReprTree
*/ */
let u8_view = let u64_view =
src_rt.view_char() 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::<dyn SingletonView<Item = u8>>( rt.write().unwrap().attach_leaf_to::<dyn SingletonView<Item = u64>>(
Context::parse(&ctx, "_256~machine::UInt8").get_lnf_vec().into_iter(), Context::parse(&ctx, "_2^64~machine.UInt64").get_lnf_vec().into_iter(),
u8_view u64_view
); );
} else { } else {
eprintln!("could not find required source representation: <Digit {}>~Char", radix); eprintln!("could not find required source representation: <Digit {}>~Char", radix);
@ -140,14 +140,14 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
_ => 0 _ => 0
}; };
if radix <= 256 { if radix <= 16 {
/* insert projected view into ReprTree /* insert projected view into ReprTree
*/ */
let char_view = let char_view =
rt.descend(Context::parse(&ctx, "_2^64~machine::UInt64")) rt.descend(Context::parse(&ctx, "_2^64~machine.UInt64"))
.unwrap() .unwrap()
.view_usize() .view_u64()
.map(move |digit| char::from_digit((digit%radix as usize) as u32, radix).unwrap_or('?')); .map(move |digit| char::from_digit((digit%radix as u64) as u32, radix).unwrap_or('?'));
rt.write().unwrap().attach_leaf_to::<dyn SingletonView<Item = char>>( rt.write().unwrap().attach_leaf_to::<dyn SingletonView<Item = char>>(
Context::parse(&ctx, "Char").get_lnf_vec().into_iter(), Context::parse(&ctx, "Char").get_lnf_vec().into_iter(),

View file

@ -39,7 +39,7 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
.apply_substitution(&|k|σ.get(k).cloned()).clone() .apply_substitution(&|k|σ.get(k).cloned()).clone()
).expect("cant descend") ).expect("cant descend")
.read().unwrap() .read().unwrap()
.view_seq::< usize >(); .view_seq::< u64 >();
src_rt.write().unwrap().insert_leaf( src_rt.write().unwrap().insert_leaf(
vec![ vec![
@ -60,6 +60,8 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
let morphism_type = MorphismType { let morphism_type = MorphismType {
src_type: Context::parse(&ctx, " ~ <PosInt Radix LittleEndian> ~ <Seq <Digit SrcRadix>~_2^64~machine.UInt64>"), src_type: Context::parse(&ctx, " ~ <PosInt Radix LittleEndian> ~ <Seq <Digit SrcRadix>~_2^64~machine.UInt64>"),
dst_type: Context::parse(&ctx, " ~ <PosInt Radix BigEndian> ~ <Seq <Digit DstRadix>~_2^64~machine.UInt64>") dst_type: Context::parse(&ctx, " ~ <PosInt Radix BigEndian> ~ <Seq <Digit DstRadix>~_2^64~machine.UInt64>")
@ -79,7 +81,7 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
.apply_substitution(&|k|σ.get(k).cloned()).clone() .apply_substitution(&|k|σ.get(k).cloned()).clone()
).expect("cant descend") ).expect("cant descend")
.read().unwrap() .read().unwrap()
.view_seq::< usize >(); .view_seq::< u64 >();
src_rt.write().unwrap().insert_leaf( src_rt.write().unwrap().insert_leaf(
vec![ vec![
@ -113,14 +115,14 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
let src_radix = match σ.get(&laddertypes::TypeID::Var( let src_radix = match σ.get(&laddertypes::TypeID::Var(
ctx.read().unwrap().get_var_typeid("SrcRadix").unwrap() 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 _ => 0
}; };
let dst_radix = match σ.get(&laddertypes::TypeID::Var( let dst_radix = match σ.get(&laddertypes::TypeID::Var(
ctx.read().unwrap().get_var_typeid("DstRadix").unwrap() 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 _ => 0
}; };
@ -134,7 +136,7 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
let dst_digits_port = let dst_digits_port =
src_digits_rt.read().unwrap() src_digits_rt.read().unwrap()
.view_seq::<usize>() .view_seq::<u64>()
.to_positional_uint( src_radix ) .to_positional_uint( src_radix )
.transform_radix( dst_radix ) .transform_radix( dst_radix )
; ;

View file

@ -29,9 +29,9 @@ use {
std::sync::{Arc, RwLock} std::sync::{Arc, RwLock}
}; };
pub trait PositionalUInt : SequenceView<Item = usize> { pub trait PositionalUInt : SequenceView<Item = u64> {
fn get_radix(&self) -> usize; fn get_radix(&self) -> u64;
fn get_value(&self) -> usize { fn get_value(&self) -> u64 {
let mut val = 0; let mut val = 0;
let mut r = 1; let mut r = 1;
for i in 0..self.len().unwrap_or(0) { for i in 0..self.len().unwrap_or(0) {
@ -44,14 +44,14 @@ pub trait PositionalUInt : SequenceView<Item = usize> {
} }
impl<V: PositionalUInt> PositionalUInt for RwLock<V> { impl<V: PositionalUInt> PositionalUInt for RwLock<V> {
fn get_radix(&self) -> usize { fn get_radix(&self) -> u64 {
self.read().unwrap().get_radix() self.read().unwrap().get_radix()
} }
} }
struct PosUIntFromDigits { struct PosUIntFromDigits {
radix: usize, radix: u64,
src_digits: Option<Arc<dyn SequenceView<Item = usize>>>, src_digits: Option<Arc<dyn SequenceView<Item = u64>>>,
cast: Arc<RwLock<ObserverBroadcast<dyn PositionalUInt>>> cast: Arc<RwLock<ObserverBroadcast<dyn PositionalUInt>>>
} }
@ -60,9 +60,9 @@ impl View for PosUIntFromDigits {
} }
impl SequenceView for PosUIntFromDigits { impl SequenceView for PosUIntFromDigits {
type Item = usize; type Item = u64;
fn get(&self, idx: &usize) -> Option<usize> { fn get(&self, idx: &usize) -> Option<u64> {
self.src_digits.get(idx) self.src_digits.get(idx)
} }
@ -72,13 +72,13 @@ impl SequenceView for PosUIntFromDigits {
} }
impl PositionalUInt for PosUIntFromDigits { impl PositionalUInt for PosUIntFromDigits {
fn get_radix(&self) -> usize { fn get_radix(&self) -> u64 {
self.radix self.radix
} }
} }
impl Observer<dyn SequenceView<Item = usize>> for PosUIntFromDigits { impl Observer<dyn SequenceView<Item = u64>> for PosUIntFromDigits {
fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = usize>>>) { fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = u64>>>) {
self.src_digits = new_src; self.src_digits = new_src;
// self.cast.write().unwrap().notify(0); // self.cast.write().unwrap().notify(0);
} }
@ -91,11 +91,11 @@ impl Observer<dyn SequenceView<Item = usize>> for PosUIntFromDigits {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait DigitSeqProjection { pub trait DigitSeqProjection {
fn to_positional_uint(&self, radix: usize) -> OuterViewPort<dyn PositionalUInt>; fn to_positional_uint(&self, radix: u64) -> OuterViewPort<dyn PositionalUInt>;
} }
impl DigitSeqProjection for OuterViewPort<dyn SequenceView<Item = usize>> { impl DigitSeqProjection for OuterViewPort<dyn SequenceView<Item = u64>> {
fn to_positional_uint(&self, radix: usize) -> OuterViewPort<dyn PositionalUInt> { fn to_positional_uint(&self, radix: u64) -> OuterViewPort<dyn PositionalUInt> {
let port = ViewPort::new(); let port = ViewPort::new();
port.add_update_hook(Arc::new(self.0.clone())); port.add_update_hook(Arc::new(self.0.clone()));
@ -115,7 +115,7 @@ impl DigitSeqProjection for OuterViewPort<dyn SequenceView<Item = usize>> {
struct PosUIntToDigits { struct PosUIntToDigits {
src: Option<Arc<dyn PositionalUInt>>, src: Option<Arc<dyn PositionalUInt>>,
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = usize>>>> cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = u64>>>>
} }
impl View for PosUIntToDigits { impl View for PosUIntToDigits {
@ -123,9 +123,9 @@ impl View for PosUIntToDigits {
} }
impl SequenceView for PosUIntToDigits { impl SequenceView for PosUIntToDigits {
type Item = usize; type Item = u64;
fn get(&self, idx: &usize) -> Option<usize> { fn get(&self, idx: &usize) -> Option<u64> {
self.src.get(idx) self.src.get(idx)
} }

View file

@ -21,13 +21,13 @@ use {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait PosIntProjections { pub trait PosIntProjections {
fn transform_radix(&self, dst_radix: usize) -> OuterViewPort<dyn SequenceView<Item = usize>>; fn transform_radix(&self, dst_radix: u64) -> OuterViewPort<dyn SequenceView<Item = u64>>;
// fn to_digits(&self) -> OuterViewPort<dyn SequenceView<Item = usize>>; // fn to_digits(&self) -> OuterViewPort<dyn SequenceView<Item = usize>>;
} }
impl PosIntProjections for OuterViewPort<dyn PositionalUInt> { impl PosIntProjections for OuterViewPort<dyn PositionalUInt> {
fn transform_radix(&self, dst_radix: usize) -> OuterViewPort<dyn SequenceView<Item = usize>> { fn transform_radix(&self, dst_radix: u64) -> OuterViewPort<dyn SequenceView<Item = u64>> {
let port = ViewPort::<dyn SequenceView<Item = usize>>::new(); let port = ViewPort::<dyn SequenceView<Item = u64>>::new();
port.add_update_hook(Arc::new(self.0.clone())); port.add_update_hook(Arc::new(self.0.clone()));
// let mut vec_port = ViewPort::new(); // let mut vec_port = ViewPort::new();
@ -39,7 +39,7 @@ impl PosIntProjections for OuterViewPort<dyn PositionalUInt> {
})); }));
self.add_observer(proj.clone()); self.add_observer(proj.clone());
port.set_view(Some(proj as Arc<dyn SequenceView<Item = usize>>)); port.set_view(Some(proj as Arc<dyn SequenceView<Item = u64>>));
port.into_outer() port.into_outer()
} }
/* /*
@ -61,9 +61,9 @@ impl PosIntProjections for OuterViewPort<dyn PositionalUInt> {
pub struct RadixProjection { pub struct RadixProjection {
src: Option<Arc<dyn PositionalUInt>>, src: Option<Arc<dyn PositionalUInt>>,
dst_radix: usize, dst_radix: u64,
dst_digits: VecBuffer<usize>, dst_digits: VecBuffer<u64>,
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = usize>>>> cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = u64>>>>
} }
impl View for RadixProjection { impl View for RadixProjection {
@ -71,9 +71,9 @@ impl View for RadixProjection {
} }
impl SequenceView for RadixProjection { impl SequenceView for RadixProjection {
type Item = usize; type Item = u64;
fn get(&self, idx: &usize) -> Option<usize> { fn get(&self, idx: &usize) -> Option<u64> {
if *idx < self.dst_digits.len() { if *idx < self.dst_digits.len() {
Some(self.dst_digits.get(*idx)) Some(self.dst_digits.get(*idx))
} else { } else {
@ -87,7 +87,7 @@ impl SequenceView for RadixProjection {
} }
impl PositionalUInt for RadixProjection { impl PositionalUInt for RadixProjection {
fn get_radix(&self) -> usize { fn get_radix(&self) -> u64 {
self.dst_radix self.dst_radix
} }
} }

View file

@ -411,6 +411,7 @@ pub trait ReprTreeExt {
fn insert_leaf(&mut self, type_ladder: impl Into<TypeTerm>, leaf: ReprLeaf); fn insert_leaf(&mut self, type_ladder: impl Into<TypeTerm>, leaf: ReprLeaf);
fn insert_branch(&mut self, repr: Arc<RwLock<ReprTree>>); fn insert_branch(&mut self, repr: Arc<RwLock<ReprTree>>);
fn create_branch(&mut self, rung: impl Into<TypeTerm>);
fn descend(&self, target_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>>; fn descend(&self, target_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>>;
fn view_char(&self) -> OuterViewPort<dyn SingletonView<Item = char>>; fn view_char(&self) -> OuterViewPort<dyn SingletonView<Item = char>>;
@ -435,6 +436,25 @@ impl ReprTreeExt for Arc<RwLock<ReprTree>> {
self.write().unwrap().insert_branch(repr) self.write().unwrap().insert_branch(repr)
} }
fn create_branch(&mut self, rung: impl Into<TypeTerm>) {
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<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> { fn descend(&self, target_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
ReprTree::descend( self, target_type ) ReprTree::descend( self, target_type )
} }

View file

@ -45,7 +45,7 @@ fn char_view() {
} }
#[test] #[test]
fn digit_projection_char_to_u8() { fn digit_projection_char_to_u64() {
let ctx = Arc::new(RwLock::new(Context::new())); let ctx = Arc::new(RwLock::new(Context::new()));
crate::editors::digit::init_ctx( ctx.clone() ); crate::editors::digit::init_ctx( ctx.clone() );
@ -62,14 +62,14 @@ fn digit_projection_char_to_u8() {
ctx.read().unwrap().morphisms.apply_morphism( ctx.read().unwrap().morphisms.apply_morphism(
rt_digit.clone(), rt_digit.clone(),
&Context::parse(&ctx, "<Digit 16>~Char"), &Context::parse(&ctx, "<Digit 16>~Char"),
&Context::parse(&ctx, "<Digit 16>~_256~machine::UInt8") &Context::parse(&ctx, "<Digit 16>~_2^64~machine.UInt64")
); );
let digit_u8_view = rt_digit let digit_u64_view = rt_digit
.descend(Context::parse(&ctx, "_256~machine::UInt8")).unwrap() .descend(Context::parse(&ctx, "_2^64~machine.UInt64")).unwrap()
.view_u8(); .view_u64();
assert_eq!( digit_u8_view.get_view().unwrap().get(), 5 as u8 ); assert_eq!( digit_u64_view.get_view().unwrap().get(), 5 as u64 );
// projection behaves accordingly , when buffer is changed // projection behaves accordingly , when buffer is changed
@ -79,19 +79,19 @@ fn digit_projection_char_to_u8() {
.singleton_buffer::<char>(); .singleton_buffer::<char>();
digit_char_buffer.set('2'); digit_char_buffer.set('2');
assert_eq!( digit_u8_view.get_view().unwrap().get(), 2 as u8 ); assert_eq!( digit_u64_view.get_view().unwrap().get(), 2 as u64 );
} }
#[test] #[test]
fn digit_projection_u8_to_char() { fn digit_projection_u64_to_char() {
let ctx = Arc::new(RwLock::new(Context::new())); let ctx = Arc::new(RwLock::new(Context::new()));
crate::editors::digit::init_ctx( ctx.clone() ); crate::editors::digit::init_ctx( ctx.clone() );
let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") ); let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") );
rt_digit.insert_leaf( rt_digit.insert_leaf(
Context::parse(&ctx, "_256~machine::UInt8"), Context::parse(&ctx, "_2^64~machine.UInt64"),
ReprLeaf::from_singleton_buffer( SingletonBuffer::new(5 as u8) ) ReprLeaf::from_singleton_buffer( SingletonBuffer::new(5 as u64) )
); );
//<><><><> //<><><><>
@ -99,15 +99,15 @@ fn digit_projection_u8_to_char() {
ctx.read().unwrap().morphisms.apply_morphism( ctx.read().unwrap().morphisms.apply_morphism(
rt_digit.clone(), rt_digit.clone(),
&Context::parse(&ctx, "<Digit 16>~_256~machine::UInt8"), &Context::parse(&ctx, "<Digit 16>~_2^64~machine.UInt64"),
&Context::parse(&ctx, "<Digit 16>~Char") &Context::parse(&ctx, "<Digit 16>~Char")
); );
let digit_u8_view = rt_digit let digit_u64_view = rt_digit
.descend(Context::parse(&ctx, "Char")).unwrap() .descend(Context::parse(&ctx, "Char")).unwrap()
.view_char(); .view_char();
assert_eq!( digit_u8_view.get_view().unwrap().get(), '5' ); assert_eq!( digit_u64_view.get_view().unwrap().get(), '5' );
} }
@ -119,15 +119,15 @@ fn char_buffered_projection() {
let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") ); let mut rt_digit = ReprTree::new_arc( Context::parse(&ctx, "<Digit 16>") );
rt_digit.insert_leaf( rt_digit.insert_leaf(
Context::parse(&ctx, "_256~machine::UInt8"), Context::parse(&ctx, "_2^64~machine.UInt64"),
ReprLeaf::from_singleton_buffer( SingletonBuffer::new(8 as u8) ) ReprLeaf::from_singleton_buffer( SingletonBuffer::new(8 as u64) )
); );
let mut digit_u8_buffer = rt_digit let mut digit_u64_buffer = rt_digit
.descend(Context::parse(&ctx, "_256~machine::UInt8")).unwrap() .descend(Context::parse(&ctx, "_2^64~machine.UInt64")).unwrap()
.singleton_buffer::<u8>(); .singleton_buffer::<u64>();
assert_eq!( digit_u8_buffer.get(), 8 ); assert_eq!( digit_u64_buffer.get(), 8 );
rt_digit.insert_leaf( rt_digit.insert_leaf(
Context::parse(&ctx, "Char"), Context::parse(&ctx, "Char"),
@ -145,20 +145,20 @@ fn char_buffered_projection() {
assert_eq!( digit_char_buf.get(), '5' ); assert_eq!( digit_char_buf.get(), '5' );
assert_eq!( digit_char_view.get_view().unwrap().get(), '5' ); assert_eq!( digit_char_view.get_view().unwrap().get(), '5' );
// now we attach the char-repr to the u8-repr // now we attach the char-repr to the u64-repr
ctx.read().unwrap().morphisms.apply_morphism( ctx.read().unwrap().morphisms.apply_morphism(
rt_digit.clone(), rt_digit.clone(),
&Context::parse(&ctx, "<Digit 16>~_256~machine::UInt8"), &Context::parse(&ctx, "<Digit 16>~_2^64~machine.UInt64"),
&Context::parse(&ctx, "<Digit 16>~Char") &Context::parse(&ctx, "<Digit 16>~Char")
); );
// char buffer and view should now follow the u8-buffer // char buffer and view should now follow the u64-buffer
assert_eq!( digit_char_view.get_view().unwrap().get(), '8' ); assert_eq!( digit_char_view.get_view().unwrap().get(), '8' );
assert_eq!( digit_char_buf.get(), '8' ); assert_eq!( digit_char_buf.get(), '8' );
// now u8-buffer changes, and char-buffer should change accordingly // now u64-buffer changes, and char-buffer should change accordingly
digit_u8_buffer.set(3); digit_u64_buffer.set(3);
assert_eq!( digit_u8_buffer.get(), 3 ); assert_eq!( digit_u64_buffer.get(), 3 );
// char buffer should follow // char buffer should follow
digit_char_view.0.update(); digit_char_view.0.update();