move digit into separate module

This commit is contained in:
Michael Sippel 2024-03-10 16:17:24 +01:00
parent 77d9e64531
commit d7d0a46c7b
Signed by: senvas
GPG key ID: F96CF119C34B64A6
6 changed files with 16 additions and 193 deletions

View file

@ -29,7 +29,8 @@ async fn main() {
let ctx = Arc::new(RwLock::new(Context::new())); let ctx = Arc::new(RwLock::new(Context::new()));
nested::editors::char::init_ctx( ctx.clone() ); nested::editors::char::init_ctx( ctx.clone() );
nested::editors::integer::editor::init_ctx( ctx.clone() ); nested::editors::digit::init_ctx( ctx.clone() );
nested::editors::integer::init_ctx( ctx.clone() );
nested::editors::list::init_ctx( ctx.clone() ); nested::editors::list::init_ctx( ctx.clone() );
let char_type = Context::parse(&ctx, "Char"); let char_type = Context::parse(&ctx, "Char");
@ -85,9 +86,9 @@ async fn main() {
* / | \ * / | \
* / | \ * / | \
* / | \ * / | \
* u32 [ EditTree ] Char * u32 EditTree Char
* - Editor \ * - Editor \
* - Display [ EditTree ] * - Display EditTree
* / | \ - Editor * / | \ - Editor
* / | \ - Display * / | \ - Display
* TTY PixelBuf SDF / | \ * TTY PixelBuf SDF / | \

View file

@ -15,39 +15,8 @@ use {
std::sync::{Arc, RwLock} std::sync::{Arc, RwLock}
}; };
pub fn init_ctx(ctx: &mut Context) { pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
/* /*
ctx.add_typename("MachineInt".into());
ctx.add_typename("u32".into());
ctx.add_typename("u64".into());
ctx.add_typename("LittleEndian".into());
ctx.add_typename("BigEndian".into());
ctx.add_node_ctor(
"Digit", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>| {
match ty {
TypeTerm::App(args) => {
if args.len() > 1 {
match args[1] {
TypeTerm::Num(radix) => {
let node = DigitEditor::new(ctx.clone(), radix as u32).into_node(depth);
Some(
node
)
},
_ => None
}
} else {
None
}
}
_ => None
}
}
)
);
ctx.add_list_typename("PosInt".into()); ctx.add_list_typename("PosInt".into());
let pattern = MorphismTypePattern { let pattern = MorphismTypePattern {
src_tyid: ctx.get_typeid("List"), src_tyid: ctx.get_typeid("List"),
@ -128,6 +97,7 @@ pub fn init_ctx(ctx: &mut Context) {
) )
); );
*/ */
/*
ctx.add_typename("Date".into()); ctx.add_typename("Date".into());
ctx.add_typename("ISO-8601".into()); ctx.add_typename("ISO-8601".into());
ctx.add_typename("TimeSince".into()); ctx.add_typename("TimeSince".into());
@ -137,5 +107,6 @@ pub fn init_ctx(ctx: &mut Context) {
ctx.add_typename("Duration".into()); ctx.add_typename("Duration".into());
ctx.add_typename("Seconds".into()); ctx.add_typename("Seconds".into());
ctx.add_typename("".into()); ctx.add_typename("".into());
*/
} }

View file

