From c2a9d8e3bd79fd2bb1714ab0f8568949c5b157ec Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 20 Feb 2023 03:40:37 +0100 Subject: [PATCH] impl ObjCommander, simplify type creation & data retrieval from node" --- nested/src/commander.rs | 10 +- nested/src/editors/char/mod.rs | 11 +-- nested/src/editors/integer/editor.rs | 4 +- nested/src/editors/list/editor.rs | 49 +++++++--- nested/src/editors/list/pty_editor.rs | 37 ++++--- nested/src/editors/product/editor.rs | 107 +++++++++++---------- nested/src/editors/product/nav.rs | 2 +- nested/src/editors/sum/editor.rs | 62 +++++++----- nested/src/tree/node.rs | 68 +++++++++++-- nested/src/type_system/context.rs | 16 ++- nested/src/type_system/make_editor.rs | 21 ++-- nested/src/type_system/mod.rs | 4 +- nested/src/type_system/repr_tree.rs | 28 +++--- nested/src/type_system/type_term.rs | 1 + nested/src/type_system/type_term_editor.rs | 10 +- 15 files changed, 269 insertions(+), 161 deletions(-) diff --git a/nested/src/commander.rs b/nested/src/commander.rs index d58db3d..2dee032 100644 --- a/nested/src/commander.rs +++ b/nested/src/commander.rs @@ -7,12 +7,18 @@ pub trait Commander { use std::sync::{Arc, RwLock}; use crate::{ - type_system::ReprTree + type_system::ReprTree, + tree::{nav::TreeNavResult, NestedNode} }; //use r3vi::view::singleton::*; pub trait ObjCommander { + + fn send_cmd_node(&mut self, node: NestedNode) -> TreeNavResult { + TreeNavResult::Continue + } + fn send_cmd_obj(&mut self, cmd_obj: Arc>); } @@ -20,7 +26,7 @@ pub trait ObjCommander { impl ObjCommander for C where C::Cmd: 'static { - fn send_cmd_obj(&mut self, cmd_obj: Arc>) { + fn send_cmd_obj(&mut self, _cmd_obj: Arc>) { /* self.send_cmd( &cmd_obj.read().unwrap() diff --git a/nested/src/editors/char/mod.rs b/nested/src/editors/char/mod.rs index c4d9374..e77175a 100644 --- a/nested/src/editors/char/mod.rs +++ b/nested/src/editors/char/mod.rs @@ -8,13 +8,12 @@ use { }, crate::{ type_system::{Context, ReprTree}, - terminal::{TerminalAtom, TerminalEvent, TerminalStyle}, + terminal::{TerminalAtom, TerminalStyle}, tree::NestedNode, commander::{ObjCommander} }, std::sync::Arc, - std::sync::RwLock, - termion::event::{Event, Key} + std::sync::RwLock }; pub struct CharEditor { @@ -24,13 +23,11 @@ pub struct CharEditor { impl ObjCommander for CharEditor { fn send_cmd_obj(&mut self, cmd_obj: Arc>) { - let ctx = self.ctx.read().unwrap(); - let cmd_obj = cmd_obj.read().unwrap(); let cmd_type = cmd_obj.get_type().clone(); - let char_type = ctx.type_term_from_str("( Char )").unwrap(); - let term_event_type = ctx.type_term_from_str("( TerminalEvent )").unwrap(); + let char_type = (&self.ctx, "( Char )").into(); + //let _term_event_type = (&ctx, "( TerminalEvent )").into(); if cmd_type == char_type { if let Some(cmd_view) = cmd_obj.get_view::>() { diff --git a/nested/src/editors/integer/editor.rs b/nested/src/editors/integer/editor.rs index 1bfe69b..5863557 100644 --- a/nested/src/editors/integer/editor.rs +++ b/nested/src/editors/integer/editor.rs @@ -79,7 +79,7 @@ impl DigitEditor { pub fn into_node(self, depth: usize) -> NestedNode { let data = self.get_data(); let editor = Arc::new(RwLock::new(self)); - let mut ed = editor.write().unwrap(); + let ed = editor.write().unwrap(); let r = ed.radix; NestedNode::new(depth) @@ -140,7 +140,7 @@ pub struct PosIntEditor { impl PosIntEditor { pub fn new(ctx: Arc>, radix: u32) -> Self { - let mut editor = PTYListEditor::new( + let editor = PTYListEditor::new( ctx.clone(), TypeTerm::Type { id: ctx.read().unwrap().get_typeid("Digit").unwrap(), diff --git a/nested/src/editors/list/editor.rs b/nested/src/editors/list/editor.rs index cf662b1..1c27977 100644 --- a/nested/src/editors/list/editor.rs +++ b/nested/src/editors/list/editor.rs @@ -1,24 +1,14 @@ use { r3vi::{ - view::{ - OuterViewPort, - singleton::*, - sequence::*, - }, - buffer::{ - singleton::*, - vec::*, - } + view::{OuterViewPort, singleton::*, sequence::*}, + buffer::{singleton::*, vec::*} }, crate::{ type_system::{Context, TypeTerm, ReprTree}, - editors::list::{ - ListCursor, - ListCursorMode - }, + editors::list::{ListCursor, ListCursorMode}, tree::{NestedNode, TreeNav} }, - std::sync::{Arc, RwLock}, + std::sync::{Arc, RwLock} }; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> @@ -246,7 +236,7 @@ impl ListEditor { self.cursor.set(cur); } } - + /// split the list off at the current cursor position and return the second half pub fn split(&mut self) -> ListEditor { let mut le = ListEditor::new(self.ctx.clone(), self.typ.clone()); @@ -258,6 +248,35 @@ impl ListEditor { le.data.push( self.data.get(idx) ); self.data.remove(idx); } + + if self.is_listlist() { + if idx > 0 && idx < self.data.len() { + let prev_idx = idx - 1; + let prev_node = self.data.get(prev_idx); + + if let Some(prev_editor) = prev_node.editor.clone() { + eprintln!("prev prev editor"); + let mut prev_editor = prev_editor.downcast::>().unwrap(); + let mut prev_editor = prev_editor.write().unwrap(); + if prev_editor.get_data_port().get_view().unwrap() + .iter().filter( + |x| + /* + if let Some(data) = x.data.clone() { + let data = data.; + data.read().unwrap().is_some() + } else { + false + } + */ true + ).count() == 0 + { + drop(prev_editor); + self.data.remove(prev_idx); + } + } + } + } } le diff --git a/nested/src/editors/list/pty_editor.rs b/nested/src/editors/list/pty_editor.rs index 9871520..8a21fe2 100644 --- a/nested/src/editors/list/pty_editor.rs +++ b/nested/src/editors/list/pty_editor.rs @@ -1,23 +1,12 @@ use { r3vi::{ - view::{ - OuterViewPort, - sequence::*, - }, + view::{OuterViewPort, sequence::*}, projection::decorate_sequence::*, }, crate::{ type_system::{Context, TypeTerm, ReprTree}, - editors::list::{ - ListCursor, ListCursorMode, - segment::{ListSegmentSequence}, - editor::ListEditor - }, - terminal::{ - TerminalEvent, - TerminalView, - make_label - }, + editors::list::*, + terminal::{TerminalEvent, TerminalView, make_label}, tree::{TreeCursor, TreeNav}, diagnostics::{Diagnostics}, tree::NestedNode, @@ -274,11 +263,21 @@ impl ObjCommander for PTYListEditor { e.delete_nexd(); } _ => { - let mut new_edit = Context::make_node(&e.ctx, e.typ.clone(), self.depth).unwrap(); - new_edit.goto(TreeCursor::home()); - new_edit.send_cmd_obj(cmd_obj); + let mut node = Context::make_node(&e.ctx, e.typ.clone(), self.depth).unwrap(); + node.goto(TreeCursor::home()); + node.send_cmd_obj(cmd_obj); +/* + if e.is_listlist() { + if let Some(new_edit) = node.get_edit::() { + if new_edit.data.len() == 0 { + remove = true; + } + } + } - e.insert(new_edit); + if ! remove { + */ + e.insert(node); } } }, @@ -302,7 +301,7 @@ impl ObjCommander for PTYListEditor { if item_cur.tree_addr.len() > 1 { let mut item = e.get_item_mut().unwrap(); item.handle_terminal_event(event); - } + } */ } else { item.send_cmd_obj(cmd_obj); diff --git a/nested/src/editors/product/editor.rs b/nested/src/editors/product/editor.rs index 64ab5c6..ef9d88d 100644 --- a/nested/src/editors/product/editor.rs +++ b/nested/src/editors/product/editor.rs @@ -185,75 +185,84 @@ impl ProductEditor { self.update_segment(c); } } -} -use crate::commander::Commander; -impl Commander for ProductEditor { - type Cmd = TerminalEvent; - - fn send_cmd(&mut self, ev: &TerminalEvent) { - self.handle_terminal_event(ev); - } -} - -impl TerminalEditor for ProductEditor { - fn get_term_view(&self) -> OuterViewPort { + pub fn get_term_view(&self) -> OuterViewPort { let ctx = self.ctx.clone(); self.segments .get_port() .map_item(move |_pos, e: &ProductEditorSegment| { e.get_view(ctx.clone()) }) .flatten() } +} - fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult { - let mut update_segment = false; +use r3vi::view::singleton::SingletonView; +use crate::{commander::ObjCommander, type_system::ReprTree}; - let result = if let Some(mut segment) = self.get_cur_segment_mut().as_deref_mut() { - if let Some(ProductEditorSegment::N{ t, editor, ed_depth, cur_depth, cur_dist: _ }) = segment.deref_mut() { - *cur_depth = self.get_cursor().tree_addr.len(); +impl ObjCommander for ProductEditor { + fn send_cmd_obj(&mut self, cmd_obj: Arc>) { + let co = cmd_obj.read().unwrap(); + let cmd_type = co.get_type().clone(); + let term_event_type = (&self.ctx, "( TerminalEvent )").into(); - if let Some(mut e) = editor.clone() { - match e.handle_terminal_event(event) { - TerminalEditorResult::Exit => - match event { - TerminalEvent::Input(Event::Key(Key::Backspace)) => { - *editor = None; - update_segment = true; + if cmd_type == term_event_type { + if let Some(te_view) = co.get_view::>() { + drop(co); + let event = te_view.get(); + + let mut update_segment = false; + + let result = if let Some(mut segment) = self.get_cur_segment_mut().as_deref_mut() { + if let Some(ProductEditorSegment::N{ t, editor, ed_depth, cur_depth, cur_dist: _ }) = segment.deref_mut() { + *cur_depth = self.get_cursor().tree_addr.len(); + + if let Some(mut e) = editor.clone() { + match e.handle_terminal_event(&event) { + TerminalEditorResult::Exit => + match event { + TerminalEvent::Input(Event::Key(Key::Backspace)) => { + *editor = None; + update_segment = true; + TerminalEditorResult::Continue + } + _ => { + *cur_depth = e.get_cursor().tree_addr.len(); + match self.nexd() { + TreeNavResult::Continue => TerminalEditorResult::Continue, + TreeNavResult::Exit => TerminalEditorResult::Exit + } + } + }, + TerminalEditorResult::Continue => { + *cur_depth = e.get_cursor().tree_addr.len(); TerminalEditorResult::Continue } - _ => { - *cur_depth = e.get_cursor().tree_addr.len(); - match self.nexd() { - TreeNavResult::Continue => TerminalEditorResult::Continue, - TreeNavResult::Exit => TerminalEditorResult::Exit - } - } - }, - TerminalEditorResult::Continue => { - *cur_depth = e.get_cursor().tree_addr.len(); - TerminalEditorResult::Continue + } + } else { + let mut e = Context::make_node(&self.ctx, t[0].clone(), *ed_depth+1).unwrap(); + *editor = Some(e.clone()); + update_segment = true; + + e.dn(); + let x = e.handle_terminal_event(&event); + x } + } else { + unreachable!(); } } else { - let mut e = Context::make_node(&self.ctx, t[0].clone(), *ed_depth+1).unwrap(); - *editor = Some(e.clone()); - update_segment = true; + TerminalEditorResult::Exit + }; - e.dn(); - let x = e.handle_terminal_event(event); - x + if update_segment { + self.update_cur_segment(); } - } else { - unreachable!(); } } else { - TerminalEditorResult::Exit - }; - - if update_segment { - self.update_cur_segment(); + drop(co); + if let Some(mut node) = self.get_cur_editor() { + node.send_cmd_obj(cmd_obj); + } } - result } } diff --git a/nested/src/editors/product/nav.rs b/nested/src/editors/product/nav.rs index 89354d2..7747715 100644 --- a/nested/src/editors/product/nav.rs +++ b/nested/src/editors/product/nav.rs @@ -172,7 +172,7 @@ impl TreeNav for ProductEditor { let nav_result = if let Some(mut element) = self.get_cur_segment_mut() { if let Some(ProductEditorSegment::N{ t: _, editor, ed_depth: _, cur_depth, cur_dist:_ }) = element.deref_mut() { - if let Some(mut e) = editor.as_mut() { + if let Some(e) = editor.as_mut() { //\\//\\//\\//\\ // horizontal // //\\//\\//\\//\\ diff --git a/nested/src/editors/sum/editor.rs b/nested/src/editors/sum/editor.rs index 51574c4..86f49ac 100644 --- a/nested/src/editors/sum/editor.rs +++ b/nested/src/editors/sum/editor.rs @@ -2,6 +2,7 @@ use { r3vi::{ view::{ ViewPort, OuterViewPort, + singleton::*, sequence::*, } }, @@ -10,11 +11,12 @@ use { TerminalEditor, TerminalEditorResult, TerminalEvent, TerminalView }, - type_system::{Context}, + editors::list::ListCursorMode, + type_system::{Context, ReprTree}, tree::{TreeNav, TreeCursor, TreeNavResult}, diagnostics::{Diagnostics, Message}, tree::NestedNode, - commander::Commander, + commander::{ObjCommander}, PtySegment }, cgmath::{Vector2}, @@ -26,6 +28,9 @@ pub struct SumEditor { cur: usize, pub editors: Vec< NestedNode >, + addr_port: ViewPort< dyn SequenceView >, + mode_port: ViewPort< dyn SingletonView >, + port: ViewPort< dyn TerminalView >, diag_port: ViewPort< dyn SequenceView > } @@ -40,7 +45,11 @@ impl SumEditor { cur: 0, editors, port, - diag_port: ViewPort::new() + diag_port: ViewPort::new(), + + + addr_port: ViewPort::new(), + mode_port: ViewPort::new() } } @@ -50,6 +59,7 @@ impl SumEditor { NestedNode::new(0) .set_ctx(ctx) .set_view(view) + .set_editor(editor.clone()) .set_cmd(editor.clone()) .set_nav(editor.clone()) // .set_diag(editor.read().unwrap().diag.clone()) @@ -62,7 +72,7 @@ impl SumEditor { pub fn select(&mut self, idx: usize) { self.cur = idx; - let tv = self.editors[ self.cur ].get_term_view(); + let tv = self.editors[ self.cur ].get_view(); tv.add_observer( self.port.get_cast() ); self.port.update_hooks.write().unwrap().clear(); self.port.add_update_hook( Arc::new(tv.0.clone()) ); @@ -73,6 +83,18 @@ impl SumEditor { self.diag_port.update_hooks.write().unwrap().clear(); self.diag_port.add_update_hook( Arc::new(dv.0.clone()) ); self.diag_port.set_view( Some(dv.get_view_arc()) ); + + let dv = self.editors[ self.cur ].get_addr_view(); + dv.add_observer( self.addr_port.get_cast() ); + self.addr_port.update_hooks.write().unwrap().clear(); + self.addr_port.add_update_hook( Arc::new(dv.0.clone()) ); + self.addr_port.set_view( Some(dv.get_view_arc()) ); + + let dv = self.editors[ self.cur ].get_mode_view(); + dv.add_observer( self.mode_port.get_cast() ); + self.mode_port.update_hooks.write().unwrap().clear(); + self.mode_port.add_update_hook( Arc::new(dv.0.clone()) ); + self.mode_port.set_view( Some(dv.get_view_arc()) ); } } @@ -92,6 +114,14 @@ impl TreeNav for SumEditor { fn goto(&mut self, new_cursor: TreeCursor) -> TreeNavResult { self.editors[ self.cur ].goto( new_cursor ) } + + fn get_addr_view(&self) -> OuterViewPort> { + self.addr_port.outer() + } + + fn get_mode_view(&self) -> OuterViewPort> { + self.mode_port.outer() + } } impl PtySegment for SumEditor { @@ -100,26 +130,8 @@ impl PtySegment for SumEditor { } } -impl Commander for SumEditor { - type Cmd = TerminalEvent; - - fn send_cmd(&mut self, event: &TerminalEvent) { - match event { - TerminalEvent::Input( termion::event::Event::Key(Key::Ctrl('x')) ) => { - let res = self.editors[ self.cur ].handle_terminal_event( event ); - match res { - TerminalEditorResult::Exit => { - self.select( (self.cur + 1) % self.editors.len() ); - if self.editors[ self.cur ].get_cursor().tree_addr.len() == 0 { - self.dn(); - } - }, - _ => {} - } - }, - event => { - self.editors[ self.cur ].handle_terminal_event( event ); - } - } +impl ObjCommander for SumEditor { + fn send_cmd_obj(&mut self, obj: Arc>) { + self.editors[ self.cur ].send_cmd_obj( obj ); } } diff --git a/nested/src/tree/node.rs b/nested/src/tree/node.rs index 697adaa..55377d7 100644 --- a/nested/src/tree/node.rs +++ b/nested/src/tree/node.rs @@ -1,17 +1,13 @@ use { std::{sync::{Arc, RwLock}, any::Any}, - cgmath::Vector2, + cgmath::{Vector2, Point2}, r3vi::{ - view::{ - ViewPort, OuterViewPort, AnyOuterViewPort, - singleton::*, - sequence::* - }, + view::{View, ViewPort, OuterViewPort, AnyOuterViewPort, singleton::*, sequence::*}, buffer::{singleton::*} }, crate::{ type_system::{ReprTree, Context, TypeTerm}, - terminal::{TerminalView, TerminalEvent, TerminalEditor, TerminalEditorResult}, + terminal::{TerminalView, TerminalEvent, TerminalEditor, TerminalEditorResult, TerminalAtom}, diagnostics::{Diagnostics, Message}, tree::{TreeNav, TreeCursor, TreeNavResult}, editors::list::{ListCursorMode}, @@ -101,7 +97,7 @@ impl TreeNav for NestedNode { tn.read().unwrap().get_addr_view() } else { OuterViewPort::default() - } + } } fn get_mode_view(&self) -> OuterViewPort> { @@ -161,6 +157,31 @@ impl NestedNode { } } + pub fn from_char(ctx: Arc>, c: char) -> NestedNode { + let buf = r3vi::buffer::singleton::SingletonBuffer::::new(c); + + NestedNode::new(0) + .set_view(buf.get_port() + .map(|c| TerminalAtom::from(c)) + .to_index() + .map_key( + |x| { + Point2::new(0, 0) + }, + |p| { + if *p == Point2::new(0,0) { Some(()) } else { None } + }) + ) + .set_data( + ReprTree::new_leaf( + (&ctx, "( Char )"), + buf.get_port().into() + ) + ) + .set_editor(Arc::new(RwLock::new(buf))) + .set_ctx(ctx) + } + pub fn set_ctx(mut self, ctx: Arc>) -> Self { self.ctx = Some(ctx); self @@ -207,5 +228,36 @@ impl NestedNode { pub fn morph(self, ty: TypeTerm) -> NestedNode { Context::morph_node(self, ty) } + + pub fn get_data_view<'a, V: View + ?Sized + 'static>(&'a self, type_str: impl Iterator) -> Option> + where V::Msg: Clone { + if let Some(ctx) = self.ctx.clone() { + if let Some(data) = self.data.clone() { + let type_ladder = type_str.map(|s| ((&ctx, s)).into()); + + let repr_tree = ReprTree::descend_ladder(&data, type_ladder)?; + repr_tree.clone().read().unwrap() + .get_view::().clone() + } else { + eprintln!("get_data(): no data port"); + None + } + } else { + eprintln!("get_data(): no ctx"); + None + } + } + + pub fn get_edit(&self) -> Option>> { + if let Some(edit) = self.editor.clone() { + if let Ok(edit) = edit.downcast::>() { + Some(edit) + } else { + None + } + } else { + None + } + } } diff --git a/nested/src/type_system/context.rs b/nested/src/type_system/context.rs index 90ce78e..c8300d8 100644 --- a/nested/src/type_system/context.rs +++ b/nested/src/type_system/context.rs @@ -42,14 +42,18 @@ pub struct MorphismType { #[derive(Hash, Eq, PartialEq, Debug)] pub struct MorphismTypePattern { - pub src_type: Option, + pub src_tyid: Option, pub dst_tyid: TypeID } impl From for MorphismTypePattern { fn from(value: MorphismType) -> MorphismTypePattern { MorphismTypePattern { - src_type: value.src_type, + src_tyid: match value.src_type { + Some(TypeTerm::Type { id, args: _ }) => Some(id), + _ => None, + }, + dst_tyid: match value.dst_type { TypeTerm::Type { id, args: _ } => id, _ => unreachable!() @@ -84,6 +88,12 @@ pub struct Context { parent: Option>>, } +impl Into for (&Arc>, &str) { + fn into(self) -> TypeTerm { + self.0.read().unwrap().type_term_from_str(self.1).unwrap() + } +} + impl Context { pub fn with_parent(parent: Option>>) -> Self { Context { @@ -154,7 +164,7 @@ impl Context { }; let morphism_pattern = MorphismTypePattern { - src_type: None, + src_tyid: None, dst_tyid: tyid }; diff --git a/nested/src/type_system/make_editor.rs b/nested/src/type_system/make_editor.rs index 317e30b..dda3555 100644 --- a/nested/src/type_system/make_editor.rs +++ b/nested/src/type_system/make_editor.rs @@ -5,13 +5,12 @@ use { char::*, list::*, integer::*, - product::*, - sum::* + product::* }, tree::{NestedNode}, terminal::{TerminalEditor}, diagnostics::{Diagnostics}, - type_system::{TypeTermEditor, MorphismTypePattern}, + type_system::{MorphismTypePattern}, }, std::sync::{Arc, RwLock}, cgmath::Point2 @@ -28,7 +27,7 @@ pub fn init_mem_ctx(parent: Arc>) -> Arc> { id: _, args } => { if args.len() > 0 { - let mut buf = r3vi::buffer::vec::VecBuffer::::new(); + let buf = r3vi::buffer::vec::VecBuffer::::new(); let data = ReprTree::new_leaf( ctx.read().unwrap().type_term_from_str("( Char )").unwrap(), buf.get_port().into() @@ -105,7 +104,7 @@ pub fn init_editor_ctx(parent: Arc>) -> Arc> { ctx.write().unwrap().add_list_typename("Symbol".into()); let pattern = MorphismTypePattern { - src_type: ctx.read().unwrap().type_term_from_str("( List Char )"), + src_tyid: ctx.read().unwrap().get_typeid("List"), dst_tyid: ctx.read().unwrap().get_typeid("Symbol").unwrap() }; ctx.write().unwrap().add_morphism(pattern, @@ -151,7 +150,7 @@ pub fn init_editor_ctx(parent: Arc>) -> Arc> { ctx.write().unwrap().add_list_typename("String".into()); let pattern = MorphismTypePattern { - src_type: ctx.read().unwrap().type_term_from_str("( List Char )"), + src_tyid: ctx.read().unwrap().get_typeid("List"), dst_tyid: ctx.read().unwrap().get_typeid("String").unwrap() }; ctx.write().unwrap().add_morphism(pattern, @@ -196,7 +195,7 @@ pub fn init_editor_ctx(parent: Arc>) -> Arc> { } ) ); - +/* ctx.write().unwrap().add_list_typename("TypeTerm".into()); ctx.write().unwrap().add_node_ctor( "TypeTerm", Arc::new( @@ -205,7 +204,7 @@ pub fn init_editor_ctx(parent: Arc>) -> Arc> { } ) ); - +*/ ctx.write().unwrap().add_typename("TerminalEvent".into()); ctx } @@ -246,7 +245,7 @@ pub fn init_math_ctx(parent: Arc>) -> Arc> { ctx.write().unwrap().add_list_typename("PosInt".into()); let pattern = MorphismTypePattern { - src_type: ctx.read().unwrap().type_term_from_str("( List ( Digit 10 ) )"), + src_tyid: ctx.read().unwrap().get_typeid("List"), dst_tyid: ctx.read().unwrap().get_typeid("PosInt").unwrap() }; ctx.write().unwrap().add_morphism(pattern, @@ -255,13 +254,15 @@ pub fn init_math_ctx(parent: Arc>) -> Arc> { let depth = node.depth; let editor = node.editor.clone().unwrap().downcast::>().unwrap(); + // todo: check src_type parameter to be ( Digit radix ) + match dst_type { TypeTerm::Type { id: _, args } => { if args.len() > 0 { match args[0] { - TypeTerm::Num(radix) => { + TypeTerm::Num(_radix) => { let pty_editor = PTYListEditor::from_editor( editor, Some(','), diff --git a/nested/src/type_system/mod.rs b/nested/src/type_system/mod.rs index 2db9051..a4c590a 100644 --- a/nested/src/type_system/mod.rs +++ b/nested/src/type_system/mod.rs @@ -3,13 +3,13 @@ pub mod type_term; pub mod repr_tree; pub mod context; pub mod make_editor; -pub mod type_term_editor; +//pub mod type_term_editor; pub use { repr_tree::{ReprTree}, type_term::{TypeDict, TypeID, TypeTerm, TypeLadder}, context::{Context, MorphismMode, MorphismType, MorphismTypePattern}, - type_term_editor::TypeTermEditor, +// type_term_editor::TypeTermEditor, make_editor::* }; diff --git a/nested/src/type_system/repr_tree.rs b/nested/src/type_system/repr_tree.rs index e551c9b..7fa3a1c 100644 --- a/nested/src/type_system/repr_tree.rs +++ b/nested/src/type_system/repr_tree.rs @@ -19,9 +19,9 @@ pub struct ReprTree { } impl ReprTree { - pub fn new(type_tag: TypeTerm) -> Self { + pub fn new(type_tag: impl Into) -> Self { ReprTree { - type_tag, + type_tag: type_tag.into(), port: None, branches: HashMap::new(), } @@ -31,8 +31,8 @@ impl ReprTree { &self.type_tag } - pub fn new_leaf(type_tag: TypeTerm, port: AnyOuterViewPort) -> Arc> { - let mut tree = ReprTree::new(type_tag); + pub fn new_leaf(type_tag: impl Into, port: AnyOuterViewPort) -> Arc> { + let mut tree = ReprTree::new(type_tag.into()); tree.insert_leaf(vec![].into_iter(), port); Arc::new(RwLock::new(tree)) } @@ -82,18 +82,22 @@ impl ReprTree { .get_view() } - pub fn descend(&self, dst_type: &TypeTerm) -> Option>> { - self.branches.get(dst_type).cloned() + pub fn descend(&self, dst_type: impl Into) -> Option>> { + self.branches.get(&dst_type.into()).cloned() } - pub fn descend_ladder(&self, mut repr_ladder: impl Iterator) -> Option>> { - let first = repr_ladder.next()?; - repr_ladder.fold( - self.descend(&first), - |s, t| s?.read().unwrap().descend(&t)) + pub fn descend_ladder(rt: &Arc>, mut repr_ladder: impl Iterator) -> Option>> { + if let Some(first) = repr_ladder.next() { + let rt = rt.read().unwrap(); + repr_ladder.fold( + rt.descend(first), + |s, t| s?.read().unwrap().descend(t)) + } else { + Some(rt.clone()) + } } - pub fn ascend(rt: &Arc>, type_term: TypeTerm) -> Arc> { + pub fn ascend(rt: &Arc>, type_term: impl Into) -> Arc> { let mut n = Self::new(type_term); n.insert_branch(rt.clone()); Arc::new(RwLock::new(n)) diff --git a/nested/src/type_system/type_term.rs b/nested/src/type_system/type_term.rs index de2281d..fb3243e 100644 --- a/nested/src/type_system/type_term.rs +++ b/nested/src/type_system/type_term.rs @@ -11,6 +11,7 @@ pub type TypeLadder = Vec; pub enum TypeTerm { Type { id: TypeID, args: Vec }, Num(i64), +// Var(u64), } impl TypeTerm { diff --git a/nested/src/type_system/type_term_editor.rs b/nested/src/type_system/type_term_editor.rs index 775d62e..19a385d 100644 --- a/nested/src/type_system/type_term_editor.rs +++ b/nested/src/type_system/type_term_editor.rs @@ -2,9 +2,7 @@ use { crate::{ type_system::{Context}, terminal::{TerminalEvent}, - editors::{ - sum::*, - }, + editors::{sum::*}, tree::{TreeNav}, tree::NestedNode, commander::Commander, @@ -37,9 +35,9 @@ impl TypeTermEditor { ty: TypeTermVar::Any, sum_edit: Arc::new(RwLock::new(SumEditor::new( vec![ - Context::make_node( &ctx, ctx.read().unwrap().type_term_from_str("( List TypeTerm )").unwrap(), depth + 1).unwrap(), - Context::make_node( &ctx, ctx.read().unwrap().type_term_from_str("( PosInt 10 )").unwrap(), depth + 1 ).unwrap(), - Context::make_node( &ctx, ctx.read().unwrap().type_term_from_str("( Symbol )").unwrap(), depth + 1 ).unwrap() + Context::make_node( &ctx, (&ctx, "( List TypeTerm )").into(), depth + 1).unwrap(), + Context::make_node( &ctx, (&ctx, "( PosInt 10 )").into(), depth + 1 ).unwrap(), + Context::make_node( &ctx, (&ctx, "( Symbol )").into(), depth + 1 ).unwrap() ]))) } }