lib-nested/nested/src/type_system/make_editor.rs

297 lines
9.8 KiB
Rust
Raw Normal View History

2022-05-08 23:30:49 +02:00
use {
crate::{
2023-02-13 18:39:45 +01:00
type_system::{Context, TypeTerm, ReprTree},
editors::{
char::*,
list::*,
integer::*,
product::*,
sum::*
},
tree::{NestedNode},
terminal::{TerminalEditor},
diagnostics::{Diagnostics},
2023-02-17 00:59:07 +01:00
type_system::{TypeTermEditor, MorphismTypePattern},
2022-05-08 23:30:49 +02:00
},
2022-06-19 23:13:21 +02:00
std::sync::{Arc, RwLock},
2023-02-13 18:39:45 +01:00
cgmath::Point2
2022-05-08 23:30:49 +02:00
};
pub fn init_mem_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let ctx = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
ctx.write().unwrap().add_typename("Vec".into());
ctx
}
pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
2022-12-18 01:39:01 +01:00
let ctx = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
2022-11-18 00:21:29 +01:00
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
"Char", Arc::new(
2022-12-18 01:39:01 +01:00
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, _depth: usize| {
2022-12-19 11:26:07 +01:00
Some(CharEditor::new_node(&ctx))
2022-11-18 00:21:29 +01:00
}
)
);
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("Sequence".into());
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("List".into());
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
"List", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, depth: usize| {
2022-11-18 00:21:29 +01:00
match ty {
TypeTerm::Type {
2022-12-18 01:39:01 +01:00
id: _, args
2022-11-18 00:21:29 +01:00
} => {
if args.len() > 0 {
Some(
2022-12-19 11:26:07 +01:00
PTYListEditor::new(
2023-02-13 18:39:45 +01:00
ctx,
2022-12-19 11:26:07 +01:00
args[0].clone(),
2023-02-13 18:39:45 +01:00
ListStyle::HorizontalSexpr,
depth + 1
).into_node()
2022-11-18 00:21:29 +01:00
)
} else {
None
}
}
2022-11-18 00:21:29 +01:00
_ => None
}
}
)
);
2022-11-18 00:21:29 +01:00
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("Symbol".into());
2023-02-17 00:59:07 +01:00
let pattern = MorphismTypePattern {
src_type: ctx.read().unwrap().type_term_from_str("( List Char )"),
dst_tyid: ctx.read().unwrap().get_typeid("Symbol").unwrap()
};
ctx.write().unwrap().add_morphism(pattern,
Arc::new(
|mut node, dst_type:_, depth| {
let editor = node.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let pty_editor = PTYListEditor::from_editor(
editor,
2023-02-13 18:39:45 +01:00
ListStyle::Plain,
2023-02-17 00:59:07 +01:00
depth
);
2023-02-13 18:39:45 +01:00
2023-02-17 00:59:07 +01:00
node.view = Some(pty_editor.pty_view());
node.cmd = Some(Arc::new(RwLock::new(pty_editor)));
Some(node)
}
)
);
ctx.write().unwrap().add_node_ctor(
"Symbol", Arc::new(
|ctx: Arc<RwLock<Context>>, dst_typ: TypeTerm, depth: usize| {
let mut node = Context::make_node(
&ctx,
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("List").unwrap(),
args: vec![
TypeTerm::new(ctx.read().unwrap().get_typeid("Char").unwrap())
]
},
depth
).unwrap();
node = Context::morph_node(ctx, node, dst_typ);
2023-02-13 18:39:45 +01:00
Some(node)
2022-11-18 00:21:29 +01:00
}
)
);
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("String".into());
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
"String", Arc::new(
2022-12-18 01:39:01 +01:00
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
2023-02-13 18:39:45 +01:00
let mut node = PTYListEditor::new(
ctx.clone(),
ctx.read().unwrap().type_term_from_str("( Char )").unwrap(),
ListStyle::DoubleQuote,
depth + 1
).into_node();
node.data = Some(ReprTree::ascend(
&node.data.unwrap(),
ctx.read().unwrap().type_term_from_str("( String )").unwrap()
));
Some(node)
2022-11-18 00:21:29 +01:00
}
)
);
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("TypeTerm".into());
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
"TypeTerm", Arc::new(
2022-12-18 01:39:01 +01:00
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
2022-12-19 11:26:07 +01:00
Some(TypeTermEditor::new(ctx, depth).into_node())
2022-11-18 00:21:29 +01:00
}
)
);
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_typename("TerminalEvent".into());
ctx
}
pub fn init_math_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
2022-12-18 01:39:01 +01:00
let ctx = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
ctx.write().unwrap().add_typename("MachineInt".into());
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_typename("u32".into());
ctx.write().unwrap().add_typename("BigEndian".into());
2022-12-19 11:26:07 +01:00
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
2022-12-19 11:26:07 +01:00
"Digit", Arc::new(
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, _depth: usize| {
match ty {
TypeTerm::Type {
id: _, args
} => {
if args.len() > 0 {
match args[0] {
TypeTerm::Num(radix) => {
2023-02-13 18:39:45 +01:00
let node = DigitEditor::new(ctx.clone(), radix as u32).into_node();
2022-12-19 11:26:07 +01:00
Some(
2023-02-13 18:39:45 +01:00
node
2022-12-19 11:26:07 +01:00
)
},
_ => None
}
} else {
None
}
}
_ => None
}
}
)
);
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("PosInt".into());
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
"PosInt", Arc::new(
2022-12-19 11:26:07 +01:00
|ctx: Arc<RwLock<Context>>, ty: TypeTerm, _depth: usize| {
match ty {
TypeTerm::Type {
2022-12-18 01:39:01 +01:00
id: _, args
} => {
if args.len() > 0 {
match args[0] {
TypeTerm::Num(radix) => {
Some(
2022-12-19 11:26:07 +01:00
PosIntEditor::new(ctx.clone(), radix as u32).into_node()
)
},
_ => None
}
} else {
None
}
}
_ => None
}
}
)
);
2022-11-18 00:21:29 +01:00
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("RGB".into());
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
2023-02-13 18:39:45 +01:00
"RGB", Arc::new(
2023-02-17 00:59:07 +01:00
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
2023-02-13 18:39:45 +01:00
let editor = ProductEditor::new(depth, ctx.clone())
.with_t(Point2::new(0, 0), "r: ")
.with_n(Point2::new(1, 0),
vec![
ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap()
])
.with_t(Point2::new(0, 1), "g: ")
.with_n(Point2::new(1, 1),
vec![
ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap()
])
.with_t(Point2::new(0, 2), "b: ")
.with_n(Point2::new(1, 2),
vec![
ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap()
]
);
let view = editor.get_term_view();
let diag = editor.get_msg_port();
let editor = Arc::new(RwLock::new(editor));
let node = NestedNode::new()
.set_ctx(ctx)
.set_cmd(editor.clone())
.set_nav(editor.clone())
.set_view(view)
.set_diag(diag)
;
Some(node)
}
));
2022-11-18 00:21:29 +01:00
ctx
2022-05-08 23:30:49 +02:00
}
pub fn init_os_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
2022-12-18 01:39:01 +01:00
let ctx = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("PathSegment".into());
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
"PathSegment", Arc::new(
2022-12-18 01:39:01 +01:00
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
2023-02-13 18:39:45 +01:00
let mut node = PTYListEditor::new(
ctx.clone(),
ctx.read().unwrap().type_term_from_str("( Char )").unwrap(),
ListStyle::Plain,
depth + 1
).into_node();
node.data = Some(ReprTree::ascend(
&node.data.unwrap(),
ctx.read().unwrap().type_term_from_str("( PathSegment )").unwrap()
));
Some(node)
}
)
);
2023-02-13 18:39:45 +01:00
ctx.write().unwrap().add_list_typename("Path".into());
2023-02-17 00:59:07 +01:00
ctx.write().unwrap().add_node_ctor(
"Path", Arc::new(
2022-12-18 01:39:01 +01:00
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
2023-02-13 18:39:45 +01:00
let mut node = PTYListEditor::new(
ctx.clone(),
ctx.read().unwrap().type_term_from_str("( PathSegment )").unwrap(),
ListStyle::Path,
depth + 1
).into_node();
node.data = Some(ReprTree::ascend(
&node.data.unwrap(),
ctx.read().unwrap().type_term_from_str("( Path )").unwrap()
));
Some(node)
}
)
);
ctx
}
2022-05-08 23:30:49 +02:00
2022-11-18 00:21:29 +01:00