add ReprTree::from_str() to simplify creation of initial representation nodes
This commit is contained in:
parent
6e2b82585e
commit
8d637a6f32
4 changed files with 47 additions and 16 deletions
|
@ -43,20 +43,24 @@ async fn main() {
|
|||
|
||||
/* Create a Representation-Tree of type <List Char>
|
||||
*/
|
||||
let mut rt_string = ReprTree::new_arc( Context::parse(&ctx, "<List Char>") );
|
||||
|
||||
rt_string.insert_leaf(
|
||||
Context::parse(&ctx, "<List~Vec EditTree>"),
|
||||
nested::repr_tree::ReprLeaf::from_vec_buffer( VecBuffer::<Arc<RwLock<EditTree>>>::new() )
|
||||
let mut rt_string = ReprTree::from_str(
|
||||
Context::parse(&ctx, "<List Char>~<Vec Char>"),
|
||||
"hello world"
|
||||
);
|
||||
|
||||
/* create EditTree
|
||||
*/
|
||||
ctx.read().unwrap().apply_morphism(
|
||||
&rt_string,
|
||||
&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")
|
||||
}
|
||||
);
|
||||
|
||||
// .. avoid cycle of projections..
|
||||
rt_string.write().unwrap().detach(&ctx);
|
||||
|
||||
/* Setup the Editor-View for this ReprTree
|
||||
*/
|
||||
let edittree_list = ctx.read().unwrap()
|
||||
|
@ -65,7 +69,7 @@ async fn main() {
|
|||
SingletonBuffer::new(0).get_port()
|
||||
).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
|
||||
* the value from each EditTree and shows them in a ListView.
|
||||
*/
|
||||
|
|
|
@ -66,16 +66,22 @@ async fn main() {
|
|||
nested_tty::setup_edittree_hook(&ctx);
|
||||
|
||||
/* 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)
|
||||
*/
|
||||
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' ]
|
||||
)));
|
||||
/* VALUE */
|
||||
"cff"
|
||||
);
|
||||
|
||||
/* initially copy values from Vec to EditTree...
|
||||
*/
|
||||
|
|
|
@ -139,6 +139,27 @@ impl ReprTree {
|
|||
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>>
|
||||
where T: Clone + Send + Sync + 'static
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue