add ReprTree::from_str() to simplify creation of initial representation nodes

This commit is contained in:
Michael Sippel 2024-08-06 16:29:28 +02:00
parent 6e2b82585e
commit 8d637a6f32
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 47 additions and 16 deletions

View file

@ -43,20 +43,24 @@ async fn main() {
/* Create a Representation-Tree of type <List Char> /* Create a Representation-Tree of type <List Char>
*/ */
let mut rt_string = ReprTree::new_arc( Context::parse(&ctx, "<List Char>") ); let mut rt_string = ReprTree::from_str(
Context::parse(&ctx, "<List Char>~<Vec Char>"),
rt_string.insert_leaf( "hello world"
Context::parse(&ctx, "<List~Vec EditTree>"),
nested::repr_tree::ReprLeaf::from_vec_buffer( VecBuffer::<Arc<RwLock<EditTree>>>::new() )
); );
/* create EditTree
*/
ctx.read().unwrap().apply_morphism( ctx.read().unwrap().apply_morphism(
&rt_string, &rt_string,
&laddertypes::MorphismType { &laddertypes::MorphismType {
src_type: Context::parse(&ctx, "<List Char~EditTree>~<Vec EditTree>"), src_type: Context::parse(&ctx, "<List~Vec Char>"),
dst_type: Context::parse(&ctx, "<List Char> ~ EditTree") dst_type: Context::parse(&ctx, "<List Char> ~ EditTree")
} }
); );
// .. avoid cycle of projections..
rt_string.write().unwrap().detach(&ctx);
/* Setup the Editor-View for this ReprTree /* Setup the Editor-View for this ReprTree
*/ */
let edittree_list = ctx.read().unwrap() let edittree_list = ctx.read().unwrap()
@ -65,7 +69,7 @@ async fn main() {
SingletonBuffer::new(0).get_port() SingletonBuffer::new(0).get_port()
).unwrap(); ).unwrap();
/* In order to get acces to the values that are modified by the Editor, /* In order to get access to the values that are modified by the Editor,
* we apply a morphism that, given the List of Edit-Trees, extracts * we apply a morphism that, given the List of Edit-Trees, extracts
* the value from each EditTree and shows them in a ListView. * the value from each EditTree and shows them in a ListView.
*/ */

View file

@ -66,16 +66,22 @@ async fn main() {
nested_tty::setup_edittree_hook(&ctx); nested_tty::setup_edittree_hook(&ctx);
/* Create a Representation-Tree of type `` /* Create a Representation-Tree of type ``
* with a specific representation-path (big-endian hexadecimal string)
*/ */
let mut rt_int = ReprTree::new_arc( Context::parse(&ctx, "") ); let mut rt_int = nested::repr_tree::ReprTree::from_str(
/* TYPE */
Context::parse(&ctx, "
~ <PosInt 16 BigEndian>
~ <Seq <Digit 16>>
~ <List <Digit 16>>
~ <List Char>
~ <Vec Char>
"),
/* Add a specific Representation-Path (big-endian hexadecimal) /* VALUE */
*/ "cff"
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(
VecBuffer::with_data(vec![ 'c', 'f', 'f' ]
)));
/* initially copy values from Vec to EditTree... /* initially copy values from Vec to EditTree...
*/ */

View file

@ -91,7 +91,7 @@ impl Context {
src_type: src_type.clone(), src_type: src_type.clone(),
dst_type: dst_type.clone() dst_type: dst_type.clone()
} }
) { ) {
let mut rt = rt.descend( τ ).expect("descend src repr"); let mut rt = rt.descend( τ ).expect("descend src repr");
(m.setup_projection)( &mut rt, &σ ); (m.setup_projection)( &mut rt, &σ );
} }

View file

@ -139,6 +139,27 @@ impl ReprTree {
Arc::new(RwLock::new(rt)) Arc::new(RwLock::new(rt))
} }
pub fn from_str(
type_tag: impl Into<TypeTerm>,
val: &str
) -> Arc<RwLock<Self>> {
let mut lnf = type_tag.into().get_lnf_vec();
let mut rt = ReprTree::from_vec_buffer(
lnf.pop().unwrap(),
VecBuffer::with_data( val.chars().collect() )
);
while let Some(t) = lnf.pop() {
let mut new_rt = ReprTree::new_arc(t);
new_rt.insert_branch(rt);
rt = new_rt;
}
rt
}
pub fn from_vec_buffer<T>( type_tag: impl Into<TypeTerm>, buf: VecBuffer<T> ) -> Arc<RwLock<Self>> pub fn from_vec_buffer<T>( type_tag: impl Into<TypeTerm>, buf: VecBuffer<T> ) -> Arc<RwLock<Self>>
where T: Clone + Send + Sync + 'static where T: Clone + Send + Sync + 'static
{ {