lib-nested/nested/src/editors/integer/ctx.rs

149 lines
5 KiB
Rust
Raw Normal View History

use {
crate::{
type_system::{Context, TypeTerm, ReprTree},
editors::{
char::*,
list::*,
integer::*,
product::*
},
tree::{NestedNode},
diagnostics::{Diagnostics},
type_system::{MorphismTypePattern},
},
std::sync::{Arc, RwLock},
cgmath::Point2
};
pub fn init_integer_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let ctx0 = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
let mut ctx = ctx0.write().unwrap();
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: 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());
let pattern = MorphismTypePattern {
src_tyid: ctx.get_typeid("List"),
dst_tyid: ctx.get_typeid("PosInt").unwrap()
};
ctx.add_morphism(pattern,
Arc::new(
|mut node, dst_type| {
let depth = node.depth.get();
let editor = node.editor.get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
// 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) => {
PTYListController::for_node(
&mut node,
Some(','),
None,
);
PTYListStyle::for_node(
&mut node,
("0d", "", "")
);
Some(node)
},
_ => None
}
} else {
None
}
}
_ => None
}
}
)
);
ctx.add_node_ctor(
"PosInt", Arc::new(
|ctx0: Arc<RwLock<Context>>, dst_typ: TypeTerm, depth: 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+1
).unwrap();
node = node.morph(dst_typ);
Some(node)
}
_ => None
}
} else {
None
}
}
_ => None
}
}
)
);
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());
drop(ctx);
ctx0
}