@ -12,7 +12,11 @@ use {
}, },
laddertypes::{TypeTerm}, laddertypes::{TypeTerm},
crate::{ crate::{
editors::{list::{ListCmd}, ObjCommander}, editors::{
digit::DigitEditor,
list::{ListCmd},
ObjCommander
},
repr_tree::{Context, ReprTree}, repr_tree::{Context, ReprTree},
edit_tree::{EditTree, TreeNav, TreeNavResult, TreeCursor, diagnostics::{Message}}, edit_tree::{EditTree, TreeNav, TreeNavResult, TreeCursor, diagnostics::{Message}},
}, },
@ -22,160 +26,6 @@ use {
cgmath::{Point2} cgmath::{Point2}
}; };
pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
// todo: proper scoping of Radix variable
ctx.write().unwrap().add_varname("Radix");
let morphtype =
crate::repr_tree::MorphismType {
src_type: Context::parse(&ctx, "<Digit Radix>"),
dst_type: Context::parse(&ctx, "<Digit Radix>~EditTree")
};
ctx.write().unwrap()
.morphisms
.add_morphism(
morphtype,
{
let ctx = ctx.clone();
move |rt, σ| {
let radix =
match σ.get( &laddertypes::TypeID::Var(ctx.read().unwrap().get_var_typeid("Radix").unwrap()) ) {
Some(TypeTerm::Num(n)) => *n as u32,
_ => 0
};
/* Create EditTree object
*/
let mut edittree_digit = DigitEditor::new(
ctx.clone(),
radix
).into_node(
r3vi::buffer::singleton::SingletonBuffer::<usize>::new(0).get_port()
);
/* Insert EditTree into ReprTree
*/
let mut rt = rt.write().unwrap();
rt.insert_leaf(
vec![ Context::parse(&ctx, "EditTree") ].into_iter(),
SingletonBuffer::new( Arc::new(RwLock::new(edittree_digit)) ).get_port().into()
);
}
}
);
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct DigitEditor {
ctx: Arc<RwLock<Context>>,
radix: u32,
data: SingletonBuffer<Option<char>>,
msg: VecBuffer<Message>,
}
impl ObjCommander for DigitEditor {
fn send_cmd_obj(&mut self, cmd_obj: Arc<RwLock<ReprTree>>) -> TreeNavResult {
let cmd_obj = cmd_obj.read().unwrap();
let cmd_type = cmd_obj.get_type().clone();
if cmd_type == Context::parse(&self.ctx, "Char") {
if let Some(cmd_view) = cmd_obj.get_view::<dyn SingletonView<Item = char>>() {
let c = cmd_view.get();
self.msg.clear();
if self.ctx.read().unwrap().meta_chars.contains(&c) {
return TreeNavResult::Exit;
} else if c.to_digit(self.radix).is_none() {
/* in case the character c is not in the range of digit-chars,
add a message to the diagnostics view
*/
/*
let message = IndexBuffer::from_iter(vec![
(Point2::new(1, 0), make_label("invalid digit '")),
(Point2::new(2, 0), make_label(&format!("{}", c))
.map_item(|_p,a| a.add_style_back(TerminalStyle::fg_color((140,140,250))))),
(Point2::new(3, 0), make_label("'"))
]);
self.msg.push(crate::diagnostics::make_error(message.get_port().flatten()));
*/
self.data.set(Some(c));
} else {
self.data.set(Some(c));
}
}
}
TreeNavResult::Continue
}
}
impl DigitEditor {
pub fn new(ctx: Arc<RwLock<Context>>, radix: u32) -> Self {
DigitEditor {
ctx,
radix,
data: SingletonBuffer::new(None),
msg: VecBuffer::new(),
}
}
pub fn into_node(self, depth: OuterViewPort<dyn SingletonView<Item = usize>>) -> EditTree {
let data = self.get_data();
let editor = Arc::new(RwLock::new(self));
let ed = editor.write().unwrap();
let r = ed.radix;
EditTree::new(ed.ctx.clone(), depth)
.set_editor(editor.clone())
.set_cmd(editor.clone())
.set_diag(
ed.msg.get_port().to_sequence()
)
}
pub fn attach_to(&mut self, source: OuterViewPort<dyn SingletonView<Item = u32>>) {
/*
source.add_observer(
Arc::new(NotifyFnObserver::new(|_msg| {
self.data.set( source.get() )
}))
);
*/
}
pub fn get_data_port(&self) -> OuterViewPort<dyn SingletonView<Item = Result<u32, char>>> {
let radix = self.radix;
self.data.get_port().map(move |c|
if let Some(d) = c.unwrap_or('?').to_digit(radix) {
Ok(d)
} else {
Err(c.unwrap_or('?'))
}
)
}
pub fn get_type(&self) -> TypeTerm {
TypeTerm::TypeID(self.ctx.read().unwrap().get_typeid("Digit").unwrap())
}
pub fn get_data(&self) -> Arc<RwLock<ReprTree>> {
ReprTree::ascend(
&ReprTree::new_leaf(
self.ctx.read().unwrap().type_term_from_str("<Seq u32>").unwrap(),
self.get_data_port().into()
),
self.get_type()
)
}
}
pub struct PosIntEditor { pub struct PosIntEditor {
radix: u32, radix: u32,
digits: EditTree, digits: EditTree,

View file

@ -5,7 +5,7 @@ pub mod ctx;
pub use { pub use {
add::Add, add::Add,
editor::{DigitEditor, PosIntEditor}, editor::PosIntEditor,
radix::RadixProjection, radix::RadixProjection,
ctx::init_ctx ctx::init_ctx
}; };

View file

@ -4,6 +4,7 @@ pub mod list;
//pub mod sum; //pub mod sum;
pub mod char; pub mod char;
pub mod digit;
pub mod integer; pub mod integer;
//pub mod typeterm; //pub mod typeterm;

View file

@ -44,7 +44,7 @@ pub fn edittree_make_digit_view(
.write().unwrap() .write().unwrap()
.insert_branch(ReprTree::new_leaf( .insert_branch(ReprTree::new_leaf(
Context::parse(&node.ctx, "TerminalView"), Context::parse(&node.ctx, "TerminalView"),
node.get_edit::< nested::editors::integer::DigitEditor >() node.get_edit::< nested::editors::digit::DigitEditor >()
.unwrap() .unwrap()
.read() .read()
.unwrap() .unwrap()