From 4b6cc1ee553561bc1e449c042372bd650e985409 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 26 Feb 2023 19:41:10 +0100 Subject: [PATCH] implement first typelader-compatibility algorithm --- nested/src/editors/product/editor.rs | 6 ++-- nested/src/editors/product/nav.rs | 4 +-- nested/src/editors/product/segment.rs | 2 +- nested/src/tree/treetype.rs | 2 +- nested/src/type_system/make_editor.rs | 15 +++++++-- nested/src/type_system/type_term.rs | 45 +++++++++++++++++++++++++-- 6 files changed, 61 insertions(+), 13 deletions(-) diff --git a/nested/src/editors/product/editor.rs b/nested/src/editors/product/editor.rs index 5389330..f484dae 100644 --- a/nested/src/editors/product/editor.rs +++ b/nested/src/editors/product/editor.rs @@ -90,7 +90,7 @@ impl ProductEditor { self.n_indices.push(pos); let mut b = VecBuffer::new(); - b.push(crate::diagnostics::make_todo(crate::terminal::make_label(&format!("complete {}", self.ctx.read().unwrap().type_term_to_str(&n[0]))))); + b.push(crate::diagnostics::make_todo(crate::terminal::make_label(&format!("complete {}", self.ctx.read().unwrap().type_term_to_str(&n.0[0]))))); self.msg_buf.push(Some(b.get_port().to_sequence())); self } @@ -157,7 +157,7 @@ impl ProductEditor { self.msg_buf.update(idx as usize, Some(e.get_msg_port())); } else { let mut b = VecBuffer::new(); - b.push(crate::diagnostics::make_todo(crate::terminal::make_label(&format!("complete {}", self.ctx.read().unwrap().type_term_to_str(&t[0]))))); + b.push(crate::diagnostics::make_todo(crate::terminal::make_label(&format!("complete {}", self.ctx.read().unwrap().type_term_to_str(&t.0[0]))))); self.msg_buf.update(idx as usize, Some(b.get_port().to_sequence())); @@ -238,7 +238,7 @@ impl ObjCommander for ProductEditor { } } } else { - let mut e = Context::make_node(&self.ctx, t[0].clone(), *ed_depth+1).unwrap(); + let mut e = Context::make_node(&self.ctx, t.0[0].clone(), *ed_depth+1).unwrap(); *editor = Some(e.clone()); update_segment = true; diff --git a/nested/src/editors/product/nav.rs b/nested/src/editors/product/nav.rs index 7747715..e3ba8c9 100644 --- a/nested/src/editors/product/nav.rs +++ b/nested/src/editors/product/nav.rs @@ -71,7 +71,7 @@ impl TreeNav for ProductEditor { e.goto(c.clone()); } else if c.tree_addr.len() > 0 { // create editor - let mut e = Context::make_node(&self.ctx, t[0].clone(), *ed_depth+1).unwrap(); + let mut e = Context::make_node(&self.ctx, t.0[0].clone(), *ed_depth+1).unwrap(); *editor = Some(e.clone()); e.goto(c.clone()); } @@ -128,7 +128,7 @@ impl TreeNav for ProductEditor { } else { // create editor - let mut e = Context::make_node(&self.ctx, t[0].clone(), *ed_depth+1).unwrap(); + let mut e = Context::make_node(&self.ctx, t.0[0].clone(), *ed_depth+1).unwrap(); *editor = Some(e.clone()); e.goby(direction); } diff --git a/nested/src/editors/product/segment.rs b/nested/src/editors/product/segment.rs index 2f15410..238d330 100644 --- a/nested/src/editors/product/segment.rs +++ b/nested/src/editors/product/segment.rs @@ -65,7 +65,7 @@ impl ProductEditorSegment { }), ProductEditorSegment::N{ t, editor: None, ed_depth, cur_depth, cur_dist } => - make_label(&ctx.read().unwrap().type_term_to_str(&t[0])) + make_label(&ctx.read().unwrap().type_term_to_str(&t.0[0])) .map_item({ let _cur_depth = *cur_depth; let _ed_depth = *ed_depth; diff --git a/nested/src/tree/treetype.rs b/nested/src/tree/treetype.rs index 975dbc4..79760dd 100644 --- a/nested/src/tree/treetype.rs +++ b/nested/src/tree/treetype.rs @@ -8,7 +8,7 @@ use { pub trait TreeType { fn get_type(&self, _addr: &TreeAddr) -> TypeLadder { - vec![] + vec![].into() } } diff --git a/nested/src/type_system/make_editor.rs b/nested/src/type_system/make_editor.rs index 4dbe1d9..9e5b02c 100644 --- a/nested/src/type_system/make_editor.rs +++ b/nested/src/type_system/make_editor.rs @@ -61,6 +61,7 @@ pub fn init_editor_ctx(parent: Arc>) -> Arc> { } ) ); + ctx.write().unwrap().add_list_typename("Seq".into()); ctx.write().unwrap().add_list_typename("Sequence".into()); ctx.write().unwrap().add_list_typename("List".into()); @@ -203,6 +204,7 @@ pub fn init_math_ctx(parent: Arc>) -> Arc> { ctx.write().unwrap().add_typename("MachineInt".into()); ctx.write().unwrap().add_typename("u32".into()); + ctx.write().unwrap().add_typename("LittleEndian".into()); ctx.write().unwrap().add_typename("BigEndian".into()); ctx.write().unwrap().add_node_ctor( @@ -330,17 +332,17 @@ pub fn init_math_ctx(parent: Arc>) -> Arc> { .with_n(Point2::new(1, 0), vec![ ctx.read().unwrap().type_term_from_str("( PosInt 16 BigEndian )").unwrap() - ]) + ].into()) .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() - ]) + ].into()) .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() - ] + ].into() ); let view = editor.get_term_view(); @@ -358,6 +360,13 @@ pub fn init_math_ctx(parent: Arc>) -> Arc> { Some(node) } )); + + ctx.write().unwrap().add_typename("Date".into()); + ctx.write().unwrap().add_typename("ISO-8601".into()); + ctx.write().unwrap().add_typename("TimeSinceEpoch".into()); + ctx.write().unwrap().add_typename("Duration".into()); + ctx.write().unwrap().add_typename("Seconds".into()); + ctx.write().unwrap().add_typename("ℕ".into()); ctx } diff --git a/nested/src/type_system/type_term.rs b/nested/src/type_system/type_term.rs index fb3243e..bae86b6 100644 --- a/nested/src/type_system/type_term.rs +++ b/nested/src/type_system/type_term.rs @@ -3,7 +3,46 @@ use {crate::utils::Bimap, std::collections::HashMap}; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub type TypeID = u64; -pub type TypeLadder = Vec; + +#[derive(Clone)] +pub struct TypeLadder(pub Vec); + +impl From> for TypeLadder { + fn from(l: Vec) -> Self { + TypeLadder(l) + } +} + +impl TypeLadder { + /// if compatible, returns the number of descents neccesary + pub fn is_compatible_with(&self, other: &TypeLadder) -> Option { + if let Some(other_top_type) = other.0.first() { + for (i, t) in self.0.iter().enumerate() { + if t == other_top_type { + return Some(i); + } + } + + None + } else { + None + } + } + + pub fn is_matching_repr(&self, other: &TypeLadder) -> Result> { + if let Some(start) = self.is_compatible_with(other) { + for (i, (t1, t2)) in self.0.iter().skip(start).zip(other.0.iter()).enumerate() { + if t1 != t2 { + return Err(Some((start, i))); + } + } + + Ok(start) + } else { + Err(None) + } + } +} //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> @@ -30,7 +69,7 @@ impl TypeTerm { pub fn num_arg(&mut self, v: i64) -> &mut Self { self.arg(TypeTerm::Num(v)) } - + pub fn from_str(s: &str, names: &HashMap) -> Option { let mut term_stack = Vec::>::new(); @@ -107,7 +146,7 @@ impl TypeTerm { pub fn to_str(&self, names: &HashMap) -> String { match self { TypeTerm::Type { id, args } => format!( - "« {} {}»", + "( {} {})", names[id], if args.len() > 0 { args.iter().fold(String::new(), |str, term| {