impl ObjCommander, simplify type creation & data retrieval from node"

This commit is contained in:
Michael Sippel 2023-02-20 03:40:37 +01:00
parent cf313727a6
commit c2a9d8e3bd
Signed by: senvas
GPG key ID: F96CF119C34B64A6
15 changed files with 269 additions and 161 deletions

View file

@ -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<RwLock<ReprTree>>);
}
@ -20,7 +26,7 @@ pub trait ObjCommander {
impl<C: Commander> ObjCommander for C
where C::Cmd: 'static
{
fn send_cmd_obj(&mut self, cmd_obj: Arc<RwLock<ReprTree>>) {
fn send_cmd_obj(&mut self, _cmd_obj: Arc<RwLock<ReprTree>>) {
/*
self.send_cmd(
&cmd_obj.read().unwrap()

View file

@ -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<RwLock<ReprTree>>) {
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::<dyn SingletonView<Item = char>>() {

View file

@ -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<RwLock<Context>>, radix: u32) -> Self {
let mut editor = PTYListEditor::new(
let editor = PTYListEditor::new(
ctx.clone(),
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("Digit").unwrap(),

View file

@ -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::<RwLock<ListEditor>>().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

View file

@ -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::<ListEditor>() {
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);

View file

@ -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<dyn TerminalView> {
pub fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
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<RwLock<ReprTree>>) {
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::<dyn SingletonView<Item = TerminalEvent>>() {
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
}
}

View file

@ -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 //
//\\//\\//\\//\\

View file

@ -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<Item = isize> >,
mode_port: ViewPort< dyn SingletonView<Item = ListCursorMode> >,
port: ViewPort< dyn TerminalView >,
diag_port: ViewPort< dyn SequenceView<Item = Message> >
}
@ -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<dyn SequenceView<Item = isize>> {
self.addr_port.outer()
}
fn get_mode_view(&self) -> OuterViewPort<dyn SingletonView<Item = ListCursorMode>> {
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<RwLock<ReprTree>>) {
self.editors[ self.cur ].send_cmd_obj( obj );
}
}

View file

@ -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<dyn SingletonView<Item = ListCursorMode>> {
@ -161,6 +157,31 @@ impl NestedNode {
}
}
pub fn from_char(ctx: Arc<RwLock<Context>>, c: char) -> NestedNode {
let buf = r3vi::buffer::singleton::SingletonBuffer::<char>::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<RwLock<Context>>) -> 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<Item = &'a str>) -> Option<Arc<V>>
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::<V>().clone()
} else {
eprintln!("get_data(): no data port");
None
}
} else {
eprintln!("get_data(): no ctx");
None
}
}
pub fn get_edit<T: Send + Sync + 'static>(&self) -> Option<Arc<RwLock<T>>> {
if let Some(edit) = self.editor.clone() {
if let Ok(edit) = edit.downcast::<RwLock<T>>() {
Some(edit)
} else {
None
}
} else {
None
}
}
}

View file

@ -42,14 +42,18 @@ pub struct MorphismType {
#[derive(Hash, Eq, PartialEq, Debug)]
pub struct MorphismTypePattern {
pub src_type: Option<TypeTerm>,
pub src_tyid: Option<TypeID>,
pub dst_tyid: TypeID
}
impl From<MorphismType> 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<Arc<RwLock<Context>>>,
}
impl Into<TypeTerm> for (&Arc<RwLock<Context>>, &str) {
fn into(self) -> TypeTerm {
self.0.read().unwrap().type_term_from_str(self.1).unwrap()
}
}
impl Context {
pub fn with_parent(parent: Option<Arc<RwLock<Context>>>) -> Self {
Context {
@ -154,7 +164,7 @@ impl Context {
};
let morphism_pattern = MorphismTypePattern {
src_type: None,
src_tyid: None,
dst_tyid: tyid
};

View file

@ -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<RwLock<Context>>) -> Arc<RwLock<Context>> {
id: _, args
} => {
if args.len() > 0 {
let mut buf = r3vi::buffer::vec::VecBuffer::<char>::new();
let buf = r3vi::buffer::vec::VecBuffer::<char>::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<RwLock<Context>>) -> Arc<RwLock<Context>> {
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<RwLock<Context>>) -> Arc<RwLock<Context>> {
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<RwLock<Context>>) -> Arc<RwLock<Context>> {
}
)
);
/*
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<RwLock<Context>>) -> Arc<RwLock<Context>> {
}
)
);
*/
ctx.write().unwrap().add_typename("TerminalEvent".into());
ctx
}
@ -246,7 +245,7 @@ pub fn init_math_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
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<RwLock<Context>>) -> Arc<RwLock<Context>> {
let depth = node.depth;
let editor = node.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().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(','),

View file

@ -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::*
};

View file

@ -19,9 +19,9 @@ pub struct ReprTree {
}
impl ReprTree {
pub fn new(type_tag: TypeTerm) -> Self {
pub fn new(type_tag: impl Into<TypeTerm>) -> 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<RwLock<Self>> {
let mut tree = ReprTree::new(type_tag);
pub fn new_leaf(type_tag: impl Into<TypeTerm>, port: AnyOuterViewPort) -> Arc<RwLock<Self>> {
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<Arc<RwLock<ReprTree>>> {
self.branches.get(dst_type).cloned()
pub fn descend(&self, dst_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
self.branches.get(&dst_type.into()).cloned()
}
pub fn descend_ladder(&self, mut repr_ladder: impl Iterator<Item = TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
let first = repr_ladder.next()?;
repr_ladder.fold(
self.descend(&first),
|s, t| s?.read().unwrap().descend(&t))
pub fn descend_ladder(rt: &Arc<RwLock<Self>>, mut repr_ladder: impl Iterator<Item = TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
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<RwLock<Self>>, type_term: TypeTerm) -> Arc<RwLock<ReprTree>> {
pub fn ascend(rt: &Arc<RwLock<Self>>, type_term: impl Into<TypeTerm>) -> Arc<RwLock<ReprTree>> {
let mut n = Self::new(type_term);
n.insert_branch(rt.clone());
Arc::new(RwLock::new(n))

View file

@ -11,6 +11,7 @@ pub type TypeLadder = Vec<TypeTerm>;
pub enum TypeTerm {
Type { id: TypeID, args: Vec<TypeTerm> },
Num(i64),
// Var(u64),
}
impl TypeTerm {

View file

@ -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()
])))
}
}