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 9fe8fa9e3b
Signed by: senvas
GPG key ID: F96CF119C34B64A6
6 changed files with 158 additions and 112 deletions

View file

@ -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 <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
* (this will add the representation <List <Digit 16>>~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, "<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()
.setup_edittree(
ReprTree::descend(
&rt_int,
Context::parse(&ctx, "<PosInt 16 BigEndian>~<Seq~List <Digit 16>>")
).expect("cant descend reprtree"),
rt_int.descend(Context::parse(&ctx, "
<PosInt 16 BigEndian>
~ <Seq <Digit 16>>
~ <List <Digit 16>>
")).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, "
<PosInt 16 BigEndian>
~<Seq <Digit 16>>
~<List <Digit 16>>
")
).expect("cant descend repr tree"),
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>~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, "<PosInt 16 BigEndian>~<Seq <Digit 16>>~<List <Digit 16>~Char>")
).expect("cant descend")
//
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
let mut chars_view = rt_int.descend(Context::parse(&ctx, "
< PosInt 16 BigEndian >
~ < Seq <Digit 16> >
~ < List <Digit 16>~Char >
")).expect("cant descend")
.read().unwrap()
.get_port::<dyn ListView<char>>()
.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, "<PosInt 16 BigEndian>"),
Context::parse(&ctx, "<Seq <Digit 16>>"),
Context::parse(&ctx, "<Seq _2^64>"),
Context::parse(&ctx, "<Seq machine.UInt64>")
].into_iter(),
rt_int.insert_leaf(Context::parse(&ctx, "
<PosInt 16 BigEndian>
~ <Seq <Digit 16>
~ _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, "~ <PosInt 16 BigEndian> ~ <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(
rt_int.clone(),
&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>")
);
/* convert back to big endian
*/
ctx.read().unwrap().morphisms.apply_morphism(
rt_int.clone(),
&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>")
);
let dec_digits_view = ReprTree::descend(&rt_int,
Context::parse(&ctx, "
<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();
/* 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, "
<PosInt 16 BigEndian>
~<Seq <Digit 16> >
~<List <Digit 16>
~Char>")
).expect("cant descend")
//
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
let dec_digits_view =
rt_int.descend(Context::parse(&ctx, "
< PosInt 10 BigEndian >
~ < Seq <Digit 10>
~ _2^64
~ machine.UInt64 >
")).expect("cant descend repr tree")
.read().unwrap()
.get_port::<dyn ListView<char>>().unwrap()
.to_sequence()
.to_grid_horizontal()
.map_item(|_pt,c| TerminalAtom::new(*c, TerminalStyle::fg_color((30,90,200))));
.get_port::<dyn SequenceView<Item = u64>>().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 <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
*/
@ -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!");
}

View file

@ -98,18 +98,18 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
_ => 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::<dyn SingletonView<Item = u8>>(
Context::parse(&ctx, "_256~machine::UInt8").get_lnf_vec().into_iter(),
u8_view
rt.write().unwrap().attach_leaf_to::<dyn SingletonView<Item = u64>>(
Context::parse(&ctx, "_2^64~machine::UInt64").get_lnf_vec().into_iter(),
u64_view
);
} else {
eprintln!("could not find required source representation: <Digit {}>~Char", radix);
@ -134,20 +134,20 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
move |rt: &mut Arc<RwLock<ReprTree>>, σ: &std::collections::HashMap<laddertypes::TypeID, TypeTerm>| {
/* 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::<dyn SingletonView<Item = char>>(
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()
).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<RwLock<Context>>) {
let morphism_type = MorphismType {
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>")
@ -79,7 +81,7 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
.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<RwLock<Context>>) {
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<RwLock<Context>>) {
let dst_digits_port =
src_digits_rt.read().unwrap()
.view_seq::<usize>()
.view_seq::<u64>()
.to_positional_uint( src_radix )
.transform_radix( dst_radix )
;

View file

@ -29,9 +29,9 @@ use {
std::sync::{Arc, RwLock}
};
pub trait PositionalUInt : SequenceView<Item = usize> {
fn get_radix(&self) -> usize;
fn get_value(&self) -> usize {
pub trait PositionalUInt : SequenceView<Item = u64> {
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<Item = usize> {
}
impl<V: PositionalUInt> PositionalUInt for RwLock<V> {
fn get_radix(&self) -> usize {
fn get_radix(&self) -> u64 {
self.read().unwrap().get_radix()
}
}
struct PosUIntFromDigits {
radix: usize,
src_digits: Option<Arc<dyn SequenceView<Item = usize>>>,
radix: u64,
src_digits: Option<Arc<dyn SequenceView<Item = u64>>>,
cast: Arc<RwLock<ObserverBroadcast<dyn PositionalUInt>>>
}
@ -60,9 +60,9 @@ impl View 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)
}
@ -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<dyn SequenceView<Item = usize>> for PosUIntFromDigits {
fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = usize>>>) {
impl Observer<dyn SequenceView<Item = u64>> for PosUIntFromDigits {
fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = u64>>>) {
self.src_digits = new_src;
// self.cast.write().unwrap().notify(0);
}
@ -91,11 +91,11 @@ impl Observer<dyn SequenceView<Item = usize>> for PosUIntFromDigits {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
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>> {
fn to_positional_uint(&self, radix: usize) -> OuterViewPort<dyn PositionalUInt> {
impl DigitSeqProjection for OuterViewPort<dyn SequenceView<Item = u64>> {
fn to_positional_uint(&self, radix: u64) -> OuterViewPort<dyn PositionalUInt> {
let port = ViewPort::new();
port.add_update_hook(Arc::new(self.0.clone()));
@ -115,7 +115,7 @@ impl DigitSeqProjection for OuterViewPort<dyn SequenceView<Item = usize>> {
struct PosUIntToDigits {
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 {
@ -123,9 +123,9 @@ impl View 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)
}

View file

@ -21,13 +21,13 @@ use {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
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>>;
}
impl PosIntProjections for OuterViewPort<dyn PositionalUInt> {
fn transform_radix(&self, dst_radix: usize) -> OuterViewPort<dyn SequenceView<Item = usize>> {
let port = ViewPort::<dyn SequenceView<Item = usize>>::new();
fn transform_radix(&self, dst_radix: u64) -> OuterViewPort<dyn SequenceView<Item = u64>> {
let port = ViewPort::<dyn SequenceView<Item = u64>>::new();
port.add_update_hook(Arc::new(self.0.clone()));
// let mut vec_port = ViewPort::new();
@ -39,7 +39,7 @@ impl PosIntProjections for OuterViewPort<dyn PositionalUInt> {
}));
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()
}
/*
@ -61,9 +61,9 @@ impl PosIntProjections for OuterViewPort<dyn PositionalUInt> {
pub struct RadixProjection {
src: Option<Arc<dyn PositionalUInt>>,
dst_radix: usize,
dst_digits: VecBuffer<usize>,
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = usize>>>>
dst_radix: u64,
dst_digits: VecBuffer<u64>,
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = u64>>>>
}
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<usize> {
fn get(&self, idx: &usize) -> Option<u64> {
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
}
}

View file

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