From 81a22aa83153d6e830972662666b05a918daa32e Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 18 Aug 2023 00:16:16 +0200 Subject: [PATCH] move ctx init for integer types into integer module --- nested/src/editors/integer/ctx.rs | 148 ++++++++++++++++++++++++++ nested/src/editors/integer/mod.rs | 2 + nested/src/type_system/make_editor.rs | 132 +---------------------- 3 files changed, 152 insertions(+), 130 deletions(-) create mode 100644 nested/src/editors/integer/ctx.rs diff --git a/nested/src/editors/integer/ctx.rs b/nested/src/editors/integer/ctx.rs new file mode 100644 index 0000000..f7f07c3 --- /dev/null +++ b/nested/src/editors/integer/ctx.rs @@ -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>) -> Arc> { + 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>, 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::>().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>, 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 +} + diff --git a/nested/src/editors/integer/mod.rs b/nested/src/editors/integer/mod.rs index d6229ab..d8309f8 100644 --- a/nested/src/editors/integer/mod.rs +++ b/nested/src/editors/integer/mod.rs @@ -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 }; diff --git a/nested/src/type_system/make_editor.rs b/nested/src/type_system/make_editor.rs index dde7243..7c32483 100644 --- a/nested/src/type_system/make_editor.rs +++ b/nested/src/type_system/make_editor.rs @@ -51,10 +51,10 @@ pub fn init_editor_ctx(parent: Arc>) -> Arc> { 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>, _ty: TypeTerm, _depth: usize| { @@ -68,7 +68,6 @@ pub fn init_editor_ctx(parent: Arc>) -> Arc> { 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>) -> Arc> { ctx0 } -pub fn init_math_ctx(parent: Arc>) -> Arc> { - 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>, 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::>().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>, 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 -}