TypeTermEditor: improve type names, still buggy editing

This commit is contained in:
Michael Sippel 2023-08-21 14:26:52 +02:00
parent 170dfcf01c
commit 4464a72f29
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -21,13 +21,13 @@ use {
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
enum State { enum State {
Any, Any,
App,
Ladder,
Char,
Num, Num,
Char,
AnySymbol, AnySymbol,
FunSymbol, FunSymbol,
VarSymbol, VarSymbol,
App,
Ladder,
} }
pub struct TypeTermEditor { pub struct TypeTermEditor {
@ -46,97 +46,101 @@ pub struct TypeTermEditor {
impl TypeTermEditor { impl TypeTermEditor {
pub fn init_ctx(ctx: &mut Context) { pub fn init_ctx(ctx: &mut Context) {
ctx.add_list_typename("TypeTerm".into()); ctx.add_list_typename("Type".into()); // = Lit | Sym | App | Ladder
ctx.add_list_typename("TypeSymbol".into()); ctx.add_list_typename("Type::Lit".into()); // = Num | char
ctx.add_list_typename("TypeLadder".into()); ctx.add_list_typename("Type::Lit::Num".into()); // [0-9]*
ctx.add_list_typename("TypeSymbol::Function".into()); ctx.add_list_typename("Type::Lit::Char".into()); // .
ctx.add_list_typename("TypeSymbol::Variable".into()); ctx.add_list_typename("Type::Sym".into()); // = Fun | Var
ctx.add_list_typename("TypeSymbol::Literal::Num".into()); ctx.add_list_typename("Type::Sym::Fun".into()); // [a-zA-Z][a-zA-Z0-9]*
ctx.add_list_typename("TypeSymbol::Literal::Char".into()); ctx.add_list_typename("Type::Sym::Var".into()); // [a-zA-Z][a-zA-Z0-9]*
ctx.add_list_typename("Type::App".into()); // = <T1 T2 ...>
ctx.add_list_typename("Type::Ladder".into()); // = T1~T2~...
ctx.add_morphism( ctx.add_morphism(
MorphismTypePattern { MorphismTypePattern { src_tyid: ctx.get_typeid("List"), dst_tyid: ctx.get_typeid("Type").unwrap() },
src_tyid: ctx.get_typeid("List"), Arc::new(move |mut node, _dst_type:_| {
dst_tyid: ctx.get_typeid("TypeSymbol").unwrap() let mut new_node = TypeTermEditor::with_node( node.ctx.clone(), node.depth.get(), node.clone(), State::Any );
}, Some(new_node)
Arc::new( }));
|mut node, _dst_type:_| {
ctx.add_morphism(
MorphismTypePattern { src_tyid: ctx.get_typeid("List"), dst_tyid: ctx.get_typeid("Type::Ladder").unwrap() },
Arc::new(|mut node, _dst_type: _| {
PTYListController::for_node( &mut node, Some('~'), None );
PTYListStyle::for_node( &mut node, ("","~","") );
Some(node)
}));
ctx.add_morphism(
MorphismTypePattern { src_tyid: ctx.get_typeid("List"), dst_tyid: ctx.get_typeid("Type::App").unwrap() },
Arc::new( |mut node, _dst_type: _| {
PTYListController::for_node( &mut node, Some(' '), Some('>') );
PTYListStyle::for_node( &mut node, ("<"," ",">") );
Some(node)
}));
ctx.add_morphism(
MorphismTypePattern { src_tyid: ctx.get_typeid("List"), dst_tyid: ctx.get_typeid("Type::Sym").unwrap() },
Arc::new(|mut node, _dst_type:_| {
PTYListController::for_node( &mut node, Some(' '), None );
PTYListStyle::for_node( &mut node, ("","","") );
Some(node)
}));
ctx.add_morphism(
MorphismTypePattern { src_tyid: ctx.get_typeid("List"), dst_tyid: ctx.get_typeid("Type::Sym::Fun").unwrap() },
Arc::new(|mut node, _dst_type:_| {
PTYListController::for_node( &mut node, Some(' '), None );
PTYListStyle::for_node( &mut node, ("","","") );
Some(node)
}));
ctx.add_morphism(
MorphismTypePattern { src_tyid: ctx.get_typeid("List"), dst_tyid: ctx.get_typeid("Type::Sym::Var").unwrap() },
Arc::new(|mut node, _dst_type:_| {
PTYListController::for_node( &mut node, Some(' '), None ); PTYListController::for_node( &mut node, Some(' '), None );
PTYListStyle::for_node( &mut node, ("","","") ); PTYListStyle::for_node( &mut node, ("","","") );
if let Some(v) = node.view { // display variables blue color
node.view = Some(
v.map_item(|i,p| p.add_style_front(TerminalStyle::fg_color((220, 220, 200)))));
}
Some(node)
}
)
);
ctx.add_morphism(
MorphismTypePattern {
src_tyid: ctx.get_typeid("List"),
dst_tyid: ctx.get_typeid("TypeSymbol::Function").unwrap()
},
Arc::new(
|mut node, _dst_type:_| {
PTYListController::for_node( &mut node, None, None );
PTYListStyle::for_node( &mut node, ("","","") );
if let Some(v) = node.view {
node.view = Some(
v.map_item(|i,p| p.add_style_front(TerminalStyle::fg_color((220, 220, 220)))));
}
Some(node)
}
)
);
ctx.add_morphism(
MorphismTypePattern {
src_tyid: ctx.get_typeid("List"),
dst_tyid: ctx.get_typeid("TypeSymbol::Variable").unwrap()
},
Arc::new(
|mut node, _dst_type:_| {
PTYListController::for_node( &mut node, None, None );
PTYListStyle::for_node( &mut node, ("","","") );
if let Some(v) = node.view { if let Some(v) = node.view {
node.view = Some( node.view = Some(
v.map_item(|i,p| p.add_style_front(TerminalStyle::fg_color((5, 120, 240))))); v.map_item(|i,p| p.add_style_front(TerminalStyle::fg_color((5, 120, 240)))));
} }
Some(node) Some(node)
} }));
)
);
ctx.add_node_ctor(
"TypeTerm", Arc::new(
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
Some(TypeTermEditor::new_node(ctx, depth))
}
)
);
ctx.add_morphism( ctx.add_morphism(
MorphismTypePattern { MorphismTypePattern { src_tyid: ctx.get_typeid("PosInt"), dst_tyid: ctx.get_typeid("Type::Lit::Num").unwrap() },
src_tyid: ctx.get_typeid("List"), Arc::new(|mut node, _dst_type:_| {
dst_tyid: ctx.get_typeid("TypeTerm").unwrap() Some(node)
}, }));
Arc::new(
move |mut node, _dst_type:_| { ctx.add_morphism(
eprintln!("morphism to typeterm"); MorphismTypePattern { src_tyid: ctx.get_typeid("List"), dst_tyid: ctx.get_typeid("Type::Lit::Char").unwrap() },
PTYListController::for_node( &mut node, Some(' '), None ); Arc::new(|mut node, _dst_type:_| {
PTYListStyle::for_node( &mut node, ("","","") ); let mut grid = r3vi::buffer::index_hashmap::IndexBuffer::new();
let mut new_node = TypeTermEditor::with_node( node.ctx.clone(), node.depth.get(), node.clone(), State::Any );
Some(new_node) grid.insert_iter(
} vec![
) (Point2::new(0,0), crate::terminal::make_label("'")),
(Point2::new(1,0), node.view.clone().unwrap()),
(Point2::new(2,0), crate::terminal::make_label("'")),
]
); );
node.close_char.set(Some('\''));
node.view = Some(
grid.get_port()
.flatten()
);
Some(node)
}));
ctx.add_node_ctor("Type", Arc::new(
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
Some(TypeTermEditor::new_node(ctx, depth))
}));
} }
pub fn from_type_term(ctx: Arc<RwLock<Context>>, depth: usize, term: &TypeTerm) -> NestedNode { pub fn from_type_term(ctx: Arc<RwLock<Context>>, depth: usize, term: &TypeTerm) -> NestedNode {
@ -222,93 +226,42 @@ impl TypeTermEditor {
fn set_state(&mut self, new_state: State) { fn set_state(&mut self, new_state: State) {
let old_node = self.cur_node.get(); let old_node = self.cur_node.get();
let mut node = match new_state { let mut node = match new_state {
State::Char => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( Char )").into(), 0 ).unwrap();
let mut grid = r3vi::buffer::index_hashmap::IndexBuffer::new();
grid.insert_iter(
vec![
(Point2::new(0,0), crate::terminal::make_label("'")),
(Point2::new(1,0), node.view.clone().unwrap()),
(Point2::new(2,0), crate::terminal::make_label("'")),
]
);
node.close_char.set(Some('\''));
node.view = Some(
grid.get_port()
.flatten()
);
self.data.write().unwrap().insert_leaf(
vec![].into_iter(),
node.data.read().unwrap()
.get_port::<dyn SingletonView<Item = char>>().unwrap()
.map(
|c| TypeTerm::Char(c)
)
.into()
);
node
}
State::App => { State::App => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List TypeTerm )").into(), 0 ).unwrap(); let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), 0 ).unwrap();
node = node.morph( (&self.ctx, "( Type::App )").into() );
PTYListController::for_node( &mut node, Some(' '), Some('>') );
PTYListStyle::for_node( &mut node, ("<"," ",">") );
self.data.write().unwrap().insert_leaf(
vec![].into_iter(),
node.data.read().unwrap()
.get_port::<dyn SequenceView<Item = NestedNode>>().unwrap()
.map(
|node| {
node.data.read().unwrap().get_port::<dyn SingletonView<Item = TypeTerm>>().unwrap()
}
)
.into()
);
node node
} }
State::Ladder => { State::Ladder => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List TypeTerm )").into(), 0 ).unwrap(); let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Type )").into(), 0 ).unwrap();
node = node.morph( (&self.ctx, "( Type::Ladder )").into() );
PTYListController::for_node( &mut node, Some('~'), None );
PTYListStyle::for_node( &mut node, ("","~","") );
self.data.write().unwrap().insert_leaf(
vec![].into_iter(),
node.data.read().unwrap()
.get_port::<dyn SequenceView<Item = NestedNode>>().unwrap()
.map(
|node| {
node.data.read().unwrap().get_port::<dyn SingletonView<Item = TypeTerm>>().unwrap()
}
)
.into()
);
node node
} }
State::AnySymbol => { State::AnySymbol => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), 0 ).unwrap(); let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), 0 ).unwrap();
node = node.morph( (&self.ctx, "( TypeSymbol )").into() ); node = node.morph( (&self.ctx, "( Type::Sym )").into() );
node node
}, },
State::FunSymbol => { State::FunSymbol => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), 0 ).unwrap(); let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), 0 ).unwrap();
node = node.morph( (&self.ctx, "( TypeSymbol::Function )").into() ); node = node.morph( (&self.ctx, "( Type::Sym::Fun )").into() );
node node
}, },
State::VarSymbol => { State::VarSymbol => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), 0 ).unwrap(); let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List Char )").into(), 0 ).unwrap();
node = node.morph( (&self.ctx, "( TypeSymbol::Variable )").into() ); node = node.morph( (&self.ctx, "( Type::Sym::Var )").into() );
node node
} }
State::Num => { State::Num => {
Context::make_node( &self.ctx, (&self.ctx, "( PosInt 10 BigEndian )").into(), 0 ).unwrap() let mut int_edit = crate::editors::integer::PosIntEditor::new(self.ctx.clone(), 10);
let mut node = int_edit.into_node();
node = node.morph( (&self.ctx, "( Type::Lit::Num )").into() );
node
}
State::Char => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( Char )").into(), 0 ).unwrap();
node = node.morph( (&self.ctx, "( Type::Lit::Char )").into() );
node
} }
_ => { _ => {
old_node old_node
@ -317,14 +270,18 @@ impl TypeTermEditor {
node.goto(TreeCursor::home()); node.goto(TreeCursor::home());
self.editor.set(node.editor.get()); let editor = node.editor.get();
eprintln!("set_state:editor = {:?}", Arc::into_raw(editor.clone().unwrap()));
self.editor.set(editor);
self.cur_node.set(node); self.cur_node.set(node);
self.state = new_state; self.state = new_state;
} }
pub fn new_node(ctx: Arc<RwLock<Context>>, depth: usize) -> NestedNode { pub fn new_node(ctx: Arc<RwLock<Context>>, depth: usize) -> NestedNode {
let mut symb_node = Context::make_node( &ctx, (&ctx, "( List Char )").into(), 0 ).unwrap(); let mut symb_node = Context::make_node( &ctx, (&ctx, "( List Char )").into(), 0 ).unwrap();
symb_node = symb_node.morph( (&ctx, "( TypeSymbol::Variable )").into() ); symb_node = symb_node.morph( (&ctx, "( Type::Sym )").into() );
Self::with_node( Self::with_node(
ctx.clone(), ctx.clone(),
@ -338,7 +295,7 @@ impl TypeTermEditor {
let buffer = SingletonBuffer::<Option<TypeTerm>>::new( None ); let buffer = SingletonBuffer::<Option<TypeTerm>>::new( None );
let data = Arc::new(RwLock::new(ReprTree::new( let data = Arc::new(RwLock::new(ReprTree::new(
(&ctx, "( TypeTerm )") (&ctx, "( Type )")
))); )));
let mut editor = TypeTermEditor { let mut editor = TypeTermEditor {
@ -402,7 +359,7 @@ impl TypeTermEditor {
(&ctx, "( MachineInt )").into() (&ctx, "( MachineInt )").into()
] ]
); );
*/ */
Some(TypeTerm::new(TypeID::Fun(0))) Some(TypeTerm::new(TypeID::Fun(0)))
}, },
State::App => { State::App => {
@ -456,9 +413,8 @@ impl ObjCommander for TypeTermEditor {
fn send_cmd_obj(&mut self, co: Arc<RwLock<ReprTree>>) -> TreeNavResult { fn send_cmd_obj(&mut self, co: Arc<RwLock<ReprTree>>) -> TreeNavResult {
let cmd_obj = co.clone(); let cmd_obj = co.clone();
let cmd_obj = cmd_obj.read().unwrap(); let cmd_obj = cmd_obj.read().unwrap();
let cmd_type = cmd_obj.get_type().clone();
if cmd_type == (&self.ctx, "( Char )").into() { if cmd_obj.get_type().clone() == (&self.ctx, "( Char )").into() {
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>>() {
let c = cmd_view.get(); let c = cmd_view.get();
@ -478,6 +434,9 @@ impl ObjCommander for TypeTermEditor {
self.set_state( State::Char ); self.set_state( State::Char );
TreeNavResult::Continue TreeNavResult::Continue
} }
'~' => {
TreeNavResult::Exit
}
_ => { _ => {
self.set_state( State::AnySymbol ); self.set_state( State::AnySymbol );
self.cur_node.get_mut().goto(TreeCursor::home()); self.cur_node.get_mut().goto(TreeCursor::home());
@ -498,8 +457,32 @@ impl ObjCommander for TypeTermEditor {
} }
} }
// State::App | State::AnySymbol | State::FunSymbol | State::VarSymbol
_ => { _ => {
self.cur_node.get_mut().send_cmd_obj( co ) match self.cur_node.get_mut().send_cmd_obj( co ) {
TreeNavResult::Exit => {
match c {
'~' => {
// todo:
// in case previous item is not ladder
// create new intermediary ladder-node
// with the previous item as one step
// in case previous item is ladder
// goto end
eprintln!("TypeEdit: child exit ~");
}
_ => {}
}
TreeNavResult::Exit
}
TreeNavResult::Continue => {
TreeNavResult::Continue
}
}
} }
} }
} else { } else {
@ -509,22 +492,14 @@ impl ObjCommander for TypeTermEditor {
match &self.state { match &self.state {
State::Any => { State::Any => {
eprintln!("undefined comd object set to listl"); eprintln!("undefined comd object set to listl");
self.set_state( State::App ); self.set_state( State::Ladder );
self.cur_node.get_mut().goto(TreeCursor::home()); self.cur_node.get_mut().goto(TreeCursor::home());
let res = self.cur_node.get().cmd.get().unwrap().write().unwrap().send_cmd_obj( co );
self.cur_node.get_mut().goto(TreeCursor::none());
res
} }
_ => {}
}
match self.state {
State::App => {
self.cur_node.get().send_cmd_obj( co )
},
State::Ladder => {
self.cur_node.get().send_cmd_obj( co )
},
_ => { _ => {
eprintln!("undefined cmd object"); self.cur_node.get().cmd.get().unwrap().write().unwrap().send_cmd_obj( co )
TreeNavResult::Exit
} }
} }
} }