move ctx init for integer types into integer module

This commit is contained in:
Michael Sippel 2023-08-18 00:16:16 +02:00
parent 408f79be8e
commit 81a22aa831
Signed by: senvas
GPG key ID: F96CF119C34B64A6
3 changed files with 152 additions and 130 deletions

View file

@ -0,0 +1,148 @@
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
}

View file

@ -1,10 +1,12 @@
pub mod add;
pub mod editor;
pub mod radix;
pub mod ctx;
pub use {
add::Add,
editor::{DigitEditor, PosIntEditor},
radix::RadixProjection,
ctx::init_integer_ctx
};

View file

@ -51,10 +51,10 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let ctx0 = Arc::new(RwLock::new(Context::with_parent(Some(parent))));
ListEditor::init_ctx( &ctx0 );
let mut ctx = ctx0.write().unwrap();
// TODO:: CharEditor::init_ctx( &ctx );
// TODO:: CharEditor::init_ctx( &ctx );
ctx.add_node_ctor(
"Char", Arc::new(
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, _depth: usize| {
@ -68,7 +68,6 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
ctx.add_list_typename("SepSeq".into());
ctx.add_typename("NestedNode".into());
ctx.add_list_typename("Symbol".into());
let pattern = MorphismTypePattern {
src_tyid: ctx.get_typeid("List"),
@ -150,131 +149,4 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
ctx0
}
pub fn init_math_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("TimeSinceEpoch".into());
ctx.add_typename("Duration".into());
ctx.add_typename("Seconds".into());
ctx.add_typename("".into());
drop(ctx);
ctx0
}