example: two editors with different radices

This commit is contained in:
Michael Sippel 2024-08-02 22:43:00 +02:00
parent a3c701ce88
commit 830ce613ea
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 140 additions and 111 deletions

View file

@ -47,24 +47,101 @@ fn rebuild_projections(
}
}
fn setup_le_master(ctx: &Arc<RwLock<Context>>, rt_int: &Arc<RwLock<ReprTree>>) {
fn setup_hex_master(ctx: &Arc<RwLock<Context>>, rt_int: &Arc<RwLock<ReprTree>>) {
rebuild_projections(
ctx.clone(),
rt_int.clone(),
vec![
// Little Endian Editor
// extract values from hex-editor
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
),
// Convert Endianness
// convert to little-endian
(
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
),
// convert digit representation from char to u64
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>"
),
// convert radix to decimal
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>",
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ _2^64 ~ machine.UInt64>"
),
// convert decimal digit representation back to char
(
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ _2^64 ~ machine.UInt64>",
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>"
),
// convert to big-endian
(
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>",
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>"
),
// decimal editor
(
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>",
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char ~ EditTree>"
),
(
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char ~ EditTree>",
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char ~ EditTree> ~ <Vec EditTree>"
),
(
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char ~ EditTree> ~ <Vec EditTree>",
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char> ~ EditTree"
),
].into_iter()
.map(|(s,d)| (Context::parse(&ctx, s), Context::parse(&ctx, d)))
.collect()
);
}
fn setup_dec_master(ctx: &Arc<RwLock<Context>>, rt_int: &Arc<RwLock<ReprTree>>) {
rebuild_projections(
ctx.clone(),
rt_int.clone(),
vec![
// extract values from decimal-editor
(
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char> ~ EditTree",
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>"
),
// convert to little-endian
(
" ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>",
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>"
),
// convert digit representation to u64
(
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ Char>",
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ _2^64 ~ machine.UInt64>"
),
// convert radix to decimal
(
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ _2^64 ~ machine.UInt64>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>"
),
// convert back digit representation char
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
),
// convert back to big-endian
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
),
// Big Endian Editor
// hex editor
(
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree>"
@ -77,65 +154,6 @@ fn setup_le_master(ctx: &Arc<RwLock<Context>>, rt_int: &Arc<RwLock<ReprTree>>) {
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree> ~ <Vec EditTree>",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree"
),
// convert to different digit-representation
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>"
),
// Radix Convert
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>",
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ _2^64 ~ machine.UInt64>"
),
].into_iter()
.map(|(s,d)| (Context::parse(&ctx, s), Context::parse(&ctx, d)))
.collect()
);
}
fn setup_be_master(ctx: &Arc<RwLock<Context>>, rt_int: &Arc<RwLock<ReprTree>>) {
rebuild_projections(
ctx.clone(),
rt_int.clone(),
vec![
// Big Endian Editor
(
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
),
// Convert Endianness
(
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
),
// Little Endian Editor
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree>"
),
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree> ~ <Vec EditTree>"
),
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree> ~ <Vec EditTree>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree"
),
// convert to different digit-representation
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>"
),
// Radix Convert
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ _2^64 ~ machine.UInt64>",
" ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>> ~ <List <Digit 10> ~ _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, "<PosInt 16 BigEndian>~<Seq <Digit 16>>~<List <Digit 16>>~<List Char>~<Vec Char>"),
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, "<PosInt 16 LittleEndian>~<Seq <Digit 16>>~<List <Digit 16>>~<List Char>~<Vec Char>"),
nested::repr_tree::ReprLeaf::from_vec_buffer( digits_le.clone() )
Context::parse(&ctx, "<PosInt 10 BigEndian>~<Seq <Digit 10>>~<List <Digit 10>>~<List Char>~<Vec Char>"),
nested::repr_tree::ReprLeaf::from_vec_buffer( digits_dec.clone() )
);
let mut digits_le_editvec = VecBuffer::<Arc<RwLock<EditTree>>>::new();
rt_int.insert_leaf(
Context::parse(&ctx, "
<PosInt 16 LittleEndian>
~ <Seq <Digit 16>>
~ <List <Digit 16>
~ Char
~ EditTree>
~ <Vec EditTree>
"),
nested::repr_tree::ReprLeaf::from_vec_buffer( digits_le_editvec.clone() )
);
let mut digits_be_editvec = VecBuffer::<Arc<RwLock<EditTree>>>::new();
let mut digits_hex_editvec = VecBuffer::<Arc<RwLock<EditTree>>>::new();
rt_int.insert_leaf(
Context::parse(&ctx, "
<PosInt 16 BigEndian>
@ -194,7 +199,20 @@ async fn main() {
~ EditTree>
~ <Vec 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::<Arc<RwLock<EditTree>>>::new();
rt_int.insert_leaf(
Context::parse(&ctx, "
<PosInt 10 BigEndian>
~ <Seq <Digit 10>>
~ <List <Digit 10>
~ Char
~ EditTree>
~ <Vec 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![
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <Vec Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <Vec Char>",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>"
),
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree>"
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char>",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char ~ EditTree>"
),
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <List EditTree>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <List EditTree> ~ <Vec EditTree>"
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <List EditTree>",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <List EditTree> ~ <Vec EditTree>"
),
(
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <List EditTree> ~ <Vec EditTree>",
" ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ EditTree"
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ Char> ~ <List EditTree> ~ <Vec EditTree>",
" ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16> ~ 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,"
<PosInt 16 LittleEndian>
~ <Seq <Digit 16>>
~ <List <Digit 16>>
~ <List Char>
")).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,"
<PosInt 10 BigEndian>
~ <Seq <Digit 10>>
~ <List <Digit 10>>
~ <List Char>
")).expect("descend"),
SingletonBuffer::new(0).get_port()
).unwrap().get();
let hex_digits_view = rt_int.descend(Context::parse(&ctx, "
<PosInt 16 LittleEndian>
@ -280,17 +298,17 @@ async fn main() {
*/
let mut list_editor = nested::editors::list::ListEditor::new(ctx.clone(), Context::parse(&ctx, "<Seq Char>"));
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, "<PosInt 16 BigEndian> ~ <Seq~List <Digit 16>~Char>")).expect(""), 1);
show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, "<PosInt 16 LittleEndian> ~ <Seq <Digit 16>> ~ <List <Digit 16>~Char>")).expect(""), 4);
show_edit_tree(&ctx, &mut comp, &rt_int.descend(Context::parse(&ctx, "<PosInt 10 BigEndian> ~ <Seq~List <Digit 10>~Char>")).expect(""), 4);
/* project the seq of u64 representations to a view
*/

View file

@ -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"
*/