int example & testing out more int projections
This commit is contained in:
parent
91e56d876d
commit
40a5da4512
13 changed files with 661 additions and 151 deletions
|
@ -5,5 +5,6 @@ members = [
|
|||
"examples/tty-01-hello",
|
||||
"examples/tty-02-digit",
|
||||
"examples/tty-03-string",
|
||||
"examples/tty-04-posint"
|
||||
]
|
||||
|
||||
|
|
19
examples/tty-04-posint/Cargo.toml
Normal file
19
examples/tty-04-posint/Cargo.toml
Normal file
|
@ -0,0 +1,19 @@
|
|||
[package]
|
||||
name = "tty-04-posint"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
laddertypes = { path = "../../../lib-laddertypes" }
|
||||
r3vi = { path = "../../../lib-r3vi" }
|
||||
nested = { path = "../../lib-nested-core" }
|
||||
nested-tty = { path = "../../lib-nested-tty" }
|
||||
termion = "*"
|
||||
cgmath = "*"
|
||||
|
||||
[dependencies.async-std]
|
||||
version = "1.9.0"
|
||||
features = ["unstable", "attributes"]
|
||||
|
7
examples/tty-04-posint/README.md
Normal file
7
examples/tty-04-posint/README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# tty-04-posint
|
||||
|
||||
In the following example, a <List Char> editor
|
||||
as before is used, but its data is morphed into
|
||||
representing a positional integer which is then
|
||||
projected into different radices and displayed
|
||||
in different views on screen
|
208
examples/tty-04-posint/src/main.rs
Normal file
208
examples/tty-04-posint/src/main.rs
Normal file
|
@ -0,0 +1,208 @@
|
|||
//! In the following example, a <List Char> editor
|
||||
//! as before is used, but its data is morphed into
|
||||
//! representing a positional integer which is then
|
||||
//! projected into different radices and displayed
|
||||
//! in different views on screen
|
||||
|
||||
extern crate cgmath;
|
||||
extern crate nested;
|
||||
extern crate nested_tty;
|
||||
extern crate r3vi;
|
||||
extern crate termion;
|
||||
|
||||
use {
|
||||
cgmath::Vector2,
|
||||
nested::{
|
||||
editors::{
|
||||
ObjCommander
|
||||
},
|
||||
repr_tree::{Context, ReprTree, ReprTreeExt, ReprLeaf},
|
||||
edit_tree::{EditTree}
|
||||
},
|
||||
nested_tty::{
|
||||
DisplaySegment, TTYApplication,
|
||||
TerminalCompositor, TerminalStyle, TerminalView,
|
||||
TerminalAtom, TerminalEvent
|
||||
},
|
||||
r3vi::{
|
||||
buffer::{singleton::*, vec::*},
|
||||
view::{port::UpdateTask, list::*, sequence::*},
|
||||
projection::*
|
||||
},
|
||||
std::sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
/* setup context
|
||||
*/
|
||||
let ctx = Arc::new(RwLock::new(Context::new()));
|
||||
nested::editors::char::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_tty::setup_edittree_hook(&ctx);
|
||||
|
||||
/* Create a Representation-Tree of type <List <Digit 16>>
|
||||
*/
|
||||
let mut rt_digitlist = ReprTree::new_arc( Context::parse(&ctx, "<List <Digit 16>>") );
|
||||
let mut rt_digitseq = ReprTree::new_arc( Context::parse(&ctx, "<Seq <Digit 16>>") );
|
||||
rt_digitseq.insert_branch( rt_digitlist );
|
||||
let mut rt_posint = ReprTree::new_arc(Context::parse(&ctx, "<PosInt 16 BigEndian>"));
|
||||
rt_posint.insert_branch( rt_digitseq );
|
||||
let mut rt_int = ReprTree::new_arc( Context::parse(&ctx, "ℕ") );
|
||||
rt_int.insert_branch( rt_posint );
|
||||
|
||||
/* Setup an Editor for this ReprTree
|
||||
* (this will add the representation <List <Digit 16>>~EditTree to the ReprTree)
|
||||
*/
|
||||
let rt_edittree_list = ctx.read().unwrap()
|
||||
.setup_edittree(
|
||||
ReprTree::descend(
|
||||
&rt_int,
|
||||
Context::parse(&ctx, "<PosInt 16 BigEndian>~<Seq~List <Digit 16>>")
|
||||
).expect("cant descend reprtree"),
|
||||
SingletonBuffer::new(0).get_port()
|
||||
);
|
||||
|
||||
ctx.read().unwrap().morphisms.apply_morphism(
|
||||
ReprTree::descend(&rt_int,
|
||||
Context::parse(&ctx, "
|
||||
<PosInt 16 BigEndian>
|
||||
~<Seq <Digit 16>>
|
||||
~<List <Digit 16>>
|
||||
")
|
||||
).expect("cant descend repr tree"),
|
||||
&Context::parse(&ctx, "<List <Digit 16>>~EditTree"),
|
||||
&Context::parse(&ctx, "<List <Digit 16>~Char>")
|
||||
);
|
||||
|
||||
/*
|
||||
* map seq of chars to seq of u64 digits
|
||||
*/
|
||||
let mut chars_view =
|
||||
ReprTree::descend(
|
||||
&rt_int,
|
||||
Context::parse(&ctx, "<PosInt 16 BigEndian>~<Seq <Digit 16>>~<List <Digit 16>~Char>")
|
||||
).expect("cant descend")
|
||||
.read().unwrap()
|
||||
.get_port::<dyn ListView<char>>()
|
||||
.unwrap();
|
||||
|
||||
let mut digits_view = chars_view
|
||||
.to_sequence()
|
||||
.filter_map(
|
||||
|digit_char|
|
||||
|
||||
/* TODO: call morphism
|
||||
*/
|
||||
match digit_char.to_digit(16) {
|
||||
Some(d) => Some(d as usize),
|
||||
None => None
|
||||
}
|
||||
);
|
||||
|
||||
rt_int.write().unwrap().insert_leaf(
|
||||
vec![
|
||||
Context::parse(&ctx, "<PosInt 16 BigEndian>"),
|
||||
Context::parse(&ctx, "<Seq <Digit 16>>"),
|
||||
Context::parse(&ctx, "<Seq ℤ_2^64>"),
|
||||
Context::parse(&ctx, "<Seq machine.UInt64>")
|
||||
].into_iter(),
|
||||
nested::repr_tree::ReprLeaf::from_view( digits_view.clone() )
|
||||
);
|
||||
//
|
||||
//
|
||||
|
||||
ctx.read().unwrap().morphisms.apply_morphism(
|
||||
rt_int.clone(),
|
||||
&Context::parse(&ctx, "ℕ ~ <PosInt 16 BigEndian> ~ <Seq <Digit 16>~ℤ_2^64~machine.UInt64>"),
|
||||
&Context::parse(&ctx, "ℕ ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>~ℤ_2^64~machine.UInt64>")
|
||||
);
|
||||
ctx.read().unwrap().morphisms.apply_morphism(
|
||||
rt_int.clone(),
|
||||
&Context::parse(&ctx, "ℕ ~ <PosInt 16 LittleEndian> ~ <Seq <Digit 16>~ℤ_2^64~machine.UInt64>"),
|
||||
&Context::parse(&ctx, "ℕ ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>~ℤ_2^64~machine.UInt64>")
|
||||
);
|
||||
ctx.read().unwrap().morphisms.apply_morphism(
|
||||
rt_int.clone(),
|
||||
&Context::parse(&ctx, "ℕ ~ <PosInt 10 LittleEndian> ~ <Seq <Digit 10>~ℤ_2^64~machine.UInt64>"),
|
||||
&Context::parse(&ctx, "ℕ ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10>~ℤ_2^64~machine.UInt64>")
|
||||
);
|
||||
|
||||
let dec_digits_view = ReprTree::descend(&rt_int,
|
||||
Context::parse(&ctx, "
|
||||
<PosInt 10 BigEndian>
|
||||
~< Seq <Digit 10>~ℤ_2^64~machine.UInt64 >
|
||||
")
|
||||
).expect("cant descend repr tree")
|
||||
.read().unwrap()
|
||||
.get_port::<dyn SequenceView<Item = usize>>().unwrap()
|
||||
.map(
|
||||
/* TODO: call morphism
|
||||
*/
|
||||
|digit| {
|
||||
TerminalAtom::from(
|
||||
char::from_digit(*digit as u32, 10)
|
||||
)
|
||||
}
|
||||
)
|
||||
.to_grid_horizontal();
|
||||
|
||||
let hex_digits_view =
|
||||
ReprTree::descend(
|
||||
&rt_int,
|
||||
Context::parse(&ctx, "
|
||||
<PosInt 16 BigEndian>
|
||||
~<Seq <Digit 16> >
|
||||
~<List <Digit 16>
|
||||
~Char>")
|
||||
).expect("cant descend")
|
||||
.read().unwrap()
|
||||
.get_port::<dyn ListView<char>>().unwrap()
|
||||
.to_sequence()
|
||||
.to_grid_horizontal()
|
||||
.map_item(|_pt,c| TerminalAtom::new(*c, TerminalStyle::fg_color((30,90,200))));
|
||||
|
||||
/* setup terminal
|
||||
*/
|
||||
let app = TTYApplication::new({
|
||||
let edittree_list = rt_edittree_list.clone();
|
||||
|
||||
/* event handler
|
||||
*/
|
||||
let ctx = ctx.clone();
|
||||
move |ev| {
|
||||
edittree_list.get().send_cmd_obj(ev.to_repr_tree(&ctx));
|
||||
}
|
||||
});
|
||||
|
||||
/* Setup the compositor to serve as root-view
|
||||
* by routing it to the `app.port` Viewport,
|
||||
* so it will be displayed on TTY-output.
|
||||
*/
|
||||
let compositor = TerminalCompositor::new(app.port.inner());
|
||||
|
||||
/* Now add some views to our compositor
|
||||
*/
|
||||
{
|
||||
let mut comp = compositor.write().unwrap();
|
||||
|
||||
let label_str = ctx.read().unwrap().type_term_to_str(&rt_int.read().unwrap().get_type());
|
||||
comp.push(
|
||||
nested_tty::make_label(&label_str)
|
||||
.map_item(|_pt, atom| atom.add_style_front(TerminalStyle::fg_color((90,90,90))))
|
||||
.offset(Vector2::new(1,1)));
|
||||
|
||||
comp.push(rt_edittree_list.get()
|
||||
.display_view()
|
||||
.offset(Vector2::new(3,2)));
|
||||
|
||||
comp.push(dec_digits_view.offset(Vector2::new(3,4)));
|
||||
comp.push(hex_digits_view.offset(Vector2::new(3,5)));
|
||||
}
|
||||
|
||||
/* write the changes in the view of `term_port` to the terminal
|
||||
*/
|
||||
app.show().await.expect("output error!");
|
||||
}
|
|
@ -36,6 +36,7 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
|
|||
if let Some(buf) = rt.singleton_buffer::<char>() {
|
||||
// buffer already exists
|
||||
} else {
|
||||
// create char buffer
|
||||
rt.insert_leaf(
|
||||
vec![].into_iter(),
|
||||
ReprLeaf::from_singleton_buffer(
|
||||
|
|
|
@ -80,7 +80,7 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
|
|||
let morphtype =
|
||||
crate::repr_tree::MorphismType {
|
||||
src_type: Context::parse(&ctx, "<Digit Radix>~Char"),
|
||||
dst_type: Context::parse(&ctx, "<Digit Radix>~ℤ_256~machine::UInt8")
|
||||
dst_type: Context::parse(&ctx, "<Digit Radix>~ℤ_2^64~machine.UInt64")
|
||||
};
|
||||
|
||||
ctx.write().unwrap()
|
||||
|
@ -124,7 +124,7 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
|
|||
|
||||
let morphtype =
|
||||
crate::repr_tree::MorphismType {
|
||||
src_type: Context::parse(&ctx, "<Digit Radix>~ℤ_256~machine::UInt8"),
|
||||
src_type: Context::parse(&ctx, "<Digit Radix>~ℤ_2^64~machine.UInt64"),
|
||||
dst_type: Context::parse(&ctx, "<Digit Radix>~Char")
|
||||
};
|
||||
|
||||
|
@ -144,10 +144,10 @@ pub fn init_ctx( ctx: Arc<RwLock<Context>> ) {
|
|||
/* insert projected view into ReprTree
|
||||
*/
|
||||
let char_view =
|
||||
rt.descend(Context::parse(&ctx, "ℤ_256~machine::UInt8"))
|
||||
rt.descend(Context::parse(&ctx, "ℤ_2^64~machine::UInt64"))
|
||||
.unwrap()
|
||||
.view_u8()
|
||||
.map(move |digit| char::from_digit(digit as u32, radix).unwrap_or('?'));
|
||||
.view_usize()
|
||||
.map(move |digit| char::from_digit((digit%radix as usize) as u32, radix).unwrap_or('?'));
|
||||
|
||||
rt.write().unwrap().attach_leaf_to::<dyn SingletonView<Item = char>>(
|
||||
Context::parse(&ctx, "Char").get_lnf_vec().into_iter(),
|
||||
|
|
|
@ -73,6 +73,11 @@ impl DigitEditor {
|
|||
pub fn get_type(&self) -> TypeTerm {
|
||||
TypeTerm::TypeID(self.ctx.read().unwrap().get_typeid("Digit").unwrap())
|
||||
}
|
||||
|
||||
pub fn get_char(&self) -> char {
|
||||
self.data.get()
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn get_data(&self) -> Arc<RwLock<ReprTree>> {
|
||||
ReprTree::ascend(
|
||||
|
|
|
@ -5,8 +5,7 @@ use {
|
|||
},
|
||||
laddertypes::{TypeTerm},
|
||||
crate::{
|
||||
repr_tree::{Context},
|
||||
repr_tree::{MorphismType},
|
||||
repr_tree::{ReprTree, ReprLeaf, Context, MorphismType},
|
||||
editors::{
|
||||
list::*,
|
||||
integer::*
|
||||
|
@ -16,97 +15,142 @@ use {
|
|||
};
|
||||
|
||||
pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
|
||||
/*
|
||||
ctx.add_list_typename("PosInt".into());
|
||||
let pattern = MorphismTypePattern {
|
||||
src_tyid: ctx.get_typeid("List"),
|
||||
dst_tyid: ctx.get_typeid("PosInt").unwrap()
|
||||
// TODO: proper scoping
|
||||
// ctx.write().unwrap().add_varname("Radix");
|
||||
ctx.write().unwrap().add_varname("SrcRadix");
|
||||
ctx.write().unwrap().add_varname("DstRadix");
|
||||
|
||||
let morphism_type = MorphismType {
|
||||
src_type: Context::parse(&ctx, "ℕ ~ <PosInt Radix BigEndian> ~ <Seq <Digit SrcRadix>~ℤ_2^64~machine.UInt64>"),
|
||||
dst_type: Context::parse(&ctx, "ℕ ~ <PosInt Radix LittleEndian> ~ <Seq <Digit DstRadix>~ℤ_2^64~machine.UInt64>")
|
||||
};
|
||||
ctx.add_morphism(pattern,
|
||||
Arc::new(
|
||||
|mut node, dst_type| {
|
||||
// todo: check src_type parameter to be ( Digit radix )
|
||||
|
||||
match dst_type {
|
||||
TypeTerm::App(args) => {
|
||||
if args.len() > 1 {
|
||||
match args[1] {
|
||||
TypeTerm::Num(_radix) => {
|
||||
/* FIXME
|
||||
PTYListController::for_node(
|
||||
&mut node,
|
||||
Some(','),
|
||||
None,
|
||||
);
|
||||
ctx.write().unwrap().morphisms.add_morphism(
|
||||
morphism_type,
|
||||
{
|
||||
let ctx = ctx.clone();
|
||||
move |src_rt, σ| {
|
||||
let src_digits = ReprTree::descend(
|
||||
&src_rt,
|
||||
Context::parse(&ctx, "
|
||||
<PosInt Radix BigEndian>
|
||||
~<Seq <Digit Radix>~ℤ_2^64~machine.UInt64 >
|
||||
")
|
||||
.apply_substitution(&|k|σ.get(k).cloned()).clone()
|
||||
).expect("cant descend")
|
||||
.read().unwrap()
|
||||
.view_seq::< usize >();
|
||||
|
||||
PTYListStyle::for_node(
|
||||
&mut node,
|
||||
("0d", "", "")
|
||||
);
|
||||
*/
|
||||
Some(node)
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
src_rt.write().unwrap().insert_leaf(
|
||||
vec![
|
||||
Context::parse(&ctx, "<PosInt Radix LittleEndian>")
|
||||
.apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
||||
Context::parse(&ctx, "<Seq <Digit Radix>>")
|
||||
.apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
||||
Context::parse(&ctx, "<Seq ℤ_2^64>"),
|
||||
Context::parse(&ctx, "<Seq machine.UInt64>")
|
||||
].into_iter(),
|
||||
|
||||
ReprLeaf::from_view( src_digits.reverse() )
|
||||
);
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
ctx.add_node_ctor(
|
||||
"PosInt", Arc::new(
|
||||
|ctx0: Arc<RwLock<Context>>, dst_typ: TypeTerm, depth: OuterViewPort<dyn SingletonView<Item = usize>>| {
|
||||
match dst_typ.clone() {
|
||||
TypeTerm::App(args) => {
|
||||
if args.len() > 1 {
|
||||
match args[1] {
|
||||
TypeTerm::Num(radix) => {
|
||||
let ctx = ctx0.read().unwrap();
|
||||
let mut node = Context::make_node(
|
||||
&ctx0,
|
||||
TypeTerm::App(vec![
|
||||
TypeTerm::TypeID(ctx.get_typeid("List").unwrap()),
|
||||
TypeTerm::TypeID(
|
||||
ctx.get_typeid("Digit").unwrap()
|
||||
)
|
||||
.num_arg(radix)
|
||||
.clone()
|
||||
.into()
|
||||
]),
|
||||
depth.map(|d| d+1)
|
||||
).unwrap();
|
||||
|
||||
node = node.morph(dst_typ);
|
||||
|
||||
Some(node)
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
|
||||
let morphism_type = MorphismType {
|
||||
src_type: Context::parse(&ctx, "ℕ ~ <PosInt Radix LittleEndian> ~ <Seq <Digit SrcRadix>~ℤ_2^64~machine.UInt64>"),
|
||||
dst_type: Context::parse(&ctx, "ℕ ~ <PosInt Radix BigEndian> ~ <Seq <Digit DstRadix>~ℤ_2^64~machine.UInt64>")
|
||||
};
|
||||
|
||||
ctx.write().unwrap().morphisms.add_morphism(
|
||||
morphism_type,
|
||||
{
|
||||
let ctx = ctx.clone();
|
||||
move |src_rt, σ| {
|
||||
let src_digits = ReprTree::descend(
|
||||
&src_rt,
|
||||
Context::parse(&ctx, "
|
||||
<PosInt Radix LittleEndian>
|
||||
~<Seq <Digit Radix>~ℤ_2^64~machine.UInt64 >
|
||||
")
|
||||
.apply_substitution(&|k|σ.get(k).cloned()).clone()
|
||||
).expect("cant descend")
|
||||
.read().unwrap()
|
||||
.view_seq::< usize >();
|
||||
|
||||
src_rt.write().unwrap().insert_leaf(
|
||||
vec![
|
||||
Context::parse(&ctx, "<PosInt Radix BigEndian>")
|
||||
.apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
||||
Context::parse(&ctx, "<Seq <Digit Radix>>")
|
||||
.apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
||||
Context::parse(&ctx, "<Seq ℤ_2^64>"),
|
||||
Context::parse(&ctx, "<Seq machine.UInt64>")
|
||||
].into_iter(),
|
||||
|
||||
ReprLeaf::from_view( src_digits.reverse() )
|
||||
);
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
let morphism_type = MorphismType {
|
||||
src_type: Context::parse(&ctx, "ℕ ~ <PosInt SrcRadix LittleEndian> ~ <Seq <Digit SrcRadix>~ℤ_2^64~machine.UInt64>"),
|
||||
dst_type: Context::parse(&ctx, "ℕ ~ <PosInt DstRadix LittleEndian> ~ <Seq <Digit DstRadix>~ℤ_2^64~machine.UInt64>")
|
||||
};
|
||||
|
||||
ctx.write().unwrap().morphisms.add_morphism(
|
||||
morphism_type,
|
||||
{
|
||||
let ctx = ctx.clone();
|
||||
move |src_rt, σ| {
|
||||
let src_radix = match σ.get(&laddertypes::TypeID::Var(
|
||||
ctx.read().unwrap().get_var_typeid("SrcRadix").unwrap()
|
||||
)) {
|
||||
Some(laddertypes::TypeTerm::Num(n)) => *n as usize,
|
||||
_ => 0
|
||||
};
|
||||
|
||||
let dst_radix = match σ.get(&laddertypes::TypeID::Var(
|
||||
ctx.read().unwrap().get_var_typeid("DstRadix").unwrap()
|
||||
)) {
|
||||
Some(laddertypes::TypeTerm::Num(n)) => *n as usize,
|
||||
_ => 0
|
||||
};
|
||||
|
||||
let src_digits_rt = ReprTree::descend(
|
||||
src_rt,
|
||||
Context::parse(&ctx, "
|
||||
<PosInt SrcRadix LittleEndian>
|
||||
~ <Seq <Digit SrcRadix> ~ ℤ_2^64 ~ machine.UInt64 >"
|
||||
).apply_substitution(&|k|σ.get(k).cloned()).clone()
|
||||
).expect("cant descend repr tree");
|
||||
|
||||
let dst_digits_port =
|
||||
src_digits_rt.read().unwrap()
|
||||
.view_seq::<usize>()
|
||||
.to_positional_uint( src_radix )
|
||||
.transform_radix( dst_radix )
|
||||
;
|
||||
|
||||
src_rt.write().unwrap()
|
||||
.insert_leaf(
|
||||
vec![
|
||||
Context::parse(&ctx, "<PosInt DstRadix LittleEndian>").apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
||||
Context::parse(&ctx, "<Seq <Digit DstRadix>>").apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
||||
Context::parse(&ctx, "<Seq ℤ_2^64>"),
|
||||
Context::parse(&ctx, "<Seq machine.UInt64>"),
|
||||
].into_iter(),
|
||||
ReprLeaf::from_view(dst_digits_port)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
*/
|
||||
/*
|
||||
ctx.add_typename("Date".into());
|
||||
ctx.add_typename("ISO-8601".into());
|
||||
ctx.add_typename("TimeSince".into());
|
||||
ctx.add_typename("UnixEpoch".into());
|
||||
ctx.add_typename("AnnoDomini".into());
|
||||
ctx.add_typename("Epoch".into());
|
||||
ctx.add_typename("Duration".into());
|
||||
ctx.add_typename("Seconds".into());
|
||||
ctx.add_typename("ℕ".into());
|
||||
*/
|
||||
}
|
||||
|
||||
|
|
|
@ -10,3 +10,139 @@ pub use {
|
|||
ctx::init_ctx
|
||||
};
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||
|
||||
use {
|
||||
r3vi::{
|
||||
view::{
|
||||
View, ViewPort, OuterViewPort,
|
||||
Observer,
|
||||
ObserverBroadcast,
|
||||
sequence::*
|
||||
}
|
||||
},
|
||||
crate::{
|
||||
editors::integer::radix::{
|
||||
PosIntProjections
|
||||
}
|
||||
},
|
||||
std::sync::{Arc, RwLock}
|
||||
};
|
||||
|
||||
pub trait PositionalUInt : SequenceView<Item = usize> {
|
||||
fn get_radix(&self) -> usize;
|
||||
fn get_value(&self) -> usize {
|
||||
let mut val = 0;
|
||||
let mut r = 1;
|
||||
for i in 0..self.len().unwrap_or(0) {
|
||||
val += r * self.get(&i).unwrap();
|
||||
r *= self.get_radix();
|
||||
}
|
||||
|
||||
val
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: PositionalUInt> PositionalUInt for RwLock<V> {
|
||||
fn get_radix(&self) -> usize {
|
||||
self.read().unwrap().get_radix()
|
||||
}
|
||||
}
|
||||
|
||||
struct PosUIntFromDigits {
|
||||
radix: usize,
|
||||
src_digits: Option<Arc<dyn SequenceView<Item = usize>>>,
|
||||
cast: Arc<RwLock<ObserverBroadcast<dyn PositionalUInt>>>
|
||||
}
|
||||
|
||||
impl View for PosUIntFromDigits {
|
||||
type Msg = usize;
|
||||
}
|
||||
|
||||
impl SequenceView for PosUIntFromDigits {
|
||||
type Item = usize;
|
||||
|
||||
fn get(&self, idx: &usize) -> Option<usize> {
|
||||
self.src_digits.get(idx)
|
||||
}
|
||||
|
||||
fn len(&self) -> Option<usize> {
|
||||
self.src_digits.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl PositionalUInt for PosUIntFromDigits {
|
||||
fn get_radix(&self) -> usize {
|
||||
self.radix
|
||||
}
|
||||
}
|
||||
|
||||
impl Observer<dyn SequenceView<Item = usize>> for PosUIntFromDigits {
|
||||
fn reset(&mut self, new_src: Option<Arc<dyn SequenceView<Item = usize>>>) {
|
||||
self.src_digits = new_src;
|
||||
// self.cast.write().unwrap().notify(0);
|
||||
}
|
||||
|
||||
fn notify(&mut self, idx: &usize) {
|
||||
self.cast.write().unwrap().notify(idx);
|
||||
}
|
||||
}
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||
|
||||
pub trait DigitSeqProjection {
|
||||
fn to_positional_uint(&self, radix: usize) -> OuterViewPort<dyn PositionalUInt>;
|
||||
}
|
||||
|
||||
impl DigitSeqProjection for OuterViewPort<dyn SequenceView<Item = usize>> {
|
||||
fn to_positional_uint(&self, radix: usize) -> OuterViewPort<dyn PositionalUInt> {
|
||||
let port = ViewPort::new();
|
||||
port.add_update_hook(Arc::new(self.0.clone()));
|
||||
|
||||
let proj = Arc::new(RwLock::new(PosUIntFromDigits {
|
||||
radix,
|
||||
src_digits: None,
|
||||
cast: port.inner().get_broadcast()
|
||||
}));
|
||||
|
||||
self.add_observer(proj.clone());
|
||||
port.set_view(Some(proj));
|
||||
port.into_outer()
|
||||
}
|
||||
}
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||
|
||||
struct PosUIntToDigits {
|
||||
src: Option<Arc<dyn PositionalUInt>>,
|
||||
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = usize>>>>
|
||||
}
|
||||
|
||||
impl View for PosUIntToDigits {
|
||||
type Msg = usize;
|
||||
}
|
||||
|
||||
impl SequenceView for PosUIntToDigits {
|
||||
type Item = usize;
|
||||
|
||||
fn get(&self, idx: &usize) -> Option<usize> {
|
||||
self.src.get(idx)
|
||||
}
|
||||
|
||||
fn len(&self) -> Option<usize> {
|
||||
self.src.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Observer<dyn PositionalUInt> for PosUIntToDigits {
|
||||
fn reset(&mut self, view: Option<Arc<dyn PositionalUInt>>) {
|
||||
self.src = view;
|
||||
// self.cast.notify_all();
|
||||
}
|
||||
|
||||
fn notify(&mut self, idx: &usize) {
|
||||
self.cast.notify(idx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,69 +1,127 @@
|
|||
use {
|
||||
r3vi::{
|
||||
view::{
|
||||
View, ViewPort,
|
||||
InnerViewPort, Observer, OuterViewPort,
|
||||
ObserverBroadcast,
|
||||
sequence::*,
|
||||
list::*
|
||||
},
|
||||
buffer::{vec::*}
|
||||
},
|
||||
crate::{
|
||||
editors::integer::{
|
||||
PositionalUInt
|
||||
},
|
||||
repr_tree::{ReprTree, ReprLeaf}
|
||||
},
|
||||
std::sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||
|
||||
pub trait PosIntProjections {
|
||||
fn transform_radix(&self, dst_radix: usize) -> OuterViewPort<dyn SequenceView<Item = usize>>;
|
||||
// fn to_digits(&self) -> OuterViewPort<dyn SequenceView<Item = usize>>;
|
||||
}
|
||||
|
||||
impl PosIntProjections for OuterViewPort<dyn PositionalUInt> {
|
||||
fn transform_radix(&self, dst_radix: usize) -> OuterViewPort<dyn SequenceView<Item = usize>> {
|
||||
let port = ViewPort::<dyn SequenceView<Item = usize>>::new();
|
||||
port.add_update_hook(Arc::new(self.0.clone()));
|
||||
|
||||
// let mut vec_port = ViewPort::new();
|
||||
let proj = Arc::new(RwLock::new(RadixProjection {
|
||||
src: None,
|
||||
dst_radix,
|
||||
dst_digits: VecBuffer::new(),
|
||||
cast: port.inner().get_broadcast()
|
||||
}));
|
||||
|
||||
self.add_observer(proj.clone());
|
||||
port.set_view(Some(proj as Arc<dyn SequenceView<Item = usize>>));
|
||||
port.into_outer()
|
||||
}
|
||||
/*
|
||||
fn to_digits(&self) -> OuterViewPort<dyn SequenceView<Item = usize>> {
|
||||
let port = ViewPort::new();
|
||||
port.add_update_hook(Arc::new(self.0.clone()));
|
||||
let proj = Arc::new(RwLock::new(PosUIntToDigits {
|
||||
src: None,
|
||||
cast: port.inner().get_broadcast()
|
||||
}));
|
||||
self.add_observer(proj.clone());
|
||||
port.inner().set_view(Some(proj));
|
||||
port.into_outer()
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||
|
||||
pub struct RadixProjection {
|
||||
src_radix: usize,
|
||||
src: Option<Arc<dyn PositionalUInt>>,
|
||||
dst_radix: usize,
|
||||
src_digits: Option<Arc<dyn SequenceView<Item = usize>>>,
|
||||
dst_digits: RwLock<VecBuffer<usize>>,
|
||||
dst_digits: VecBuffer<usize>,
|
||||
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = usize>>>>
|
||||
}
|
||||
|
||||
impl View for RadixProjection {
|
||||
type Msg = usize;
|
||||
}
|
||||
|
||||
impl SequenceView for RadixProjection {
|
||||
type Item = usize;
|
||||
|
||||
fn get(&self, idx: &usize) -> Option<usize> {
|
||||
if *idx < self.dst_digits.len() {
|
||||
Some(self.dst_digits.get(*idx))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn len(&self) -> Option<usize> {
|
||||
Some(self.dst_digits.len())
|
||||
}
|
||||
}
|
||||
|
||||
impl PositionalUInt for RadixProjection {
|
||||
fn get_radix(&self) -> usize {
|
||||
self.dst_radix
|
||||
}
|
||||
}
|
||||
|
||||
impl Observer< dyn PositionalUInt > for RadixProjection {
|
||||
fn reset(&mut self, view: Option<Arc<dyn PositionalUInt>>) {
|
||||
self.src = view;
|
||||
self.update();
|
||||
}
|
||||
|
||||
fn notify(&mut self, idx: &usize) {
|
||||
self.update();
|
||||
// self.update_digit(idx)
|
||||
}
|
||||
}
|
||||
|
||||
impl RadixProjection {
|
||||
pub fn new(
|
||||
// static parameters
|
||||
//---
|
||||
src_radix: usize,
|
||||
dst_radix: usize,
|
||||
//---
|
||||
/// recalculate everything
|
||||
fn update(&mut self) {
|
||||
// let mut dst = self.dst_digits;
|
||||
let old_len = self.dst_digits.len();
|
||||
self.dst_digits.clear();
|
||||
|
||||
// dynamic parameters
|
||||
//---
|
||||
// input
|
||||
src_digits: OuterViewPort<dyn SequenceView<Item = usize>>,
|
||||
// output
|
||||
dst_digits: InnerViewPort<RwLock<Vec<usize>>>,
|
||||
//---
|
||||
) -> Arc<RwLock<Self>> {
|
||||
dst_digits.0.add_update_hook(Arc::new(src_digits.0.clone()));
|
||||
let proj = Arc::new(RwLock::new(RadixProjection {
|
||||
src_radix,
|
||||
dst_radix,
|
||||
src_digits: None,
|
||||
dst_digits: RwLock::new(VecBuffer::with_port(dst_digits)),
|
||||
}));
|
||||
src_digits.add_observer(proj.clone());
|
||||
proj
|
||||
}
|
||||
|
||||
fn machine_int(&self) -> usize {
|
||||
let mut val = 0;
|
||||
let mut r = 1;
|
||||
for i in 0..self.src_digits.len().unwrap_or(0) {
|
||||
val += r * self.src_digits.get(&i).unwrap();
|
||||
r *= self.src_radix;
|
||||
if let Some(src) = self.src.as_ref() {
|
||||
let mut val = src.get_value();
|
||||
while val > 0 {
|
||||
self.dst_digits.push(val % self.dst_radix);
|
||||
val /= self.dst_radix;
|
||||
}
|
||||
}
|
||||
|
||||
val
|
||||
}
|
||||
|
||||
// recalculate everything
|
||||
fn update(&self) {
|
||||
let mut dst = self.dst_digits.write().unwrap();
|
||||
dst.clear();
|
||||
|
||||
let mut val = self.machine_int();
|
||||
|
||||
while val > 0 {
|
||||
dst.push(val % self.dst_radix);
|
||||
val /= self.dst_radix;
|
||||
let new_len = self.dst_digits.len();
|
||||
for i in 0 .. usize::max(old_len, new_len) {
|
||||
self.cast.write().unwrap().notify(&i);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,18 +142,3 @@ impl RadixProjection {
|
|||
}
|
||||
}
|
||||
|
||||
impl Observer<dyn SequenceView<Item = usize>> for RadixProjection {
|
||||
fn reset(&mut self, view: Option<Arc<dyn SequenceView<Item = usize>>>) {
|
||||
self.src_digits = view;
|
||||
}
|
||||
|
||||
fn notify(&mut self, _idx: &usize) {
|
||||
// todo:
|
||||
// src digit i changed.
|
||||
// which dst-digits does it affect?
|
||||
// update dst-digit j:
|
||||
|
||||
// ...but for now the easy way
|
||||
self.update();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,6 +92,41 @@ pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
|
|||
}
|
||||
);
|
||||
|
||||
let mt = crate::repr_tree::MorphismType {
|
||||
src_type: Context::parse(&ctx, "<List <Digit 16>>~EditTree"),
|
||||
dst_type: Context::parse(&ctx, "<List <Digit 16>~Char>")
|
||||
};
|
||||
ctx.write().unwrap().morphisms.add_morphism(
|
||||
mt,
|
||||
{
|
||||
let ctx = ctx.clone();
|
||||
move |src_rt, σ| {
|
||||
let edittree =
|
||||
src_rt
|
||||
.descend(Context::parse(&ctx, "EditTree")).unwrap()
|
||||
.singleton_buffer::<EditTree>();
|
||||
|
||||
let list_edit = edittree.get().get_edit::< ListEditor >().unwrap();
|
||||
let edittree_items = list_edit.read().unwrap().data.get_port().to_list();
|
||||
src_rt.write().unwrap().insert_leaf(
|
||||
vec![ Context::parse(&ctx, "<List Char>") ].into_iter(),
|
||||
ReprLeaf::from_view(
|
||||
edittree_items
|
||||
.map(
|
||||
|edittree_char|
|
||||
edittree_char
|
||||
.read().unwrap()
|
||||
.get_edit::<crate::editors::digit::editor::DigitEditor>().unwrap()
|
||||
.read().unwrap()
|
||||
.get_char()
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
let mt = crate::repr_tree::MorphismType {
|
||||
src_type: Context::parse(&ctx, "<List Char>"),
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
|
||||
#![feature(trait_upcasting)]
|
||||
|
||||
pub mod repr_tree;
|
||||
pub mod edit_tree;
|
||||
pub mod editors;
|
||||
|
|
|
@ -396,6 +396,10 @@ impl ReprTree {
|
|||
pub fn view_u64(&self) -> OuterViewPort<dyn SingletonView<Item = u64>> {
|
||||
self.get_port::<dyn SingletonView<Item = u64>>().expect("no u64-view available")
|
||||
}
|
||||
|
||||
pub fn view_usize(&self) -> OuterViewPort<dyn SingletonView<Item = usize>> {
|
||||
self.get_port::<dyn SingletonView<Item = usize>>().expect("no usize-view available")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -412,6 +416,7 @@ pub trait ReprTreeExt {
|
|||
fn view_char(&self) -> OuterViewPort<dyn SingletonView<Item = char>>;
|
||||
fn view_u8(&self) -> OuterViewPort<dyn SingletonView<Item = u8>>;
|
||||
fn view_u64(&self) -> OuterViewPort<dyn SingletonView<Item = u64>>;
|
||||
fn view_usize(&self) -> OuterViewPort<dyn SingletonView<Item = usize>>;
|
||||
|
||||
fn singleton_buffer<T: Clone + Send + Sync + 'static>(&self) -> SingletonBuffer<T>;
|
||||
fn vec_buffer<T: Clone + Send + Sync + 'static>(&self) -> VecBuffer<T>;
|
||||
|
@ -446,6 +451,10 @@ impl ReprTreeExt for Arc<RwLock<ReprTree>> {
|
|||
self.read().unwrap().view_u64()
|
||||
}
|
||||
|
||||
fn view_usize(&self) -> OuterViewPort<dyn SingletonView<Item = usize>> {
|
||||
self.read().unwrap().view_usize()
|
||||
}
|
||||
|
||||
fn singleton_buffer<T: Clone + Send + Sync + 'static>(&self) -> SingletonBuffer<T> {
|
||||
self.write().unwrap().singleton_buffer::<T>().expect("")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue