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 std::sync::{Arc, RwLock};
use crate::{ use crate::{
type_system::ReprTree type_system::ReprTree,
tree::{nav::TreeNavResult, NestedNode}
}; };
//use r3vi::view::singleton::*; //use r3vi::view::singleton::*;
pub trait ObjCommander { 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>>); fn send_cmd_obj(&mut self, cmd_obj: Arc<RwLock<ReprTree>>);
} }
@ -20,7 +26,7 @@ pub trait ObjCommander {
impl<C: Commander> ObjCommander for C impl<C: Commander> ObjCommander for C
where C::Cmd: 'static 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( self.send_cmd(
&cmd_obj.read().unwrap() &cmd_obj.read().unwrap()

View file

@ -8,13 +8,12 @@ use {
}, },
crate::{ crate::{
type_system::{Context, ReprTree}, type_system::{Context, ReprTree},
terminal::{TerminalAtom, TerminalEvent, TerminalStyle}, terminal::{TerminalAtom, TerminalStyle},
tree::NestedNode, tree::NestedNode,
commander::{ObjCommander} commander::{ObjCommander}
}, },
std::sync::Arc, std::sync::Arc,
std::sync::RwLock, std::sync::RwLock
termion::event::{Event, Key}
}; };
pub struct CharEditor { pub struct CharEditor {
@ -24,13 +23,11 @@ pub struct CharEditor {
impl ObjCommander for CharEditor { impl ObjCommander for CharEditor {
fn send_cmd_obj(&mut self, cmd_obj: Arc<RwLock<ReprTree>>) { 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_obj = cmd_obj.read().unwrap();
let cmd_type = cmd_obj.get_type().clone(); let cmd_type = cmd_obj.get_type().clone();
let char_type = ctx.type_term_from_str("( Char )").unwrap(); let char_type = (&self.ctx, "( Char )").into();
let term_event_type = ctx.type_term_from_str("( TerminalEvent )").unwrap(); //let _term_event_type = (&ctx, "( TerminalEvent )").into();
if cmd_type == char_type { if cmd_type == char_type {
if let Some(cmd_view) = cmd_obj.get_view::<dyn SingletonView<Item = char>>() { 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 { pub fn into_node(self, depth: usize) -> NestedNode {
let data = self.get_data(); let data = self.get_data();
let editor = Arc::new(RwLock::new(self)); let editor = Arc::new(RwLock::new(self));
let mut ed = editor.write().unwrap(); let ed = editor.write().unwrap();
let r = ed.radix; let r = ed.radix;
NestedNode::new(depth) NestedNode::new(depth)
@ -140,7 +140,7 @@ pub struct PosIntEditor {
impl PosIntEditor { impl PosIntEditor {
pub fn new(ctx: Arc<RwLock<Context>>, radix: u32) -> Self { pub fn new(ctx: Arc<RwLock<Context>>, radix: u32) -> Self {
let mut editor = PTYListEditor::new( let editor = PTYListEditor::new(
ctx.clone(), ctx.clone(),
TypeTerm::Type { TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("Digit").unwrap(), id: ctx.read().unwrap().get_typeid("Digit").unwrap(),

View file

@ -1,24 +1,14 @@
use { use {
r3vi::{ r3vi::{
view::{ view::{OuterViewPort, singleton::*, sequence::*},
OuterViewPort, buffer::{singleton::*, vec::*}
singleton::*,
sequence::*,
},
buffer::{
singleton::*,
vec::*,
}
}, },
crate::{ crate::{
type_system::{Context, TypeTerm, ReprTree}, type_system::{Context, TypeTerm, ReprTree},
editors::list::{ editors::list::{ListCursor, ListCursorMode},
ListCursor,
ListCursorMode
},
tree::{NestedNode, TreeNav} tree::{NestedNode, TreeNav}
}, },
std::sync::{Arc, RwLock}, std::sync::{Arc, RwLock}
}; };
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
@ -258,6 +248,35 @@ impl ListEditor {
le.data.push( self.data.get(idx) ); le.data.push( self.data.get(idx) );
self.data.remove(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 le

View file

@ -1,23 +1,12 @@
use { use {
r3vi::{ r3vi::{
view::{ view::{OuterViewPort, sequence::*},
OuterViewPort,
sequence::*,
},
projection::decorate_sequence::*, projection::decorate_sequence::*,
}, },
crate::{ crate::{
type_system::{Context, TypeTerm, ReprTree}, type_system::{Context, TypeTerm, ReprTree},
editors::list::{ editors::list::*,
ListCursor, ListCursorMode, terminal::{TerminalEvent, TerminalView, make_label},
segment::{ListSegmentSequence},
editor::ListEditor
},
terminal::{
TerminalEvent,
TerminalView,
make_label
},
tree::{TreeCursor, TreeNav}, tree::{TreeCursor, TreeNav},
diagnostics::{Diagnostics}, diagnostics::{Diagnostics},
tree::NestedNode, tree::NestedNode,
@ -274,11 +263,21 @@ impl ObjCommander for PTYListEditor {
e.delete_nexd(); e.delete_nexd();
} }
_ => { _ => {
let mut new_edit = Context::make_node(&e.ctx, e.typ.clone(), self.depth).unwrap(); let mut node = Context::make_node(&e.ctx, e.typ.clone(), self.depth).unwrap();
new_edit.goto(TreeCursor::home()); node.goto(TreeCursor::home());
new_edit.send_cmd_obj(cmd_obj); 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);
} }
} }
}, },

View file

@ -185,27 +185,30 @@ impl ProductEditor {
self.update_segment(c); self.update_segment(c);
} }
} }
}
use crate::commander::Commander; pub fn get_term_view(&self) -> OuterViewPort<dyn TerminalView> {
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> {
let ctx = self.ctx.clone(); let ctx = self.ctx.clone();
self.segments self.segments
.get_port() .get_port()
.map_item(move |_pos, e: &ProductEditorSegment| { e.get_view(ctx.clone()) }) .map_item(move |_pos, e: &ProductEditorSegment| { e.get_view(ctx.clone()) })
.flatten() .flatten()
} }
}
use r3vi::view::singleton::SingletonView;
use crate::{commander::ObjCommander, type_system::ReprTree};
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 cmd_type == term_event_type {
if let Some(te_view) = co.get_view::<dyn SingletonView<Item = TerminalEvent>>() {
drop(co);
let event = te_view.get();
fn handle_terminal_event(&mut self, event: &TerminalEvent) -> TerminalEditorResult {
let mut update_segment = false; let mut update_segment = false;
let result = if let Some(mut segment) = self.get_cur_segment_mut().as_deref_mut() { let result = if let Some(mut segment) = self.get_cur_segment_mut().as_deref_mut() {
@ -213,7 +216,7 @@ impl TerminalEditor for ProductEditor {
*cur_depth = self.get_cursor().tree_addr.len(); *cur_depth = self.get_cursor().tree_addr.len();
if let Some(mut e) = editor.clone() { if let Some(mut e) = editor.clone() {
match e.handle_terminal_event(event) { match e.handle_terminal_event(&event) {
TerminalEditorResult::Exit => TerminalEditorResult::Exit =>
match event { match event {
TerminalEvent::Input(Event::Key(Key::Backspace)) => { TerminalEvent::Input(Event::Key(Key::Backspace)) => {
@ -240,7 +243,7 @@ impl TerminalEditor for ProductEditor {
update_segment = true; update_segment = true;
e.dn(); e.dn();
let x = e.handle_terminal_event(event); let x = e.handle_terminal_event(&event);
x x
} }
} else { } else {
@ -253,7 +256,13 @@ impl TerminalEditor for ProductEditor {
if update_segment { if update_segment {
self.update_cur_segment(); self.update_cur_segment();
} }
result }
} else {
drop(co);
if let Some(mut node) = self.get_cur_editor() {
node.send_cmd_obj(cmd_obj);
}
}
} }
} }

View file

@ -172,7 +172,7 @@ impl TreeNav for ProductEditor {
let nav_result = let nav_result =
if let Some(mut element) = self.get_cur_segment_mut() { 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(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 // // horizontal //
//\\//\\//\\//\\ //\\//\\//\\//\\

View file

@ -2,6 +2,7 @@ use {
r3vi::{ r3vi::{
view::{ view::{
ViewPort, OuterViewPort, ViewPort, OuterViewPort,
singleton::*,
sequence::*, sequence::*,
} }
}, },
@ -10,11 +11,12 @@ use {
TerminalEditor, TerminalEditorResult, TerminalEditor, TerminalEditorResult,
TerminalEvent, TerminalView TerminalEvent, TerminalView
}, },
type_system::{Context}, editors::list::ListCursorMode,
type_system::{Context, ReprTree},
tree::{TreeNav, TreeCursor, TreeNavResult}, tree::{TreeNav, TreeCursor, TreeNavResult},
diagnostics::{Diagnostics, Message}, diagnostics::{Diagnostics, Message},
tree::NestedNode, tree::NestedNode,
commander::Commander, commander::{ObjCommander},
PtySegment PtySegment
}, },
cgmath::{Vector2}, cgmath::{Vector2},
@ -26,6 +28,9 @@ pub struct SumEditor {
cur: usize, cur: usize,
pub editors: Vec< NestedNode >, pub editors: Vec< NestedNode >,
addr_port: ViewPort< dyn SequenceView<Item = isize> >,
mode_port: ViewPort< dyn SingletonView<Item = ListCursorMode> >,
port: ViewPort< dyn TerminalView >, port: ViewPort< dyn TerminalView >,
diag_port: ViewPort< dyn SequenceView<Item = Message> > diag_port: ViewPort< dyn SequenceView<Item = Message> >
} }
@ -40,7 +45,11 @@ impl SumEditor {
cur: 0, cur: 0,
editors, editors,
port, 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) NestedNode::new(0)
.set_ctx(ctx) .set_ctx(ctx)
.set_view(view) .set_view(view)
.set_editor(editor.clone())
.set_cmd(editor.clone()) .set_cmd(editor.clone())
.set_nav(editor.clone()) .set_nav(editor.clone())
// .set_diag(editor.read().unwrap().diag.clone()) // .set_diag(editor.read().unwrap().diag.clone())
@ -62,7 +72,7 @@ impl SumEditor {
pub fn select(&mut self, idx: usize) { pub fn select(&mut self, idx: usize) {
self.cur = idx; 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() ); tv.add_observer( self.port.get_cast() );
self.port.update_hooks.write().unwrap().clear(); self.port.update_hooks.write().unwrap().clear();
self.port.add_update_hook( Arc::new(tv.0.clone()) ); 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.update_hooks.write().unwrap().clear();
self.diag_port.add_update_hook( Arc::new(dv.0.clone()) ); self.diag_port.add_update_hook( Arc::new(dv.0.clone()) );
self.diag_port.set_view( Some(dv.get_view_arc()) ); 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 { fn goto(&mut self, new_cursor: TreeCursor) -> TreeNavResult {
self.editors[ self.cur ].goto( new_cursor ) 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 { impl PtySegment for SumEditor {
@ -100,26 +130,8 @@ impl PtySegment for SumEditor {
} }
} }
impl Commander for SumEditor { impl ObjCommander for SumEditor {
type Cmd = TerminalEvent; fn send_cmd_obj(&mut self, obj: Arc<RwLock<ReprTree>>) {
self.editors[ self.cur ].send_cmd_obj( obj );
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 );
}
}
} }
} }

View file

@ -1,17 +1,13 @@
use { use {
std::{sync::{Arc, RwLock}, any::Any}, std::{sync::{Arc, RwLock}, any::Any},
cgmath::Vector2, cgmath::{Vector2, Point2},
r3vi::{ r3vi::{
view::{ view::{View, ViewPort, OuterViewPort, AnyOuterViewPort, singleton::*, sequence::*},
ViewPort, OuterViewPort, AnyOuterViewPort,
singleton::*,
sequence::*
},
buffer::{singleton::*} buffer::{singleton::*}
}, },
crate::{ crate::{
type_system::{ReprTree, Context, TypeTerm}, type_system::{ReprTree, Context, TypeTerm},
terminal::{TerminalView, TerminalEvent, TerminalEditor, TerminalEditorResult}, terminal::{TerminalView, TerminalEvent, TerminalEditor, TerminalEditorResult, TerminalAtom},
diagnostics::{Diagnostics, Message}, diagnostics::{Diagnostics, Message},
tree::{TreeNav, TreeCursor, TreeNavResult}, tree::{TreeNav, TreeCursor, TreeNavResult},
editors::list::{ListCursorMode}, editors::list::{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 { pub fn set_ctx(mut self, ctx: Arc<RwLock<Context>>) -> Self {
self.ctx = Some(ctx); self.ctx = Some(ctx);
self self
@ -207,5 +228,36 @@ impl NestedNode {
pub fn morph(self, ty: TypeTerm) -> NestedNode { pub fn morph(self, ty: TypeTerm) -> NestedNode {
Context::morph_node(self, ty) 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)] #[derive(Hash, Eq, PartialEq, Debug)]
pub struct MorphismTypePattern { pub struct MorphismTypePattern {
pub src_type: Option<TypeTerm>, pub src_tyid: Option<TypeID>,
pub dst_tyid: TypeID pub dst_tyid: TypeID
} }
impl From<MorphismType> for MorphismTypePattern { impl From<MorphismType> for MorphismTypePattern {
fn from(value: MorphismType) -> MorphismTypePattern { fn from(value: MorphismType) -> MorphismTypePattern {
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 { dst_tyid: match value.dst_type {
TypeTerm::Type { id, args: _ } => id, TypeTerm::Type { id, args: _ } => id,
_ => unreachable!() _ => unreachable!()
@ -84,6 +88,12 @@ pub struct Context {
parent: Option<Arc<RwLock<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 { impl Context {
pub fn with_parent(parent: Option<Arc<RwLock<Context>>>) -> Self { pub fn with_parent(parent: Option<Arc<RwLock<Context>>>) -> Self {
Context { Context {
@ -154,7 +164,7 @@ impl Context {
}; };
let morphism_pattern = MorphismTypePattern { let morphism_pattern = MorphismTypePattern {
src_type: None, src_tyid: None,
dst_tyid: tyid dst_tyid: tyid
}; };

View file

@ -5,13 +5,12 @@ use {
char::*, char::*,
list::*, list::*,
integer::*, integer::*,
product::*, product::*
sum::*
}, },
tree::{NestedNode}, tree::{NestedNode},
terminal::{TerminalEditor}, terminal::{TerminalEditor},
diagnostics::{Diagnostics}, diagnostics::{Diagnostics},
type_system::{TypeTermEditor, MorphismTypePattern}, type_system::{MorphismTypePattern},
}, },
std::sync::{Arc, RwLock}, std::sync::{Arc, RwLock},
cgmath::Point2 cgmath::Point2
@ -28,7 +27,7 @@ pub fn init_mem_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
id: _, args id: _, args
} => { } => {
if args.len() > 0 { 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( let data = ReprTree::new_leaf(
ctx.read().unwrap().type_term_from_str("( Char )").unwrap(), ctx.read().unwrap().type_term_from_str("( Char )").unwrap(),
buf.get_port().into() 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()); ctx.write().unwrap().add_list_typename("Symbol".into());
let pattern = MorphismTypePattern { 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() dst_tyid: ctx.read().unwrap().get_typeid("Symbol").unwrap()
}; };
ctx.write().unwrap().add_morphism(pattern, 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()); ctx.write().unwrap().add_list_typename("String".into());
let pattern = MorphismTypePattern { 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() dst_tyid: ctx.read().unwrap().get_typeid("String").unwrap()
}; };
ctx.write().unwrap().add_morphism(pattern, 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_list_typename("TypeTerm".into());
ctx.write().unwrap().add_node_ctor( ctx.write().unwrap().add_node_ctor(
"TypeTerm", Arc::new( "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.write().unwrap().add_typename("TerminalEvent".into());
ctx 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()); ctx.write().unwrap().add_list_typename("PosInt".into());
let pattern = MorphismTypePattern { 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() dst_tyid: ctx.read().unwrap().get_typeid("PosInt").unwrap()
}; };
ctx.write().unwrap().add_morphism(pattern, 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 depth = node.depth;
let editor = node.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap(); let editor = node.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
// todo: check src_type parameter to be ( Digit radix )
match dst_type { match dst_type {
TypeTerm::Type { TypeTerm::Type {
id: _, args id: _, args
} => { } => {
if args.len() > 0 { if args.len() > 0 {
match args[0] { match args[0] {
TypeTerm::Num(radix) => { TypeTerm::Num(_radix) => {
let pty_editor = PTYListEditor::from_editor( let pty_editor = PTYListEditor::from_editor(
editor, editor,
Some(','), Some(','),

View file

@ -3,13 +3,13 @@ pub mod type_term;
pub mod repr_tree; pub mod repr_tree;
pub mod context; pub mod context;
pub mod make_editor; pub mod make_editor;
pub mod type_term_editor; //pub mod type_term_editor;
pub use { pub use {
repr_tree::{ReprTree}, repr_tree::{ReprTree},
type_term::{TypeDict, TypeID, TypeTerm, TypeLadder}, type_term::{TypeDict, TypeID, TypeTerm, TypeLadder},
context::{Context, MorphismMode, MorphismType, MorphismTypePattern}, context::{Context, MorphismMode, MorphismType, MorphismTypePattern},
type_term_editor::TypeTermEditor, // type_term_editor::TypeTermEditor,
make_editor::* make_editor::*
}; };

View file

@ -19,9 +19,9 @@ pub struct ReprTree {
} }
impl ReprTree { impl ReprTree {
pub fn new(type_tag: TypeTerm) -> Self { pub fn new(type_tag: impl Into<TypeTerm>) -> Self {
ReprTree { ReprTree {
type_tag, type_tag: type_tag.into(),
port: None, port: None,
branches: HashMap::new(), branches: HashMap::new(),
} }
@ -31,8 +31,8 @@ impl ReprTree {
&self.type_tag &self.type_tag
} }
pub fn new_leaf(type_tag: TypeTerm, port: AnyOuterViewPort) -> Arc<RwLock<Self>> { pub fn new_leaf(type_tag: impl Into<TypeTerm>, port: AnyOuterViewPort) -> Arc<RwLock<Self>> {
let mut tree = ReprTree::new(type_tag); let mut tree = ReprTree::new(type_tag.into());
tree.insert_leaf(vec![].into_iter(), port); tree.insert_leaf(vec![].into_iter(), port);
Arc::new(RwLock::new(tree)) Arc::new(RwLock::new(tree))
} }
@ -82,18 +82,22 @@ impl ReprTree {
.get_view() .get_view()
} }
pub fn descend(&self, dst_type: &TypeTerm) -> Option<Arc<RwLock<ReprTree>>> { pub fn descend(&self, dst_type: impl Into<TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
self.branches.get(dst_type).cloned() self.branches.get(&dst_type.into()).cloned()
} }
pub fn descend_ladder(&self, mut repr_ladder: impl Iterator<Item = TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> { pub fn descend_ladder(rt: &Arc<RwLock<Self>>, mut repr_ladder: impl Iterator<Item = TypeTerm>) -> Option<Arc<RwLock<ReprTree>>> {
let first = repr_ladder.next()?; if let Some(first) = repr_ladder.next() {
let rt = rt.read().unwrap();
repr_ladder.fold( repr_ladder.fold(
self.descend(&first), rt.descend(first),
|s, t| s?.read().unwrap().descend(&t)) |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); let mut n = Self::new(type_term);
n.insert_branch(rt.clone()); n.insert_branch(rt.clone());
Arc::new(RwLock::new(n)) Arc::new(RwLock::new(n))

View file

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

View file

@ -2,9 +2,7 @@ use {
crate::{ crate::{
type_system::{Context}, type_system::{Context},
terminal::{TerminalEvent}, terminal::{TerminalEvent},
editors::{ editors::{sum::*},
sum::*,
},
tree::{TreeNav}, tree::{TreeNav},
tree::NestedNode, tree::NestedNode,
commander::Commander, commander::Commander,
@ -37,9 +35,9 @@ impl TypeTermEditor {
ty: TypeTermVar::Any, ty: TypeTermVar::Any,
sum_edit: Arc::new(RwLock::new(SumEditor::new( sum_edit: Arc::new(RwLock::new(SumEditor::new(
vec![ vec![
Context::make_node( &ctx, ctx.read().unwrap().type_term_from_str("( List TypeTerm )").unwrap(), depth + 1).unwrap(), Context::make_node( &ctx, (&ctx, "( List TypeTerm )").into(), 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, "( PosInt 10 )").into(), depth + 1 ).unwrap(),
Context::make_node( &ctx, ctx.read().unwrap().type_term_from_str("( Symbol )").unwrap(), depth + 1 ).unwrap() Context::make_node( &ctx, (&ctx, "( Symbol )").into(), depth + 1 ).unwrap()
]))) ])))
} }
} }